-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomexamples.cpp
More file actions
66 lines (55 loc) · 1.99 KB
/
Copy pathrandomexamples.cpp
File metadata and controls
66 lines (55 loc) · 1.99 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// Without srand(), the program will create same sequence of
// random numbers on every program run
//srand(1);
// Use current time as seed for random generator
// then create different sequence of
// random numbers on every program run
srand(time(0));
//// generate random numbers from 0 to RAND_MAX: [0-32767]
for(int i = 0; i<5; i++){
cout << rand() << " ";
}
//// generate random numbers from 1 to 10: [1,10]
//// [a,b]: rand() % (b-a+1) + a
// cout<<"Random numbers generated between [1,10]:"<<endl;
// for(int i=0;i<10;i++){
// cout << (rand() % 10) + 1 << " ";
// }
//// generate random numbers from 1 to 10: [1,10)
//// [a,b): rand() % (b-a) + a
// cout<<"Random numbers generated between [1,10):"<<endl;
// for(int i=0;i<10;i++){
// cout << (rand() % 9) + 1 << " ";
// }
//// generate random numbers from 1 to 10: (1,10]
//// (a,b]: rand() % (b-a) + (a+1)
// cout<<"Random numbers generated between (1,10]:"<<endl;
// for(int i=0;i<10;i++){
// cout << (rand() % 9) + 2 << " ";
// }
//// generate random numbers from 1 to 10: (1,10)
//// (a,b): rand() % (b-a-1) + (a+1)
// cout<<"Random numbers generated between (1,10):"<<endl;
// for(int i=0;i<10;i++){
// cout << (rand() % 8) + 2 << " ";
// }
//// generate float random numbers from 0 to 1: [0,1]
// cout<<"Random numbers generated between 0 and 1:"<<endl;
// for (int i = 0; i < 5; i++) {
// cout << (float) rand()/RAND_MAX << endl;
// }
//// generate float random numbers from 15 to 20: [15,20]
//// Float [a,b]: ((b - a) * ((float)rand() / RAND_MAX)) + a
// cout<<"Random numbers generated between 0 and 1:"<<endl;
// for (int i = 0; i < 5; i++) {
// cout << ((5 * (float) rand()/RAND_MAX) + 15) << endl;
// }
cout << endl;
return 0;
}