August 1st, 2003, 02:15 PM
Pseudo-random Generator in C
Hi. I am a fairly new C programmer (I'm more experienced in Java and C++) and was just wondering if the C libraries provided a way to generate a random integer? Thank you!
August 1st, 2003, 02:18 PM
You should defintively get a C reference book...
August 1st, 2003, 02:20 PM
Thank you very much for your help.
August 1st, 2003, 02:36 PM
... and a C++ reference book by the look of it ;-)
August 1st, 2003, 02:37 PM
You could also look into srand if you want to create different random number each time. rand generates the same random number each time it is called
August 1st, 2003, 02:42 PM
... a very useful "feature" in debugging ;)
August 1st, 2003, 02:50 PM
Absolutely true, but was mentioning that srand could be used too to create random numbers:) ;)
August 1st, 2003, 04:03 PM
It may be that you understand what you mean but expressed it poorly, but your two posts could be considered misleading.
srand() doesn't generate random numbers, it seeds the random number generator. rand() doesn't generate the same number each time it's called, it returns the next number in the sequence. If you don't seed the generator before your first call to rand() it may start the sequence from the same point each time.
August 2nd, 2003, 10:14 PM
What are you using the random values for?
rand() is a very poor random number generator, useless for almost everything except for student exercises. If you are doing simulations (or, heaven forbid, cryptography) you will find that rand() is going to give anomalous results. In addition, it only provides a small set (it only provides numbers between 0 and 32767 and if I recall correctly, providing a different seed to srand() only shifts your starting point in the exact same sequence of random numbers.
August 3rd, 2003, 03:36 AM
Re: What are you using the random values for?
It provides at least 0 to 32767. Some implementations provide a greater range.
August 3rd, 2003, 08:50 AM
The RAND_MAX macro is set to equal to the maximum range rand() returns. It has been 32,767 on every machine I have tested it on (all Windows OS's). srand() does simply shift the starting point in the sequence, since rand() just returns the next number in the sequence based on the current number, the sequence is always the same.
You could attempt to use the least significant bits of the current time via high resolution timer, if one exists, to help construct a random number. These bits from the current time should be fairly random by themselves.
August 3rd, 2003, 09:11 AM
The random generator isn't for anything too fancy. I just have to write a small program in C that implements MPI on our computer cluster. I was just trying to figure out how to make calls to random processes. Thank you all for your help! I think I am all set now.
August 3rd, 2003, 10:48 AM
That wouldn't surprise me, and is consistent with what I said. But there are platforms using versions of RAND_MAX with much higher ranges, e.g. 0 .. 2,147,483,647.