Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
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.
The code below draws 10 different random numbers.
#include <cstdlib> |
As true random number generation is impossible for a device as non-random as a computer, std::rand tries to mimic inpredictability as close as possible. std::rand always generate the same sequence from the same seed. The seed is the starting point of the std::rand sequence. std::srand sets this seed. The code below demonstrates that after the same seed (zero in this case), the first 'randomly drawn' number is always the same.
#include <cstdlib> |
As std::srand and std::rand use a global/static variable and therefore is not suitable for multithreading. Check out the Boost C++ library for other random number generators that do support multithreading.
See GetRandomUniform.
See GetRandomNormal.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.