Jump to content

Differences between C++ random number genertators

Dobbsjr

They're just from different libraries. iirc, std::default_random_engine is the more up to date one to use

Link to comment
Share on other sites

Link to post
Share on other sites

Acutally (almost) all random number generators are what we call pseudo-random. They work by setting a seed. Then do some calculations on that and return a number, the next time to generate a number, is will use the last generated number as a seed.

 

rand() is usually considered bad, since the distribution is not what we call 'uniform'. A uniform distribution is where all numbers have an equal chance to get generated.

 

C++ offers the <random> header file, containing better random number generators, like std::default_random_engine.

If you want to be able to recreate the same values with the same seed everytime, it is recommended that you don't use the std::default_random_engine, but a different one, or that you write your own.

If you need to generate a truly random number (not pseudo), there are ways to do that. x86 offers and extension that allows you to read a truly random number generated on the CPU (it had something to with crystals and stuff iirc). To use this include <immintrin.h> and call _rdrand16_step, _rdrand32_step, or _rdrand64_step. Note that these functions are very slow. The intel intrinsics guide says that it takes about 200 cycles on Ivy Bridge, and doesn't specify a cycle count on Haswell.

This video has a great explanation on how pseudo random number generators work: 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×