Visit the LQ Articles and Editorials section
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Random number generator for 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

 09-12-2001, 05:45 PM #1 Steve_Taylor LQ Newbie   Registered: Sep 2001 Posts: 22 Rep: Random number generator for linux i have a Random number generatorfor win32 but when i compiled it and ran it on linux it always gave 0 as the generated number but on windows it gives a differnt number!? can anyone help? Code: ```int num; srand(time(NULL)); num=(rand()*100)/(RAND_MAX+1); printf(" rand == %d ",num);```
 09-12-2001, 08:09 PM #2 kin LQ Newbie   Registered: Sep 2001 Posts: 2 Rep: hi try to assign and print out as a double or float and use %f instead of %d, it might help.
 09-13-2001, 04:17 PM #3 Steve_Taylor LQ Newbie   Registered: Sep 2001 Posts: 22 Original Poster Rep: i tred that it still did not make a differnce does anyone know of any good randum number generators that if givvena number say 37 would generate a number between 1 and 37??
 09-23-2001, 05:44 PM #4 gluon Member   Registered: Jun 2001 Location: Switzerland Distribution: SuSE 8.[012], Gentoo Posts: 42 Rep: hi the main problem of your prog is that you have an overflow!!! Just try something like : int num; srand(time(NULL)); return (int) (((double) rand() / (double) RAND_MAX) * (double) limit) + 1; by
 09-23-2001, 09:39 PM #5 habiblove Member   Registered: Feb 2001 Location: Smyrna, TN Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix Posts: 59 Rep: Anything wrong with using this? srand(time(NULL)); array[i] = rand() % 1001;
 09-23-2001, 09:44 PM #6 habiblove Member   Registered: Feb 2001 Location: Smyrna, TN Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix Posts: 59 Rep: Sorry about previous post. I am adapting this from previous code. Anything wrong with using this? int num; srand(time(NULL)); num = rand() % 38; return num;
 09-23-2001, 10:59 PM #7 habiblove Member   Registered: Feb 2001 Location: Smyrna, TN Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix Posts: 59 Rep: Or if you wanted to create a random number from 1 to limit, try this: int num; int limit; srand(time(NULL)); num = (rand() % limit) + 1; return num;
 09-24-2001, 03:54 PM #8 Steve_Taylor LQ Newbie   Registered: Sep 2001 Posts: 22 Original Poster Rep: Dont mean to sound stupid but what does the % do i have looked it up in the book that i have and cant find it? well not used in this way.
 09-24-2001, 05:02 PM #9 habiblove Member   Registered: Feb 2001 Location: Smyrna, TN Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix Posts: 59 Rep: The % is called the modulus. It gives you the remainder of dividing two numbers. Thus, 13 % 10 returns 3. Using this is nice because: ( number % limit ) + 1 always returns a number from 1 through the limit.
 09-25-2001, 05:20 AM #10 gluon Member   Registered: Jun 2001 Location: Switzerland Distribution: SuSE 8.[012], Gentoo Posts: 42 Rep: OK habiblove, you're right : modulus seems to be much easier... sorry !
 09-25-2001, 07:41 PM #11 habiblove Member   Registered: Feb 2001 Location: Smyrna, TN Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix Posts: 59 Rep: It is pretty neat. Who said a college education isn't worth anything?
 09-26-2001, 01:39 PM #12 Steve_Taylor LQ Newbie   Registered: Sep 2001 Posts: 22 Original Poster Rep: i went to college but din't get all that much out of it
 09-30-2001, 05:21 PM #13 Colonel Panic Member   Registered: Jul 2001 Location: California Distribution: Red Hat 7.1, Slackware 8.0 Posts: 216 Rep: main() { printf("I have no idea!\n") } *****Colonel Panic*****