89: Generate random number in C++

 

// Generate random number in C++

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    int num1;
    srand(time(NULL));
    num1=rand()%6+1;
    cout << num1;
   
    return 0;
}

In num1=rand()%6+1; 

  1. rand() will generate a random number.
  2. %6 is the range upto which you want to generate random numbers.
  3. +1 will let you start number from 1 not from 0.


Comments