-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomRange.cpp
43 lines (31 loc) · 886 Bytes
/
randomRange.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
srand(time(0));
int lower,upper,randomNo;
cout<<"Enter lower limit ";
cin>>lower;
cout<<"Enter upper limit ";
cin>>upper;
randomNo = (rand() % (upper-lower+1)) + lower;
cout<<"Random no: "<<randomNo;
return 0;
}
/*
Assume we need a no. between 10 and 20:
upper-lower+1 = 11 ...(1)
any number generated by rand() when modulus with (1) will be between
0-10 or 0-[(1)-1]
and when lower is added to the final result we get a value between
the desired range.
*/
/********************************************************
Title: Random numbers between a given range
Author: CAC
Date: 12th April 2021
Description:
This code was implemented on day 15 of 100 days of code
*********************************************************/