the definition of rand() & srand()
rand(): generate random numbers in the range [0, RAND_MAX).
srand(): Set Seed for rand() Function. Standard practice is to use the result of a call to srand(time (0)) as the seed.
Notes: If random numbers are generated with rand() without first calling srand(), your program will create the same sequence of numbers each time it runs.
how to generate a random number in the fixed range
int a = rand() % 10; //[0,9]
int b = rand() % 10 + 5; //[5,14]
int c = rand() % (upper-lower+1) + lower; //[lower,upper]
Top comments (0)