Random numbers

Generating a random number in C/C++

Last Update Unknown

Required header files

  • cstdlib provides the srand and rand functions.
  • ctime provides the time function.

Random number from 0 to 6:

Random number from 1 to 7:

srand(time(NULL)) initialises a random seed for the random function. 

It should be called every time before running the rand function.