Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::rand

 

std::rand draws a random positive integer from zero to RAND_MAX. RAND_MAX is a #defined in cstdlib.h.

 

std::rand is defined in the header file cstdlib.h.

 

std::_lrand is like std::rand, except it returns random numbers in a larger range. Check out the Boost C++ library for other random number generators.

 

 

 

 

 

Examples

 

 

 

 

 

 

Advice

 

 

 

 

 

 

Note when using multithreading

 

As std::srand and std::rand use a global/static variable and therefore is not suitable for multithreading. The Boost C++ library has other random number generators that do support multithreading.

 

 

 

 

 

External links

 

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. 40.8[15]: 'Prefer a random number class for a particular distribution over direct use of rand()'
  2. Stephan T. Lavavej. 'rand() Considered Harmful'. 2013. GoingNative 2013
  3. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. 40.7.4: 'Producing a random number generator isn't easy, and unfortunately not all systems deliver a good rand()'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict