Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-16-2005, 10:18 PM   #1
LQ Newbie
Registered: Aug 2004
Posts: 21

Rep: Reputation: 15
can bash generate random digit?

i am currently using mac os x, i do "man rand" and i find that rand can do the job. However, when i use rand, it said command not find... is there anything i can do to get back my rand command? or is that other way to do it??
Old 08-16-2005, 10:27 PM   #2
Registered: Oct 2003
Posts: 568

Rep: Reputation: 30
echo $RANDOM
Old 08-16-2005, 11:32 PM   #3
LQ Newbie
Registered: Aug 2004
Posts: 21

Original Poster
Rep: Reputation: 15
thank you very much!!!
so there is no way to control the range??i have to use modulus??
Old 08-16-2005, 11:57 PM   #4
Registered: Oct 2003
Posts: 568

Rep: Reputation: 30
There maybe a way, however, if you need a larger number, you could maybe just use two, multiplied, added, whatever.
Maybe an algorithm whereby if the random is prime, generate another and multiply them....?

$RANDOM is a bash builtin. I dunno if you can alter it.
I tried typeset to change it - but no luck....
Old 08-17-2005, 02:35 AM   #5
Senior Member
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
See Example 9−29. Pseudorandom numbers, using awk
in the Advanced Bash−Scripting Guide
then x by whatever upper limit you may want

maybe you want to look at pinching numbers from /dev/urandom?

This srand / rand function in C (man srand, rand)
I have never figured out whether these functions (or any C function found in man) could
be called from bash :-( need to read more
Old 08-17-2005, 06:05 AM   #6
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
But you have to be careful with operations between random numbers, because you can change the distribution!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - choose a random line from a file ericcarlson Linux - Software 27 11-12-2013 01:25 AM
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
Generate files using bash kaon Linux - General 1 03-08-2005 01:59 PM
Python problems trying to generate a random number davholla Programming 0 10-27-2003 04:07 AM
bash random function NSKL Programming 6 02-02-2003 01:39 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:25 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration