Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org rand() function in linux
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 04-27-2009, 09:01 AM #1 ko589 LQ Newbie   Registered: Feb 2009 Posts: 8 Rep: rand() function in linux Why can't I generate a random number between 1 and 3 using the command rand(1,3) in linux. I've included the library stdlib.h but I keep getting the error that the random() function can not accept two parameters.
 04-27-2009, 10:09 AM #2 ilikejam Senior Member Contributing Member   Registered: Aug 2003 Location: Glasgow Distribution: Fedora / Solaris Posts: 3,109 Rep: Because the rand() function doesn't take two arguments. It's either no arguments, or an single unsigned int as a seed. See 'man rand' for deeper enlightenment. Dave
 04-27-2009, 10:42 AM #3 ko589 LQ Newbie   Registered: Feb 2009 Posts: 8 Original Poster Rep: I need to generate a single random value between 1 and 3 ..I use the following command randNum = rand()%3 + 1; but everytime i run the code ..it generates the same value 2 :S
 04-27-2009, 11:20 AM #4 paulsm4 LQ Guru   Registered: Mar 2004 Distribution: SusE 8.2 Posts: 5,863 Blog Entries: 1 Rep: Hi - The standard library functions like "rand()" are called a "pseudo-random number generators" for good reason: they follow an *algorithm" and, unless you do something *different*, they will give the *same* results each time you execute them. Hence the need for a "seed". For example, read "time()", convert it to an unsigned int, and pass it as a seed the first time (and *only* the first time) you call "rand()". As ilikejam said, "read 'man rand' for enlightenment". Hope that helps .. PSM PS: An even *better* source of random numbers under Linux is /dev/urandom: http://en.wikipedia.org/wiki/Urandom Last edited by paulsm4; 04-27-2009 at 11:21 AM.
 04-27-2009, 11:30 AM #5 b0uncer LQ Guru   Registered: Aug 2003 Distribution: CentOS, OS X Posts: 5,131 Rep: You might also want to read conversations like this where it is pointed out that it might not be wise to use the %-operator to get a (pseudo-)random number between some limits; depending on the implementation of the random number generating function it might or might not make difference, but unless you want to risk getting bad results, consider other means. It's somewhat annoying to have to take this sort of things into account, but apparently it's inevitable with computer programming.
 04-27-2009, 12:03 PM #6 metrofox Member   Registered: Jan 2009 Location: Palermo, Italy Distribution: Slackware Posts: 236 Rep: Why don't you include the time.h? Code: ```[...] #include [...] randNum = rand(time(NULL))%3 + 1;``` Sincerely...I don't program in C at the moment and I've to study it yet, all that I know isn't much, so try it and let me know And remember, it might work. Last edited by metrofox; 04-27-2009 at 12:12 PM.
 04-28-2009, 08:48 AM #7 ko589 LQ Newbie   Registered: Feb 2009 Posts: 8 Original Poster Rep: Just to let u know I used the command : srand(time(NULL)) n = rand()%3 +1 and it works fine .. Thanks a lot for your answers

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post sa20358 Linux - Software 2 07-24-2008 10:19 PM dateofexpiration Linux - Software 1 08-14-2007 07:29 PM LinuxLala Linux - Software 0 04-21-2005 01:46 PM nazula Programming 1 04-19-2004 12:21 PM KneeLess Programming 12 10-01-2003 12:51 PM

LinuxQuestions.org

All times are GMT -5. The time now is 06:24 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -