-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyrand.cpp
executable file
·137 lines (103 loc) · 3.2 KB
/
myrand.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
/***************************************************************************
myrand.cc - description
-------------------
begin : Sep 24 2001
copyright : (C) 2001 by Tian-Li Yu
email : tianliyu@illigal.ge.uiuc.edu
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include "myrand.h"
#ifdef PI
#undef PI
#endif
#define PI 3.14159265
#define N 624
MyRand::MyRand() {
unsigned long init_key[N];
srand((unsigned long) time(NULL));
for (int i = 0; i < N; i++) {
init_key[i] = (unsigned long) time(NULL) * rand();
}
init_by_array(init_key, N);
}
MyRand::~MyRand() {
}
void MyRand::seed(unsigned long seed) {
init_genrand(seed);
}
bool MyRand::flip() {
static unsigned long mask = (1ul << 31);
static unsigned long x = genrand_int32();
bool result = ((mask & x) == 0);
mask >>= 1;
if (mask == 0) {
mask = (1ul << 31);
x = genrand_int32();
}
return result;
}
bool MyRand::flip(double prob) {
return (uniform() < prob);
}
/** From [0,1] */
double MyRand::uniform() {
return genrand_real1();
}
/** From [a,b] */
double MyRand::uniform(double a, double b) {
return uniform() * (b - a) + a;
}
/** Standard normal distribution */
double MyRand::normal() {
double u1, u2, z;
u1 = genrand_real3(); // (0,1)
u2 = uniform();
z = sqrt(-2 * log(u1)) * sin(2 * PI * u2);
return z;
}
/** Normal distribution with mean and standard deviation */
double MyRand::normal(double mean, double stdd) {
return normal() * stdd + mean;
}
int MyRand::uniformInt(int a, int b) {
return (a + (int) (genrand_real2() * (b - a + 1)));
}
void MyRand::uniformArray(int *array, int num, int a, int b) {
int *base = new int[b - a + 1];
int i;
int r;
for (i = 0; i < b - a + 1; i++)
base[i] = a + i;
for (i = 0; i < num; i++) {
r = uniformInt(0, b - a - i);
array[i] = base[r];
base[r] = base[b - a - i];
}
delete[] base;
}
int MyRand::dice(double *pr, int size, double prSum) {
// if we don't know prSum
if (prSum < 0.0) {
prSum = 0.0;
for (int i = 0; i < size; i++)
prSum += pr[i];
}
double x = uniform(0, prSum);
double partial = 0.0;
for (int i = 0; i < size; i++) {
if (partial > x)
return i;
partial += pr[i];
}
return size - 1;
}