LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-27-2009, 09:01 AM   #1
ko589
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Rep: Reputation: 0
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.
 
Old 04-27-2009, 10:09 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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
 
Old 04-27-2009, 10:42 AM   #3
ko589
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 04-27-2009, 11:20 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 04-27-2009, 11:30 AM   #5
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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.
 
Old 04-27-2009, 12:03 PM   #6
metrofox
Member
 
Registered: Jan 2009
Location: Palermo, Italy
Distribution: Slackware
Posts: 236

Rep: Reputation: 37
Why don't you include the time.h?

Code:
[...]
#include<time.h>
[...]
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.
 
Old 04-28-2009, 08:48 AM   #7
ko589
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
  


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
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 10:19 PM
looking for a linux audio player that uses the C language rand() function for random dateofexpiration Linux - Software 1 08-14-2007 07:29 PM
ImageMagick: the rand() function and using brackets in commands LinuxLala Linux - Software 0 04-21-2005 01:46 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
Need help using rand() in C KneeLess Programming 12 10-01-2003 12:51 PM


All times are GMT -5. The time now is 09:51 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