+ 3
Random and randomize
How does random and randomize work in c++?
1 Answer
+ 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;



