C/C++ C++ Random Function: Generate Uniformly Distributed Numbers

AI Thread Summary
The discussion centers on generating uniformly distributed random numbers in C++ for large ranges, specifically when using the standard rand() function, which is inadequate for such tasks due to its limited range and poor distribution properties. Users suggest that instead of relying on rand(), which generates numbers from 0 to MAX_RAND (typically 32767), one should consider more advanced pseudorandom number generators. Recommendations include dividing the desired range into smaller zones and using rand() to select a zone, but this approach has limitations due to potential correlations in consecutive numbers. For better results, the Mersenne Twister generator is highlighted as a superior alternative, along with the boost::random library, which offers robust and accurate random number generation. While using these libraries may require some initial learning, they provide a more reliable solution for serious applications.
omri3012
Messages
60
Reaction score
0
Hallo,

I'm looking for a Random function in c++. i tried to use in the function rand()% but
it does not generate a truly uniformly distributed random number in the span (since my span is large. 100000*100000). in other word it dosent generate a random number in equal probabilty in lage dimentions.
if you know a function or a way that i could generate random number in large span it would
be very helpful.

Thanks,

Omri
 
Technology news on Phys.org
That's because rand() generates numbers from 0..MAX_RAND range (and I think MAX_RAND is by default 32767). I remember seeing libraries for better pseudorandom number generators, shouldn't be difficult to google.
 
Last edited:
you could divide your range into 32767 zones and use rand() first to get a zone, then again to get a number within the zone. etc.
 
That may not work correctly for pseudorandom numbers, as bits in two consecutive numbers can be correlated.
 
harborsparrow said:
you could divide your range into 32767 zones and use rand() first to get a zone, then again to get a number within the zone. etc.
Which is roughly equivalent to calling rand() twice, multiplying the first one by RAND_MAX and then adding them.

If you need super random numbers you might want to look at a better rand library such as boost rand
Otherwise calling rand twice and shift+add might be ok
 
The standard rand() function is really bad and you'll run into problems very quickly (even for non-scientific applications like games).

An easy solution is to use a Mersenne twister generator instead. (There are some ready to use implementations linked at the bottom of the wiki page).
 
rand() is terrible and should not be used for anything even halfway serious. If you need a robust, fast, and accurate C++ random number generator you don't need to look any further than boost::random. You'll need to spend half an hour reading the docs in order to use it, but once you do it's a snap.
 

Similar threads

Back
Top