105k views
5 votes
Write a c++ program that generates three random numbers. The first number should be between 0-30, the second number should be between 30-60 and the third number should be between 60-90.

User Zaartha
by
5.5k points

1 Answer

5 votes

Answer:

#include <stdlib.h>

#include <time.h>

#include <iostream>

using namespace std;

int main() {

srand(time(NULL));

cout << rand() % 30 << endl;

cout << rand() % 30 + 30 << endl;

cout << rand() % 30 + 60 << endl;

}

Step-by-step explanation:

rand() returns a pseudo-random integral number in the range between 0 and RAND_MAX. By applying the modulo operator, you map that to the desired range (30 in your case), then you add an offset.

User Shaquia
by
4.9k points