This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
166 lines (154 loc) · 4.77 KB
/
main.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
162
163
164
165
166
#include <iostream>
#include "linear.h"
#include "quadratic.h"
#include "fibonacci.h"
#include "reversed.h"
#include "uniting.h"
#include "sigma.h"
#include "polar.h"
#include "ratio.h"
#include "logar.h"
#include "arens.h"
const int NUMBERS_TO_GEN = 1002;
const int COUNTERS = 10;
const int HISTOGRAM_HAS_NEGATIVE = 1;
const int HISTOGRAM_HAS_NOT_NEGATIVE = 0;
const int HISTOGRAM_RANGE[] = {1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1};
void method_prompt() {
/*
* Shows the list of the methods
*/
std::cout << "Select a method for generating quasirandom numbers:\n";
std::cout << " 1. Linear congruential method.\n";
std::cout << " 2. Quadratic congruential method.\n";
std::cout << " 3. Fibonacci numbers.\n";
std::cout << " 4. Reversed congruential method.\n";
std::cout << " 5. Uniting method.\n";
std::cout << " 6. 3-sigma.\n";
std::cout << " 7. Polar coordinates method.\n";
std::cout << " 8. Ratio method.\n";
std::cout << " 9. Logarithmic method.\n";
std::cout << "10. Gamma distribution method.\n";
std::cout << "\nInput the number of the method:\n";
}
int input() {
/*
* Returns the number of the method from cin
*/
int method_number;
std::cin >> method_number;
return method_number;
}
void unimplemented(int method_number) {
/*
* Says that user asked for an unimplemented method
*/
std::cout << "Method " << method_number << " is unimplemented!";
}
void print_with_precision(float n, float precision) {
/*
* Prints float with given precision
*
* CAUTION!
* Sets this precision for the next couts!
*/
std::cout.setf(std::ios::fixed);
std::cout.precision(precision);
std::cout << n;
}
int main() {
int method_number, i, histogram_period_precision, histogram_negative, histogram_numbers = NUMBERS_TO_GEN;
float histogram_period, generated_number;
int histogram_counters[COUNTERS];
for (i = 0; i < COUNTERS; i++) {
histogram_counters[i] = 0;
}
// select method
method_prompt();
method_number = input();
// setup histogram params
switch (HISTOGRAM_RANGE[method_number]) {
case 1: {
histogram_negative = HISTOGRAM_HAS_NOT_NEGATIVE;
histogram_period_precision = 1;
break;
}
case 3: {
histogram_negative = HISTOGRAM_HAS_NEGATIVE;
histogram_period_precision = 1;
break;
}
}
histogram_period = (float)HISTOGRAM_RANGE[method_number] / COUNTERS;
if (histogram_negative != 0) histogram_period *= 2 * histogram_negative;
for (i = 0; i < NUMBERS_TO_GEN; i++) {
// generate number
switch (method_number) {
case 1: {
generated_number = linear();
break;
}
case 2: {
generated_number = quadratic();
break;
}
case 3: {
generated_number = fibonacci();
break;
}
case 4: {
generated_number = reversed();
break;
}
case 5: {
generated_number = uniting();
break;
}
case 6: {
generated_number = sigma();
break;
}
case 7: {
generated_number = polar();
break;
}
case 8: {
generated_number = ratio();
break;
}
case 9: {
generated_number = logar();
break;
}
case 10: {
generated_number = arens();
break;
}
default: {
unimplemented(method_number);
exit(0);
}
}
// count number in the histogram
if (histogram_negative == HISTOGRAM_HAS_NEGATIVE) {
generated_number += HISTOGRAM_RANGE[method_number];
}
if (0 <= generated_number && generated_number <= histogram_period * COUNTERS) {
histogram_counters[(int) (generated_number / histogram_period)]++;
} else {
histogram_numbers--;
}
}
// show histogram
std::cout << "\n";
for (i = 0; i < COUNTERS; i++) {
std::cout << '[';
print_with_precision(histogram_period * i - (histogram_negative * HISTOGRAM_RANGE[method_number]), histogram_period_precision);
std::cout << ";\t";
print_with_precision(histogram_period * (i + 1) - (histogram_negative * HISTOGRAM_RANGE[method_number]), histogram_period_precision);
std::cout << "]\t";
print_with_precision((float)histogram_counters[i] / histogram_numbers, 2);
std::cout << "\n";
}
return 0;
}