   [win32] Best way to generate random numbers?
  Is this still the standard way of generating random numbers with Visual C++?::srand ( static_cast<unsigned int> ( ::time ( NULL ) ) ) ;int number = ::rand () ;But I want to generate floating point numbers between [min_float, max_float]. How do I do that?   
Quote:Original post by hiigaraIs this still the standard way of generating random numbers with Visual C++?::srand ( static_cast<unsigned int> ( ::time ( NULL ) ) ) ;int number = ::rand () ;It is the standard way to generate random numbers in C, and it works in C++ just as well. Depending on your exact needs, you may be better off using a 3rd-party library which provides greater flexibility.Quote:But I want to generate floating point numbers between [min_float, max_float]. How do I do that?The simplest way is to convert the integer to a float, scaling to the desired range. Something like this:min_float + (float)rand() / (float)RAND_MAX * (max_float - min_float);  There are two separate issues here:(1) How can you get a stream of numbers that seem very random (i.e., have similar statistical properties to random numbers).(2) How can you use that stream of numbers to sample the probability distribution that you want.If you suspect that rand() is not good enough for you (some compilers may have implementations that are pretty bad, actually), you can use a good-quality pseudo-random number generator, like a Mersenne Twister.Point 2 is actually quite tractable, but it requires that you know what probability distribution you want to sample.;  First, be very clear that "random" numbers are extremely hard to generate.  You can even have grand philosophical discussions about if they are even possible to generate.As for pseudorandom number generators, there are many out there.  You need to know what their use will be.  Even good random number generators have patterns, and those can become visible in your game.The Boost libraries have 17 generators, each with their own properties, plus a bunch of adapters.  Several have good distribution of values.  A few of them support generating floating point values.The Mersenne Twister (available in part of Boost) can generate floating point numbers.  You need to be careful about the parameters if you want a good, mostly random distribution of results, but those are well documented.  It is very easy to mess up.  If you don't know the numbers and just guess, you can generate horrible results.  Google can find lots of good parameter sets.But you need to remember the purpose of your number generator.  Although the MT algorithm is good, it still isn't good enough for encryption.  Don't trust it as a seed for your SSL key generator.  For that kind of thing, you need the deviously-hard compute-expensive random number generators.   ;  Were you after something like what swiftcoder posted, or were you wanting a continuous distribution between the two limits?   ;  Some of the RNGs from Boost are included in Tr1, which is included in Visual Studio 2008.http://www.johndcook.com/cpp_TR1_random.html   ;Quote:Original post by iMalcWere you after something like what swiftcoder posted, or were you wanting a continuous distribution between the two limits?What do you mean by continuous? Being able to generate all Floating Point Numbers in that Interval? It doesn't have to. Just an evenly distributed sample of Points. But how would I make it continuous?   ;Quote:Original post by taz0010Some of the RNGs from Boost are included in Tr1, which is included in Visual Studio 2008.http://www.johndcook.com/cpp_TR1_random.htmlThanks. I will try those.   ;  I'd recommend TR1.  Gives you several common distributions, and allows you to instance the random number generators, which is often useful in games.   ;Quote:Original post by hiigaraQuote:Original post by iMalcWere you after something like what swiftcoder posted, or were you wanting a continuous distribution between the two limits?What do you mean by continuous? Being able to generate all Floating Point Numbers in that Interval? It doesn't have to. Just an evenly distributed sample of Points. But how would I make it continuous?One can make it effectively continuous by using a technique that constructs the random float more directly from its exponent and significand parts.   