How do I generate random floats in C++?
I thought I could take the integer rand and divide it by something, would that be adequate enough?
How do I generate random floats in C++?
I thought I could take the integer rand and divide it by something, would that be adequate enough?
Call the code with two
float
values, the code works in any range.rand() return a int between 0 and RAND_MAX. To get a random number between 0.0 and 1.0, first cast the int return by rand() to a float, then divide by RAND_MAX.
If you know that your floating point format is IEEE 754 (almost all modern CPUs including Intel and ARM) then you can build a random floating point number from a random integer using bit-wise methods. This should only be considered if you do not have access to C++11's
random
orBoost.Random
which are both much better.This will give a better distribution than one using division.
If you are using C++ and not C, then remember that in technical report 1 (TR1) and in the C++0x draft they have added facilities for a random number generator in the header file, I believe it is identical to the Boost.Random library and definitely more flexible and "modern" than the C library function, rand.
This syntax offers the ability to choose a generator (like the mersenne twister mt19937) and then choose a distribution (normal, bernoulli, binomial etc.).
Syntax is as follows (shameless borrowed from this site):
On some systems (Windows with VC springs to mind, currently),
RAND_MAX
is ridiculously small, i. e. only 15 bit. When dividing byRAND_MAX
you are only generating a mantissa of 15 bit instead of the 23 possible bits. This may or may not be a problem for you, but you're missing out some values in that case.Oh, just noticed that there was already a comment for that problem. Anyway, here's some code that might solve this for you:
Untested, but might work :-)
C++11 gives you a lot of new options with
random
. The canonical paper on this topic would be N3551, Random Number Generation in C++11To see why using
rand()
can be problematic see the rand() Considered Harmful presentation material by Stephan T. Lavavej given during the GoingNative 2013 event. The slides are in the comments but here is a direct link.I also cover
boost
as well as usingrand
since legacy code may still require its support.The example below is distilled from the cppreference site and uses the std::mersenne_twister_engine engine and the std::uniform_real_distribution which generates numbers in the
[0,10)
interval, with other engines and distributions commented out (see it live):output will be similar to the following:
The output will vary depending on which distribution you choose, so if we decided to go with std::normal_distribution with a value of
2
for both mean and stddev e.g.dist(2, 2)
instead the output would be similar to this (see it live):The following is a modified version of some of the code presented in
N3551
(see it live) :Results will look similar to:
Boost
Of course Boost.Random is always an option as well, here I am using boost::random::uniform_real_distribution:
rand()
If you must use
rand()
then we can go to the C FAQ for a guides on How can I generate floating-point random numbers? , which basically gives an example similar to this for generating an on the interval[0,1)
:and to generate a random number in the range from
[M,N)
: