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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
// File: crn_rand.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
// See:
// http://www.ciphersbyritter.com/NEWS4/RANDC.HTM
// http://burtleburtle.net/bob/rand/smallprng.html
// http://www.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
// See GPG7, page 120, or https://www.lomont.org/papers/2008/Lomont_PRNG_2008.pdf
#include "crn_core.h"
#include "crn_rand.h"
#include "crn_hash.h"
#define znew (z = 36969 * (z & 65535) + (z >> 16))
#define wnew (w = 18000 * (w & 65535) + (w >> 16))
#define MWC ((znew << 16) + wnew)
#define SHR3 (jsr ^= (jsr << 17), jsr ^= (jsr >> 13), jsr ^= (jsr << 5))
#define CONG (jcong = 69069 * jcong + 1234567)
#define FIB ((b = a + b), (a = b - a))
#define KISS ((MWC ^ CONG) + SHR3)
#define LFIB4 (c++, t[c] = t[c] + t[UC(c + 58)] + t[UC(c + 119)] + t[UC(c + 178)])
#define SWB (c++, bro = (x < y), t[c] = (x = t[UC(c + 34)]) - (y = t[UC(c + 19)] + bro))
#define UNI (KISS * 2.328306e-10)
#define VNI ((long)KISS) * 4.656613e-10
#define UC (unsigned char)/*a cast operation*/
//#define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
#define rot(x, k) CRNLIB_ROTATE_LEFT(x, k)
namespace crnlib {
static const double cNorm = 1.0 / (double)0x100000000ULL;
kiss99::kiss99() {
x = 123456789;
y = 362436000;
z = 521288629;
c = 7654321;
}
void kiss99::seed(uint32 i, uint32 j, uint32 k) {
x = i;
y = j;
z = k;
c = 7654321;
}
inline uint32 kiss99::next() {
x = 69069 * x + 12345;
y ^= (y << 13);
y ^= (y >> 17);
y ^= (y << 5);
uint64 t = c;
t += (698769069ULL * z);
c = static_cast<uint32>(t >> 32);
z = static_cast<uint32>(t);
return (x + y + z);
}
inline uint32 ranctx::next() {
uint32 e = a - rot(b, 27);
a = b ^ rot(c, 17);
b = c + d;
c = d + e;
d = e + a;
return d;
}
void ranctx::seed(uint32 seed) {
a = 0xf1ea5eed, b = c = d = seed;
for (uint32 i = 0; i < 20; ++i)
next();
}
well512::well512() {
seed(0xDEADBE3F);
}
void well512::seed(uint32 seed[well512::cStateSize]) {
memcpy(m_state, seed, sizeof(m_state));
m_index = 0;
}
void well512::seed(uint32 seed) {
uint32 jsr = utils::swap32(seed) ^ 0xAAC29377;
for (uint i = 0; i < cStateSize; i++) {
SHR3;
seed = bitmix32c(seed);
m_state[i] = seed ^ jsr;
}
m_index = 0;
}
void well512::seed(uint32 seed1, uint32 seed2, uint32 seed3) {
uint32 jsr = seed2;
uint32 jcong = seed3;
for (uint i = 0; i < cStateSize; i++) {
SHR3;
seed1 = bitmix32c(seed1);
CONG;
m_state[i] = seed1 ^ jsr ^ jcong;
}
m_index = 0;
}
inline uint32 well512::next() {
uint32 a, b, c, d;
a = m_state[m_index];
c = m_state[(m_index + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = m_state[(m_index + 9) & 15];
c ^= (c >> 11);
a = m_state[m_index] = b ^ c;
d = a ^ ((a << 5) & 0xDA442D20UL);
m_index = (m_index + 15) & 15;
a = m_state[m_index];
m_state[m_index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
return m_state[m_index];
}
random::random() {
seed(12345, 65435, 34221);
}
random::random(uint32 i) {
seed(i);
}
void random::seed(uint32 i1, uint32 i2, uint32 i3) {
m_ranctx.seed(i1 ^ i2 ^ i3);
m_kiss99.seed(i1, i2, i3);
m_well512.seed(i1, i2, i3);
for (uint i = 0; i < 100; i++)
urand32();
}
void random::seed(uint32 i) {
uint32 jsr = i;
SHR3;
SHR3;
uint32 jcong = utils::swap32(~jsr);
CONG;
CONG;
uint32 i1 = SHR3 ^ CONG;
uint32 i2 = SHR3 ^ CONG;
uint32 i3 = SHR3 + CONG;
seed(i1, i2, i3);
}
uint32 random::urand32() {
return m_kiss99.next() ^ (m_ranctx.next() + m_well512.next());
}
uint64 random::urand64() {
uint64 result = urand32();
result <<= 32ULL;
result |= urand32();
return result;
}
uint32 random::fast_urand32() {
return m_well512.next();
}
uint32 random::bit() {
uint32 k = urand32();
return (k ^ (k >> 6) ^ (k >> 10) ^ (k >> 30)) & 1;
}
double random::drand(double l, double h) {
CRNLIB_ASSERT(l <= h);
if (l >= h)
return l;
return math::clamp(l + (h - l) * (urand32() * cNorm), l, h);
}
float random::frand(float l, float h) {
CRNLIB_ASSERT(l <= h);
if (l >= h)
return l;
float r = static_cast<float>(l + (h - l) * (urand32() * cNorm));
return math::clamp<float>(r, l, h);
}
int random::irand(int l, int h) {
CRNLIB_ASSERT(l < h);
if (l >= h)
return l;
uint32 range = static_cast<uint32>(h - l);
uint32 rnd = urand32();
#if defined(_M_IX86) && defined(_MSC_VER)
//uint32 rnd_range = static_cast<uint32>(__emulu(range, rnd) >> 32U);
uint32 x[2];
*reinterpret_cast<uint64*>(x) = __emulu(range, rnd);
uint32 rnd_range = x[1];
#else
uint32 rnd_range = static_cast<uint32>((((uint64)range) * ((uint64)rnd)) >> 32U);
#endif
int result = l + rnd_range;
CRNLIB_ASSERT((result >= l) && (result < h));
return result;
}
int random::irand_inclusive(int l, int h) {
CRNLIB_ASSERT(h < cINT32_MAX);
return irand(l, h + 1);
}
/*
ALGORITHM 712, COLLECTED ALGORITHMS FROM ACM.
THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,
VOL. 18, NO. 4, DECEMBER, 1992, PP. 434-435.
The function returns a normally distributed pseudo-random number
with a given mean and standard devaiation. Calls are made to a
function subprogram which must return independent random
numbers uniform in the interval (0,1).
The algorithm uses the ratio of uniforms method of A.J. Kinderman
and J.F. Monahan augmented with quadratic bounding curves.
*/
double random::gaussian(double mean, double stddev) {
double q, u, v, x, y;
/*
Generate P = (u,v) uniform in rect. enclosing acceptance region
Make sure that any random numbers <= 0 are rejected, since
gaussian() requires uniforms > 0, but RandomUniform() delivers >= 0.
*/
do {
u = drand(0, 1);
v = drand(0, 1);
if (u <= 0.0 || v <= 0.0) {
u = 1.0;
v = 1.0;
}
v = 1.7156 * (v - 0.5);
/* Evaluate the quadratic form */
x = u - 0.449871;
y = fabs(v) + 0.386595;
q = x * x + y * (0.19600 * y - 0.25472 * x);
/* Accept P if inside inner ellipse */
if (q < 0.27597)
break;
/* Reject P if outside outer ellipse, or outside acceptance region */
} while ((q > 0.27846) || (v * v > -4.0 * log(u) * u * u));
/* Return ratio of P's coordinates as the normal deviate */
return (mean + stddev * v / u);
}
void random::test() {
}
fast_random::fast_random()
: jsr(0xABCD917A),
jcong(0x17F3DEAD) {
}
fast_random::fast_random(const fast_random& other)
: jsr(other.jsr), jcong(other.jcong) {
}
fast_random::fast_random(uint32 i) {
seed(i);
}
fast_random& fast_random::operator=(const fast_random& other) {
jsr = other.jsr;
jcong = other.jcong;
return *this;
}
void fast_random::seed(uint32 i) {
jsr = i;
SHR3;
SHR3;
jcong = (~i) ^ 0xDEADBEEF;
SHR3;
CONG;
}
uint32 fast_random::urand32() {
return SHR3 ^ CONG;
}
uint64 fast_random::urand64() {
uint64 result = urand32();
result <<= 32ULL;
result |= urand32();
return result;
}
int fast_random::irand(int l, int h) {
CRNLIB_ASSERT(l < h);
if (l >= h)
return l;
uint32 range = static_cast<uint32>(h - l);
uint32 rnd = urand32();
#if defined(_M_IX86) && defined(_MSC_VER)
//uint32 rnd_range = static_cast<uint32>(__emulu(range, rnd) >> 32U);
uint32 x[2];
*reinterpret_cast<uint64*>(x) = __emulu(range, rnd);
uint32 rnd_range = x[1];
#else
uint32 rnd_range = static_cast<uint32>((((uint64)range) * ((uint64)rnd)) >> 32U);
#endif
int result = l + rnd_range;
CRNLIB_ASSERT((result >= l) && (result < h));
return result;
}
double fast_random::drand(double l, double h) {
CRNLIB_ASSERT(l <= h);
if (l >= h)
return l;
return math::clamp(l + (h - l) * (urand32() * cNorm), l, h);
}
float fast_random::frand(float l, float h) {
CRNLIB_ASSERT(l <= h);
if (l >= h)
return l;
float r = static_cast<float>(l + (h - l) * (urand32() * cNorm));
return math::clamp<float>(r, l, h);
}
} // namespace crnlib
|