In computer programming, a random number is often generated. But it's not that easy for a computer to produce a random number. The execution of a computer is carried out in code, so it is not possible to produce a number that really has a random meaning, like a draw or a dice. It is possible to generate a pseudo-random number with a certain algorithm, and C + + provides a function to be placed in the Cstdlib, called Rand (), with the prototype: int rand (void);
Obviously, this function does not accept any arguments, it does this by producing a random number between [0..rand_max]. Rand_max is also stored in Cstdlib, which is a macro constant:
#define Rand_max ox7fff
Rand_max the largest is, the default is. However, it is not always possible to change the constants every time the range is changed. So this is generally used:
Rand ()%max // This expression generates a random number between [0..max-1]
This function does not produce random numbers in a vacuum, but requires a random number seed. This seed is provided by the Srand function, also stored in Cstdlib, with the prototype: void Srand (unsigned seed);. When you use Rand () but do not produce a seed in advance, the seed value is initialized by default to 1.
C + + random number rand () and seed function Srand ()