| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* random.c: Portable mersinne-twister based psuedo-random number generator. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* Algorithm copyright (c) 1999-2007 Takuji Nishimura and Makoto Matsumoto |
| 7 |
* |
| 8 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 9 |
* purpose with or without fee is hereby granted, provided that the above |
| 10 |
* copyright notice and this permission notice is present in all copies. |
| 11 |
* |
| 12 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 13 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 16 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 17 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 18 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 20 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 21 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 22 |
* POSSIBILITY OF SUCH DAMAGE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "mowgli.h" |
| 26 |
#include "core/bootstrap_internal.h" |
| 27 |
|
| 28 |
/* period parameters */ |
| 29 |
#define N 624 |
| 30 |
#define M 397 |
| 31 |
#define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
| 32 |
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
| 33 |
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
| 34 |
|
| 35 |
/* mowgli_random_t contains state data which is private */ |
| 36 |
struct mowgli_random_ |
| 37 |
{ |
| 38 |
mowgli_object_t object; |
| 39 |
unsigned int mt[N]; |
| 40 |
size_t mti; |
| 41 |
}; |
| 42 |
|
| 43 |
static mowgli_object_class_t klass; |
| 44 |
|
| 45 |
/* initialization */ |
| 46 |
void |
| 47 |
mowgli_random_bootstrap(void) |
| 48 |
{ |
| 49 |
mowgli_object_class_init(&klass, "mowgli_random_t", NULL, FALSE); |
| 50 |
} |
| 51 |
|
| 52 |
/* construction and destruction. */ |
| 53 |
mowgli_random_t * |
| 54 |
mowgli_random_create(void) |
| 55 |
{ |
| 56 |
return mowgli_random_create_with_seed(time(NULL)); |
| 57 |
} |
| 58 |
|
| 59 |
mowgli_random_t * |
| 60 |
mowgli_random_create_with_seed(unsigned int seed) |
| 61 |
{ |
| 62 |
mowgli_random_t *out = mowgli_alloc(sizeof *out); |
| 63 |
|
| 64 |
mowgli_object_init(mowgli_object(out), NULL, &klass, NULL); |
| 65 |
|
| 66 |
mowgli_random_reseed(out, seed); |
| 67 |
|
| 68 |
return out; |
| 69 |
} |
| 70 |
|
| 71 |
/* reset seed */ |
| 72 |
void |
| 73 |
mowgli_random_reseed(mowgli_random_t *self, unsigned int seed) |
| 74 |
{ |
| 75 |
return_if_fail(self != NULL); |
| 76 |
|
| 77 |
self->mt[0] = seed & 0xffffffffUL; |
| 78 |
|
| 79 |
for (self->mti = 1; self->mti < N; self->mti++) |
| 80 |
{ |
| 81 |
self->mt[self->mti] = (1812433253UL * (self->mt[self->mti - 1] ^ (self->mt[self->mti - 1] >> 30)) + self->mti); |
| 82 |
self->mt[self->mti] &= 0xffffffffUL; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/* number retrieval */ |
| 87 |
unsigned int |
| 88 |
mowgli_random_int(mowgli_random_t *self) |
| 89 |
{ |
| 90 |
unsigned int y; |
| 91 |
static unsigned int mag01[2] = { 0x0UL, MATRIX_A }; |
| 92 |
|
| 93 |
return_val_if_fail(self != NULL, 0); |
| 94 |
|
| 95 |
if (self->mti >= N) |
| 96 |
{ |
| 97 |
int t; |
| 98 |
|
| 99 |
for (t = 0; t < N - M; t++) |
| 100 |
{ |
| 101 |
y = (self->mt[t] & UPPER_MASK) | (self->mt[t + 1] & LOWER_MASK); |
| 102 |
self->mt[t] = self->mt[t + M] ^ (y >> 1) ^ mag01[y & 0x1U]; |
| 103 |
} |
| 104 |
|
| 105 |
for (; t < N - 1; t++) |
| 106 |
{ |
| 107 |
y = (self->mt[t] & UPPER_MASK) | (self->mt[t + 1] & LOWER_MASK); |
| 108 |
self->mt[t] = self->mt[t + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1U]; |
| 109 |
} |
| 110 |
|
| 111 |
y = (self->mt[N - 1] & UPPER_MASK) | (self->mt[0] & LOWER_MASK); |
| 112 |
self->mt[N - 1] = self->mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1U]; |
| 113 |
self->mti = 0; |
| 114 |
} |
| 115 |
|
| 116 |
y = self->mt[self->mti++]; |
| 117 |
|
| 118 |
/* tempering */ |
| 119 |
y ^= (y >> 11); |
| 120 |
y ^= (y << 7) & 0x9d2c5680U; |
| 121 |
y ^= (y << 15) & 0xefc60000U; |
| 122 |
y ^= (y >> 18); |
| 123 |
|
| 124 |
return y; |
| 125 |
} |
| 126 |
|
| 127 |
int |
| 128 |
mowgli_random_int_ranged(mowgli_random_t *self, int begin, int end) |
| 129 |
{ |
| 130 |
unsigned int dist = end - begin; |
| 131 |
unsigned int max, ret; |
| 132 |
|
| 133 |
if (dist <= 0x80000000U) |
| 134 |
{ |
| 135 |
unsigned int remain = (0x80000000U % dist) * 2; |
| 136 |
|
| 137 |
if (remain >= dist) |
| 138 |
remain -= dist; |
| 139 |
|
| 140 |
max = 0xFFFFFFFFU - remain; |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
max = dist - 1; |
| 145 |
} |
| 146 |
|
| 147 |
do |
| 148 |
ret = mowgli_random_int(self); |
| 149 |
|
| 150 |
while (ret > max); |
| 151 |
|
| 152 |
ret %= dist; |
| 153 |
|
| 154 |
return begin + ret; |
| 155 |
} |