-
Notifications
You must be signed in to change notification settings - Fork 266
/
random.cpp
184 lines (155 loc) · 3.95 KB
/
random.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Implementation file for random number generation.
*
* Part of the Cyclone physics system.
*
* Copyright (c) Icosagon 2003. All Rights Reserved.
*
* This software is distributed under licence. Use of this software
* implies agreement with all terms and conditions of the accompanying
* software licence.
*/
#include <cstdlib>
#include <ctime>
#include <cyclone/random.h>
using namespace cyclone;
Random::Random()
{
seed(0);
}
Random::Random(unsigned seed)
{
Random::seed(seed);
}
void Random::seed(unsigned s)
{
if (s == 0) {
s = (unsigned)clock();
}
// Fill the buffer with some basic random numbers
for (unsigned i = 0; i < 17; i++)
{
// Simple linear congruential generator
s = s * 2891336453 + 1;
buffer[i] = s;
}
// Initialize pointers into the buffer
p1 = 0; p2 = 10;
}
unsigned Random::rotl(unsigned n, unsigned r)
{
return (n << r) |
(n >> (32 - r));
}
unsigned Random::rotr(unsigned n, unsigned r)
{
return (n >> r) |
(n << (32 - r));
}
unsigned Random::randomBits()
{
unsigned result;
// Rotate the buffer and store it back to itself
result = buffer[p1] = rotl(buffer[p2], 13) + rotl(buffer[p1], 9);
// Rotate pointers
if (--p1 < 0) p1 = 16;
if (--p2 < 0) p2 = 16;
// Return result
return result;
}
#ifdef SINGLE_PRECISION
real Random::randomReal()
{
// Get the random number
unsigned bits = randomBits();
// Set up a reinterpret structure for manipulation
union {
real value;
unsigned word;
} convert;
// Now assign the bits to the word. This works by fixing the ieee
// sign and exponent bits (so that the size of the result is 1-2)
// and using the bits to create the fraction part of the float.
convert.word = (bits >> 9) | 0x3f800000;
// And return the value
return convert.value - 1.0f;
}
#else
real Random::randomReal()
{
// Get the random number
unsigned bits = randomBits();
// Set up a reinterpret structure for manipulation
union {
real value;
unsigned words[2];
} convert;
// Now assign the bits to the words. This works by fixing the ieee
// sign and exponent bits (so that the size of the result is 1-2)
// and using the bits to create the fraction part of the float. Note
// that bits are used more than once in this process.
convert.words[0] = bits << 20; // Fill in the top 16 bits
convert.words[1] = (bits >> 12) | 0x3FF00000; // And the bottom 20
// And return the value
return convert.value - 1.0;
}
#endif
real Random::randomReal(real min, real max)
{
return randomReal() * (max-min) + min;
}
real Random::randomReal(real scale)
{
return randomReal() * scale;
}
unsigned Random::randomInt(unsigned max)
{
return randomBits() % max;
}
real Random::randomBinomial(real scale)
{
return (randomReal()-randomReal())*scale;
}
Quaternion Random::randomQuaternion()
{
Quaternion q(
randomReal(),
randomReal(),
randomReal(),
randomReal()
);
q.normalise();
return q;
}
Vector3 Random::randomVector(real scale)
{
return Vector3(
randomBinomial(scale),
randomBinomial(scale),
randomBinomial(scale)
);
}
Vector3 Random::randomXZVector(real scale)
{
return Vector3(
randomBinomial(scale),
0,
randomBinomial(scale)
);
}
Vector3 Random::randomVector(const Vector3 &scale)
{
return Vector3(
randomBinomial(scale.x),
randomBinomial(scale.y),
randomBinomial(scale.z)
);
}
Vector3 Random::randomVector(const Vector3 &min, const Vector3 &max)
{
return Vector3(
randomReal(min.x, max.x),
randomReal(min.y, max.y),
randomReal(min.z, max.z)
);
}