LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-21-2010, 04:02 PM   #1
mvmacd
LQ Newbie
 
Registered: Nov 2009
Distribution: OpenSuSE 11.2
Posts: 12

Rep: Reputation: 0
Question I need to generate random number either 1 or 0?


Not only that but I need tons and tons of them generated, and I need it to be totally random each time.
Code:
matthew@mvm:/h/misc> cat a
#!/bin/bash
for i in {1..5}; do
echo $RANDOM
done
matthew@mvm:/h/misc> ./a
17767
9158
6249
18547
23633
matthew@mvm:/h/misc> ./a
17767
9158
6249
18547
23633
matthew@mvm:/h/misc>
There you can see that it is the same number. It does that every time!! Grrr. The strange thing is each time I manually type [or copy & paste] it into the terminal, it gives random [not static, like I am getting from my script] results.

The reason I want this is because I am making a script where this line will randomly go up or down [depending on if the random number is 1 or 0]. Thanks, Matt
 
Old 01-21-2010, 04:40 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I see 2 questions here:

1. Getting random numbers to be 0 or 1: try this:
Code:
echo $(($RANDOM%2))
2. Reseeding the random number generator: See here:
http://www.faqs.org/docs/bashman/bashref_60.html
scroll down to the entry for "RANDOM"
 
Old 01-21-2010, 04:43 PM   #3
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
RANDOM will contain only pseudo random numbers. If you need real random numbers you should consider reading from /dev/random or /dev/urandom.
 
Old 01-21-2010, 05:06 PM   #4
mvmacd
LQ Newbie
 
Registered: Nov 2009
Distribution: OpenSuSE 11.2
Posts: 12

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by pixellany View Post
I see 2 questions here:

1. Getting random numbers to be 0 or 1: try this:
Code:
echo $(($RANDOM%2))
2. Reseeding the random number generator: See here:
http://www.faqs.org/docs/bashman/bashref_60.html
scroll down to the entry for "RANDOM"
Still doesn't work:

Code:
matthew@mvm:/h/misc> cat numberr
#!/bin/bash
echo $RANDOM
RANDOM="1"
echo $RANDOM
matthew@mvm:/h/misc> ./numberr
24247
17767
matthew@mvm:/h/misc>
:Sigh: I don't really care if I use $RANDOM or /dev/random, just as long as it generates a 1 or 0 quickly! Because if there is a 1 the $num will go up, then it will dislpay $num number of "."s (Periods). So going quickly it will randomly go up, down, etc... How would I use /dev/random? Or how would I get the $RANDOM to reset each time before it is called?? Thanks

edit: Hm it looks like it did work....... But the only problem is the 2nd number always stays the same! :\ :?

Last edited by mvmacd; 01-21-2010 at 05:08 PM.
 
Old 01-21-2010, 06:50 PM   #5
mvmacd
LQ Newbie
 
Registered: Nov 2009
Distribution: OpenSuSE 11.2
Posts: 12

Original Poster
Rep: Reputation: 0
thanks, I got it to work by:

get_number () {
RANDOM=""
rndm=$RANDOM
}
 
  


Reply

Tags
number, random


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate a random number from a bourne shell script lothario Linux - Software 2 03-01-2007 11:01 PM
pls help on how i can generate 4 digit random number that are unique within the set icylicious Programming 7 08-22-2006 01:00 PM
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 06:05 AM
Python problems trying to generate a random number davholla Programming 0 10-27-2003 04:07 AM


All times are GMT -5. The time now is 01:14 PM.

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