-
Notifications
You must be signed in to change notification settings - Fork 13
/
randfast.c
117 lines (95 loc) · 2.19 KB
/
randfast.c
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
/*
Ordo is program for calculating ratings of engine or chess players
Copyright 2013 Miguel A. Ballicora
This file is part of Ordo.
Ordo 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 3 of the License, or
(at your option) any later version.
Ordo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ordo. If not, see <http://www.gnu.org/licenses/>.
*/
#include "randfast.h"
#include "datatype.h"
/*
|
| Random number generator taken from
| http://www.burtleburtle.net/bob/rand/talksmall.html
|
*/
typedef struct ranctx {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
} ranctx;
static ranctx Rndseries;
#define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
static uint32_t
ranval( ranctx *x )
{
uint32_t e = x->a - rot(x->b, 27);
x->a = x->b ^ rot(x->c, 17);
x->b = x->c + x->d;
x->c = x->d + e;
x->d = e + x->a;
return x->d;
}
static void raninit ( ranctx *x, uint32_t seed )
{
uint32_t i;
x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
for (i = 0; i < 20; ++i) {
(void)ranval(x);
}
}
void randfast_init (uint32_t seed)
{
raninit (&Rndseries, seed);
}
uint32_t randfast32 (void)
{
return ranval (&Rndseries);
}
//==========================================
#include "gauss.h"
static double
rand_area (void)
{
uint32_t r;
double rr;
do {
r = randfast32();
} while (r == 0);
r &= 8191;
rr = (double) r;
rr = rr/8192;
return rr;
}
static double
rand_gauss_normalized(void)
{
double xi, yi, area, slope;
double limit = 0.00001;
int n;
area = rand_area();
n = 0;
xi = 0;
do {
yi = gauss_integral (xi) - area;
slope = gauss_density (xi);
xi = xi - yi / slope;
n++;
} while (n < 100 && (yi > limit || yi < -limit));
return xi;
}
double
rand_gauss(double x, double s)
{
double z = rand_gauss_normalized();
return x + z * s;
}