[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CRand
std::_lrand is like std::rand, except it returns random numbers is a larger range.
The code below draws 10 different random numbers.
As 'true' random number generation is impossible for a device as non-random as a computer, it uses this algorithm to mimic inpredictability as close as possible. This algorithm will always generate the same sequence from the same ?seed. The ?seed is the starting point of the rand() sequence. The ?srand function 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.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CRand
(C) rand
Function for drawing a random positive integer from zero to ?RAND_MAX. ?RAND_MAX is a ?#defined constant in ?stdlib.std::_lrand is like std::rand, except it returns random numbers is a larger range.
The code below draws 10 different random numbers.
int main() { int i = 0; int randomNumber = 0; for (i=0; i!=10; i++) { randomnumber = rand(); printf(randomNumber); } }
- include <stdlib.h>
As 'true' random number generation is impossible for a device as non-random as a computer, it uses this algorithm to mimic inpredictability as close as possible. This algorithm will always generate the same sequence from the same ?seed. The ?seed is the starting point of the rand() sequence. The ?srand function 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.
int main() { int i = 0; int randomNumber = 0; for (i=0; i!=10; i++) { srand(0); randomnumber = rand(); printf(randomNumber); } }
- include <stdlib.h>
Note when using multithreading
As ?srand and 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.How to: get a broken random number from zero to one?
Knowing that std::rand has ?RAND_MAX as a maximum value, just divide by this value, after casting it to a double.
double uniform()
{
return (double)rand()/(double)RAND_MAX;
}
'Rand' links
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
