A standard way of generating a uniform distribution of floats in C.

Started by
8 comments, last by Makaan 14 years ago
Is there a standard technique for generating a uniform distribution of floating point numbers (floats and doubles) in C? From what I've found on Google, you need to use rand() and then coerce it to a float. But it seems unsafe to do if I want a truly uniform distribution. I don't really want to go into the details of floating point representation and random number entropy. Surely, this functionality is common enough to be available in the standard C library or in some relatively small 3rd party library (I'm not certain what platform I'll be on yet, either). Any help in finding such a library would be appreciated.
Advertisement
The easiest thing to do is use rand() and project into [0.0, 1.0) by dividing by (float)MAX_INT.

Alternatively, use something like GSL but that's prolly overkill for simply generating a uniform distribution.

Instead of using an LCG PRNG like rand(), you could grab a more sophisticated generator like the Mersenne Twister and then project it onto [0.0, 1.0).

Stephen M. Webb
Professional Free Software Developer

1. switch to c++ (at least for just this part of your code)
2. use boost random

Sample code:
boost::mt19937 generator(time(NULL));double number = boost::uniform_real<double>(0,1)(generator);
Switching languages for random numbers seems like overkill beyond overkill.

I am a bit surprised it's not a standard part of libc, but I will try the MAX_INT method.

Thanks.
float sfrand( int *seed ){    float res;    seed[0] *= 16807;    *((unsigned int *) &res) = ( ((unsigned int)seed[0])>>9 ) | 0x40000000;    return( res-3.0f );}


from Iñigo Quilez
Anthony Umfer
Quote:Original post by Bregma
The easiest thing to do is use rand() and project into [0.0, 1.0) by dividing by (float)MAX_INT.


Are you sure you want MAX_INT and not RAND_MAX?
Actually, you want (RAND_MAX + 1.0) to yield a half-open range.
Look into algorithms like the LCG... [link]http://www.ie.metu.edu.tr/~ie505/lcgrand.c[/link] and [link]http://www.ie.metu.edu.tr/~ie505/lcgrand.h[/link] or multiple recursive generators (MRG). You should be able to find a free MRG implementation out there if you look for it.
Don't forget that rand() is not cryptographically secure, and it might not even be that random (depending on implementation, but it tends to not be so great).

So depending on your needs, it might not be good enough by itself.

Might wanna double check the distribution of the numbers it gives you to make sure it's happy.

I only bring this up cause your original post makes it seem like this is something that could be important to you.

If you want better random numbers there are ways to get them, including random.org which generates them from a combination of real world sources (;
The best one i use:
static unsigned int mirand = 1;float sfrand( void )//rezult is in [-1;1]{    unsigned int a;    mirand *= 16807;    a = (mirand&0x007fffff) | 0x40000000;    return( *((float*)&a) - 3.0f );}

from RGBA

This topic is closed to new replies.

Advertisement