While searching for Tutorials on generating random numbers in C I found this topic
When I try to use the rand()
function without parameters, I always get 0. When I try to use the rand()
function with parameters, I always get the value 41. And whenever I try to use arc4random()
and random()
functions, I get a LNK2019 error.
Here's what I've done:
#include <stdlib.h>
int main()
{
int x;
x = rand(6);
printf("%d", x);
}
This code always generates 41. Where am I going wrong? I'm running Windows XP SP3 and using VS2010 Command Prompt as compiler.
Best Answer
You should call
srand()
before calling rand to initialize the random number generator.Either call it with a specific seed, and you will always get the same pseudo-random sequence
or call it with a changing sources, ie the time function
In response to Moon's Comment rand() generates a random number with an equal probability between 0 and RAND_MAX (a macro pre-defined in stdlib.h)
You can then map this value to a smaller range, e.g.
This might be sufficient for most uses, but its worth pointing out that in the first case using the mod operator introduces a slight bias if N does not divide evenly into
RAND_MAX+1
.Random number generators are interesting and complex, it is widely said that the rand() generator in the C standard library is not a great quality random number generator, read (http://en.wikipedia.org/wiki/Random_number_generation for a definition of quality).
http://en.wikipedia.org/wiki/Mersenne_twister (source http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html ) is a popular high quality random number generator.
Also, I am not aware of arc4rand() or random() so I cannot comment.