-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ramdom Number Generator.cpp
162 lines (129 loc) · 3.11 KB
/
Ramdom Number Generator.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
// Function to generate random numbers
void multiplicativeCongruentialMethod(
int Xo, int m, int a,
vector<int>& randomNums,
int noOfRandomNums)
{
// Initialize the seed state
randomNums[0] = Xo;
// Traverse to generate required
// numbers of random numbers
for (int i = 1; i < noOfRandomNums; i++) {
// Follow the multiplicative
// congruential method
randomNums[i]
= (randomNums[i - 1] * a) % m;
}
}
// Driver Code
int main()
{
int Xo = 3; // seed value
int m = 15; // modulus parameter
int a = 7; // multiplier term
// Number of Random numbers
// to be generated
int noOfRandomNums = 10;
// To store random numbers
vector<int> randomNums(noOfRandomNums);
// Function Call
multiplicativeCongruentialMethod(
Xo, m, a, randomNums,
noOfRandomNums);
// Print the generated random numbers
for (int i = 0; i < noOfRandomNums; i++) {
cout << randomNums[i] << " ";
}
return 0;
}
// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
// Function to generate random numbers
void additiveCongruentialMethod(
int Xo, int m, int c,
vector<int>& randomNums,
int noOfRandomNums)
{
// Initialize the seed state
randomNums[0] = Xo;
// Traverse to generate required
// numbers of random numbers
for (int i = 1; i < noOfRandomNums; i++) {
// Follow the additive
// congruential method
randomNums[i]
= (randomNums[i - 1] + c)
% m;
}
}
// Driver Code
int main()
{
int Xo = 3; // seed value
int m = 15; // modulus parameter
int c = 2; // increment term
// Number of Random numbers
// to be generated
int noOfRandomNums = 20;
// To store random numbers
vector<int> randomNums(noOfRandomNums);
// Function Call
additiveCongruentialMethod(
Xo, m, c,
randomNums,
noOfRandomNums);
// Print the generated random numbers
for (int i = 0; i < noOfRandomNums; i++) {
cout << randomNums[i] << " ";
}
return 0;
}
// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
// Function to generate random numbers
void linearCongruentialMethod(
int Xo, int m, int a, int c,
vector<int>& randomNums,
int noOfRandomNums)
{
// Initialize the seed state
randomNums[0] = Xo;
// Traverse to generate required
// numbers of random numbers
for (int i = 1; i < noOfRandomNums; i++) {
// Follow the linear congruential method
randomNums[i]
= ((randomNums[i - 1] * a) + c) % m;
}
}
// Driver Code
int main()
{
int Xo = 5; // Seed value
int m = 7; // Modulus parameter
int a = 3; // Multiplier term
int c = 3; // Increment term
// Number of Random numbers
// to be generated
int noOfRandomNums = 10;
// To store random numbers
vector<int> randomNums(
noOfRandomNums);
// Function Call
linearCongruentialMethod(
Xo, m, a, c,
randomNums, noOfRandomNums);
// Print the generated random numbers
for (int i = 0; i < noOfRandomNums; i++) {
cout << randomNums[i] << " ";
}
return 0;
}