-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmrand.cpp
More file actions
151 lines (137 loc) · 3.83 KB
/
Copy pathmrand.cpp
File metadata and controls
151 lines (137 loc) · 3.83 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
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
#include <stdlib.h>
#include <random>
#include <climits>
#include "mrand.h"
mrand_t *mrand_alloc(int type_a,long int seedval){
mrand_t *ret = new mrand_t();
ret->type = type_a;
#ifdef __APPLE__
if(ret->type==0){
fprintf(stderr,"\t-> Problem with drand48 reentrant, will default to erand48\n");
ret->type = 3;
}
#else
if(ret->type==0)
srand48_r(seedval,(struct drand48_data *) &ret->buf0);
#endif
if(ret->type==1){
ret->eng = std::default_random_engine(seedval);
ret->distr = std::uniform_real_distribution<double>(0, 1);
ret->distrInt = std::uniform_int_distribution<>(0,INT_MAX);
}
if(ret->type==2)
ret->rand_r_seed = (unsigned int) seedval;
if(ret->type==3){
ret->rand_r_seed = (unsigned int) seedval;
ret->xsubi[2] = ret->rand_r_seed >> 16;
ret->xsubi[1] = ret->rand_r_seed & 0xffffl;
ret->xsubi[0] = 0x330e;
}
if(ret->type==4){
unsigned long long tmp = -1;
ret->nr_inv_rec = 1/((double) tmp);//(2^64-1)^-1
ret->nr_uvw[1] = 4101842887655102017LL;
ret->nr_uvw[2] = 1LL;
ret->nr_uvw[0] = seedval ^ ret->nr_uvw[1];ret->nr_int64();
ret->nr_uvw[1] = ret->nr_uvw[0];ret->nr_int64();
ret->nr_uvw[2] = ret->nr_uvw[1];ret->nr_int64();
}
return ret;
}
void mrand_destroy(mrand_t *mr) {
delete mr;
}
double mrand_pop(mrand_t *mr){
double res = -1;
if(mr->type==0){
#if defined(__linux__) || defined(__unix__)
drand48_r((struct drand48_data*)&mr->buf0,&res);
#else
fprintf(stderr,"\t-> Error: mr->type==0 is not supported on this platform, will exit\n");
exit(1);
#endif
}
else if(mr->type==1){
res = mr->distr(mr->eng);
}
else if(mr->type==2){
int randr = rand_r(&mr->rand_r_seed);
if (randr == RAND_MAX)
res = (double) (randr-1)/RAND_MAX;
else
res = (double) randr/RAND_MAX;
}
else if(mr->type==3){
res = erand48(mr->xsubi);
}
else if(mr->type==4){
res = mr->nr_inv_rec * mr->nr_int64();
}
else{
fprintf(stderr,"Random parameter %d is not supported\n",mr->type);
exit(1);
}
if (!(res >= 0.0 && res < 1.0)) {
fprintf(stderr, "mrand_pop: value out of range: %.17g (type=%d)\n", res, mr->type);
exit(1);
}
return res;
}
void mrand_seed(mrand_t *ret, long int seedval){
#ifdef __APPLE__
if(ret->type==0){
fprintf(stderr,"\t-> Problem with drand48 reentrant, will default to erand48\n");
ret->type = 3;
}
#else
if(ret->type==0)
srand48_r(seedval,(struct drand48_data *) &ret->buf0);
#endif
if(ret->type==1){
ret->eng.seed(seedval);
}
if(ret->type==2)
ret->rand_r_seed = (unsigned int) seedval;
if(ret->type==3){
ret->rand_r_seed = (unsigned int) seedval;
ret->xsubi[2] = ret->rand_r_seed >> 16;
ret->xsubi[1] = ret->rand_r_seed & 0xffffl;
ret->xsubi[0] = 0x330e;
}
if(ret->type==4){
unsigned long long tmp = -1;
ret->nr_inv_rec = 1/((double) tmp);
ret->nr_uvw[1] = 4101842887655102017LL;
ret->nr_uvw[2] = 1LL;
ret->nr_uvw[0] = seedval ^ ret->nr_uvw[1]; ret->nr_int64();
ret->nr_uvw[1] = ret->nr_uvw[0]; ret->nr_int64();
ret->nr_uvw[2] = ret->nr_uvw[1]; ret->nr_int64();
}
}
#ifdef __WITH_MAIN__
int main(int argc,char **argv){
mrand_t *myrand;
int type =0;
int seed =1;
long long nitems =10;
double dogeom = 0.0;
if(argc>3){
type = atoi(argv[1]);
seed = atoi(argv[2]);
nitems = atoll(argv[3]);
}
if(argc>4)
dogeom = atof(argv[4]);
fprintf(stderr,"type: %d seed: %d nitems: %lld dogeom: %f\n",type,seed,nitems,dogeom);
myrand = mrand_alloc(type,seed);
if(dogeom==0){
double sum = 0;
for(long long i=0;i<nitems;i++){
sum += mrand_pop(myrand);
}
fprintf(stdout,"type %d\tseed: %d\tsum:%f\tnitems in mio:%f mean: %f\n",type,seed,sum,nitems/1e6,sum/((double)nitems));
}
return 0;
}
#endif
//g++ mrand.cpp -std=c++11 -lm -lz -D__WITH_MAIN__ -o Rand