+ 3
Random and randomize
How does random and randomize work in c++?
1 Antwort
+ 7
To generate a random number we use the rand() function. This will produce a result in the range 0 to RAND_MAX, where RAND_MAX is a constant defined by the implementation.
These are not standard C or C++ functions. In standard C and C++ there are srand and rand which are part of the <stdlib.h> header.
These functions are probably part of Turbo C++ to improve compatibility with Turbo Pascal, where randomize and random are the default random number generator.
As you can't generate a truly random integer with the rand() function alone you have to seed it by including the time library:
#include <time.h>
including the c standard library:
#include <cstdlib>
and then the number generator:
srand(time(NULL));
int random = rand() % 100 + 1;