LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-30-2012, 12:16 PM   #1
bashMe
LQ Newbie
 
Registered: Nov 2012
Posts: 9

Rep: Reputation: Disabled
Lightbulb How to reseed RANDOM ?


Can anyone help me on how to reseed the random number generator. I have the expression
Code:
$(($RANDOM %5 +1))
in a program, and want to reseed the random number generator.

I entered info random, and it says to reseed the random number generator, I can use:
Code:
srandom(time(0))
However, when I enter this at the command line I just get an error:
Quote:
-bash: syntax error near unexpected token `time'
 
Old 11-30-2012, 01:24 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
RANDOM in bash is a built-in variable that outputs a random number between 0 and 32767. You can reseed it simply by setting it to any new integer value. the current epoch second value is a good choice.

Code:
RANDOM=$( date +%s )
Edit: I should also mention that it's really only pseudo-random. It will always output the same sequence of values if seeded with the same number. See the bash man page for a complete description.


srandom is not a command, but a C library function, and isn't usable inside the shell itself. Other than that I don't know much about it.

Last edited by David the H.; 11-30-2012 at 01:36 PM. Reason: addition and some rewording
 
Old 11-30-2012, 01:39 PM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Depending on the purpose the random numbers are intended for the time might be a bad choice of seed.
Code:
(df -k;ps auxww) | (echo ibase=16;echo -n q=; md5sum |tr '[:lower:]' '[:upper:]'| sed s/-/\\nq/) | bc
 
Old 11-30-2012, 01:52 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yeah, that's one reason I updated my last post to better explain what RANDOM is. The shell version is really only a convenience feature and shouldn't be used for serious purposes. But it's perfectly adequate if you just want to shuffle your playlist or something.

If you need something closer to true randomness you should probably be tapping the /dev/random or /dev/urandom devices instead. google can teach you how to do that, since they output raw binary rather than text integers.
 
Old 11-30-2012, 02:08 PM   #5
bashMe
LQ Newbie
 
Registered: Nov 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks fellow bashers, I will have to look over this and work on it this weekend. This is not just a casual application, but for data analysis with bootstraps. If it ends up too complicated, I might just end up generating the random numbers in Matlab, and then simply use the sun grid engine to iterate through the different bootstrap samples (I am a total Linux noob and am just using it to run Matlab code through our cluster).
 
Old 11-30-2012, 02:29 PM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Chapter 7 http://www.nrbook.com/a/bookcpdf.php or similar reading should precede use of randoms in a data analysis.
 
1 members found this post helpful.
  


Reply


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
[SOLVED] sed - replacing random numbers followed with space and more random chars gluposti Linux - Newbie 7 05-07-2012 08:36 PM
Box is attempting to scan and ssh into random machines using random usernames gianh Linux - Security 2 11-09-2008 06:42 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 02:42 PM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 07:48 PM


All times are GMT -5. The time now is 09:07 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration