-
Notifications
You must be signed in to change notification settings - Fork 2
/
randomTest.cpp
155 lines (132 loc) · 4.67 KB
/
randomTest.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
/*
streflop: STandalone REproducible FLOating-Point
Nicolas Brodu, 2006
Code released according to the GNU Lesser General Public License
Heavily relies on GNU Libm, itself depending on netlib fplibm, GNU MP, and IBM MP lib.
Uses SoftFloat too.
Please read the history and copyright information in the documentation provided with the source code
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// clock
#include <time.h>
#include "streflop.h"
using namespace streflop;
template<typename F> void checkNRandom() {
streflop_init<F>();
F mean = 0.0;
F var = 0.0;
int N = 1000000;
for (int i=0; i<N; ++i) {
// mean, var
F value = NRandom<F>() * 78.9 + 345.6;
mean += value;
var += value * value;
}
mean /= N;
var = sqrt(var/N - mean*mean);
cout << "meanN (should be 345.6): " << mean << endl;
cout << "varN (should be 78.9): " << var << endl;
}
template<bool IEmin, bool IEmax, typename F> void checkRandom() {
F mean = 0.0;
F var = 0.0;
int N = 1000000;
for (int i=0; i<N; ++i) {
// mean, var
F value = Random<IEmin, IEmax, F>(100.0,700.0);
mean += value;
var += value * value;
}
mean /= N;
var = sqrt(var/N - mean*mean);
cout << "mean<"<<IEmin<<","<<IEmax<<"> (should be 400): " << mean << endl;
cout << "var<"<<IEmin<<","<<IEmax<<"> = " << var << endl;
}
template<typename FloatType> void showrate( clock_t start, clock_t stop, int reps )
{
FloatType time = FloatType( stop - start ) / CLOCKS_PER_SEC;
FloatType rate = reps / time;
cout << (double)rate << " million per second" << endl;
}
// inspired from Richard J. Wagner Mersenne class timing test
template<typename FloatType> void randomTimings() {
streflop_init<FloatType>();
cout << "Test of generation rates in various distributions:" <<endl;
clock_t start, stop;
cout << " Integers in [0,2^32-1] ";
start = clock();
for(int i = 0; i < 50000000; ++i ) Random<SizedUnsignedInteger<32>::Type>();
stop = clock();
showrate<FloatType>(start,stop,50);
cout << " Integers in [0,100] ";
start = clock();
for(int i = 0; i < 50000000; ++i ) Random<true, true, SizedUnsignedInteger<32>::Type>(0,100);
stop = clock();
showrate<FloatType>(start,stop,50);
cout << " Reals in [1,2) ";
start = clock();
for(int i = 0; i < 50000000; ++i ) Random12<true, false, FloatType>();
stop = clock();
showrate<FloatType>(start,stop,50);
cout << " Reals in [0,1) ";
start = clock();
for(int i = 0; i < 50000000; ++i ) Random01<true, false, FloatType>();
stop = clock();
showrate<FloatType>(start,stop,50);
cout << " Reals in [0,7) ";
start = clock();
for(int i = 0; i < 50000000; ++i ) Random<true, false, FloatType>(0.0, 7.0);
stop = clock();
showrate<FloatType>(start,stop,50);
cout << " Reals in [1,2] ";
start = clock();
for(int i = 0; i < 50000000; ++i ) Random12<true, true, FloatType>();
stop = clock();
showrate<FloatType>(start,stop,50);
cout << " Reals in (1,2) ";
start = clock();
for(int i = 0; i < 50000000; ++i ) Random12<false, false, FloatType>();
stop = clock();
showrate<FloatType>(start,stop,50);
cout << " Reals in normal distribution ";
start = clock();
FloatType secondary;
for(int i = 0; i < 10000000; ++i ) NRandom<FloatType>(2.0, 7.0, &secondary);
stop = clock();
showrate<FloatType>(start,stop,20);
}
int main(int argc, const char** argv) {
cout << "Random seed: " << RandomInit() << endl;
cout << "Checking Simple ranges" << endl;
checkNRandom<Simple>();
checkRandom<true, true, Simple>();
checkRandom<true, false, Simple>();
checkRandom<false, true, Simple>();
checkRandom<false, false, Simple>();
cout << "Checking Double ranges" << endl;
checkNRandom<Double>();
checkRandom<true, true, Double>();
checkRandom<true, false, Double>();
checkRandom<false, true, Double>();
checkRandom<false, false, Double>();
#if defined(Extended)
cout << "Checking Extended ranges" << endl;
checkNRandom<Extended>();
checkRandom<true, true, Extended>();
checkRandom<true, false, Extended>();
checkRandom<false, true, Extended>();
checkRandom<false, false, Extended>();
#endif
cout << "Checking Simple timings" << endl;
randomTimings<Simple>();
cout << "Checking Double timings" << endl;
randomTimings<Double>();
#if defined(Extended)
cout << "Checking Extended timings" << endl;
randomTimings<Extended>();
#endif
return 0;
}