views:

218

answers:

3

How do I generate a random number between 0 and 1?

+7  A: 

You can generate a pseudorandom number using stdlib.h. Simply include stdlib, then call

double random_number = rand() / (double)RAND_MAX;
Mark E
For reference, this will give values in [0.0, 1.0] i.e. including both 0.0 and 1.0.
Philip Potter
And most importantly, it will most likely not be fair (!) There are many implementations where the set [0.0, 1.0] contains more than RAND_MAX elements. (_In C, the set [0.0, 1.0] is a countable finite set_)
MSalters
@MSalters, that's an interesting problem, any thoughts on a solution?
Mark E
@Mark: it really depends on the application. The first thing to realize is that the set `[0.00, 0.01]` contains many more elements than the set `[0.99, 1.00]`. This is because `1E-100 > 0` but `1 - 1E-100 == 1`. For many applications this can be counteracted by setting `p(x) = 1.0/(_nextafter(x)-x);` although this technically works for the range `[0.0, 1.0)`. The result is that p(x) depends on x, but `SUM[0<=i<x](p(i)) == x`
MSalters
Oh, and often it just doesn't matter - many applications would work even with `double arr[11] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 }; return arr[rand % 11];`
MSalters
+2  A: 

Assuming OP wants either 0 or 1:

srand(time(NULL));
foo = rand() & 1;

Edit inspired by comment: Old rand() implementations had a flaw - lower-order bits had much shorter periods than higher-order bits so use of low-order bit for such implementations isn't good. If you know your rand() implementation suffers from this flaw, use high-order bit, like this:

foo = rand() >> (sizeof(int)*8-1)

assuming regular 8-bits-per-byte architectures

qrdl
This works perfectly, thanks.
tm1
@tm1 Accepting answer is SO's way to thank :)
qrdl
you should shift the result of `rand()` to account for problems with linear congruential generators
Christoph
@Christoph Thank you, I've edited my post to reflect your point.
qrdl
Who said `rand()` uses all 32 bits? `RAND_MAX` on MSVC is 0x7fff, for instance (http://msdn.microsoft.com/en-us/library/2dfe3bzd%28VS.80%29.aspx).
KennyTM
you don't have to assume 8-bits-per-byte if you use `rand() >> (sizeof(int)*CHAR_BIT-1)`.
Philip Potter
use an arbitrary shift `(rand() >> RAND_SHIFT)
Christoph
You can always use `rand() / (RAND_MAX + 1)` to get a single random bit, independent of the exact value of `RAND_MAX`. This could be slightly unfair if RAND_MAX is even, though. Easiest to see if you assume RAND_MAX is 2: `rand()` would generate either 0, 1 or 2. It's impossible to extract a single unbiased random bit from that.
MSalters
@MSalters: Won't you always get 0 with `rand() / (RAND_MAX + 1)`?
KennyTM
+1  A: 

man 3 drand48 is exactly what you asked for.

The drand48() and erand48() functions return non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0 , 1.0].

These are found in #include <stdlib.h> on UNIX platforms. They're not in ANSI C, though, so (for example) you won't find them on Windows unless you bring your own implementation (e.g. LibGW32C).

ephemient