If I do rand()/RAND_MAX, will it give me a random probability value?
If I do so, is it going to be that 50% (on average) of the values will be more than 0.5?
If I do rand()/RAND_MAX, will it give me a random probability value?
If I do so, is it going to be that 50% (on average) of the values will be more than 0.5?
Never use rand() for any purpose, ever.
random() is likely suitable for your needs. (#include <stdlib.h>) It generates a uniform distribution in the range 0..231-1. random() / (double)((1L << 31) - 1) should get you close to a uniform distribution between 0.0 and 1.0.
You can use srandomdev() to seed it in order to get a different sequence every time.
Here is a histogram of one billion values returned by random() in 256 bins over the range 0..231-1:
If you look closely, you can see the expected tiny variations from uniform along the top of the histogram.
rand() / (double)RAND_MAX will give you a random value.rand() have no mention of a uniform(or any other) distribution. There is no guarantee that "is it going to be that 50% (on average)".Have a uniform distribution in C is a different question. You may be interested in Generating a uniform distribution of INTEGERS in C
SO topic.