C++ Programming/Code/Standard C Library/Functions/rand
Appearance
rand
[edit | edit source]Syntax |
#include <cstdlib>
int rand( void );
|
The function rand() returns a pseudo-random integer between zero and RAND_MAX. An example:
srand( time(NULL) );
for( i = 0; i < 10; i++ )
printf( "Random number #%d: %d\n", i, rand() );
The rand() function must be seeded before its first call with the srand() function - otherwise it will consistently return the same numbers when the program is restarted.
- Related topics
- srand