forked from orazaro/accelerated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-09-nrand.cpp
More file actions
60 lines (56 loc) · 1.33 KB
/
07-09-nrand.cpp
File metadata and controls
60 lines (56 loc) · 1.33 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
/* 07-09: nrand if n > RAND_MAX
to test we imitate that this case with trandom() */
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <stdexcept>
#include <vector>
#include <numeric>
#include <cmath>
const int TRAND_MAX = 1000;
int trandom()
{
return random() % TRAND_MAX;
}
// return a random integer in the range [0, n)
int nrand(int n)
{
if(n <= 0)
throw std::domain_error("Argument to nrand is out of range");
int result = 0;
if(n > TRAND_MAX) {
int parts = n / TRAND_MAX;
for(int i = 0; i < parts; ++i)
result += trandom();
}
int m = n % TRAND_MAX;
const int bucket_size = TRAND_MAX / m;
int r;
do r = trandom() / bucket_size;
while(r >= m);
return result + r;
}
int main()
{
using namespace std;
const int n_gist = 10;
vector<int> gist(n_gist);
srandom(time(NULL));
for(int i = 0; i < 1000000; ++i)
{
int r = nrand(99999);
gist[r % n_gist]++;
}
// check
double mean = accumulate(gist.begin(), gist.end(), 0.0) / n_gist;
for(int i = 0; i < n_gist; ++i) {
double dx = mean - gist[i];
double dx2 = sqrt(dx * dx);
std::cout << i
<< "\t" << gist[i]
<< "\t" << dx2
<< " (" << sqrt(gist[i]) << ")"
<< std::endl;
}
return 0;
}