| 1 |
/* |
| 2 |
A C-program for MT19937, with initialization improved 2002/1/26. |
| 3 |
Coded by Takuji Nishimura and Makoto Matsumoto. |
| 4 |
Lua interface by Peter Cawley. |
| 5 |
|
| 6 |
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
| 7 |
All rights reserved. |
| 8 |
Copyright (C) 2005, Mutsuo Saito, |
| 9 |
All rights reserved. |
| 10 |
Copyright (C) 2009, Peter Cawley, |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are met: |
| 15 |
|
| 16 |
1. Redistributions of source code must retain the above copyright notice, |
| 17 |
this list of conditions and the following disclaimer. |
| 18 |
|
| 19 |
2. Redistributions in binary form must reproduce the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer in the |
| 21 |
documentation and/or other materials provided with the distribution. |
| 22 |
|
| 23 |
3. The names of its contributors may not be used to endorse or promote |
| 24 |
products derived from this software without specific prior written |
| 25 |
permission. |
| 26 |
|
| 27 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 28 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 30 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 31 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 32 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 33 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 34 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 35 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 36 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 37 |
POSSIBILITY OF SUCH DAMAGE. |
| 38 |
|
| 39 |
Any feedback is very welcome. |
| 40 |
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
| 41 |
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
| 42 |
*/ |
| 43 |
|
| 44 |
#include <lauxlib.h> |
| 45 |
#include <lua.h> |
| 46 |
#include <stddef.h> |
| 47 |
#ifdef _MSC_VER |
| 48 |
typedef unsigned __int16 uint16_t; |
| 49 |
typedef unsigned __int32 uint32_t; |
| 50 |
#else |
| 51 |
#include <stdint.h> |
| 52 |
#endif /* _MSC_VER */ |
| 53 |
|
| 54 |
/* Period parameters */ |
| 55 |
#define N 624 |
| 56 |
#define M 397 |
| 57 |
#define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
| 58 |
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
| 59 |
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
| 60 |
|
| 61 |
uint32_t mt[N]; /* the array for the state vector */ |
| 62 |
uint16_t mti = N + 1; /* mti==N+1 means mt[N] is not initialized */ |
| 63 |
|
| 64 |
/* initializes mt[N] with a seed */ |
| 65 |
static void init_genrand(uint32_t s) { |
| 66 |
mt[0] = s; |
| 67 |
for (mti = 1; mti < N; mti++) { |
| 68 |
mt[mti] = (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti); |
| 69 |
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
| 70 |
/* In the previous versions, MSBs of the seed affect */ |
| 71 |
/* only MSBs of the array mt[]. */ |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/* generates a random number on [0,0xffffffff]-interval */ |
| 76 |
static uint32_t genrand_int32(void) { |
| 77 |
uint32_t y; |
| 78 |
static uint32_t mag01[2] = {0x0UL, MATRIX_A}; |
| 79 |
/* mag01[x] = x * MATRIX_A for x=0,1 */ |
| 80 |
|
| 81 |
if (mti >= N) { /* generate N words at one time */ |
| 82 |
int kk; |
| 83 |
|
| 84 |
if (mti == N + 1) /* if init_genrand() has not been called, */ |
| 85 |
init_genrand(5489UL); /* a default initial seed is used */ |
| 86 |
|
| 87 |
for (kk = 0; kk < N - M; kk++) { |
| 88 |
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); |
| 89 |
mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 90 |
} |
| 91 |
for (; kk < N - 1; kk++) { |
| 92 |
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK); |
| 93 |
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 94 |
} |
| 95 |
y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK); |
| 96 |
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 97 |
|
| 98 |
mti = 0; |
| 99 |
} |
| 100 |
|
| 101 |
y = mt[mti++]; |
| 102 |
|
| 103 |
/* Tempering */ |
| 104 |
y ^= (y >> 11); |
| 105 |
y ^= (y << 7) & 0x9d2c5680UL; |
| 106 |
y ^= (y << 15) & 0xefc60000UL; |
| 107 |
y ^= (y >> 18); |
| 108 |
|
| 109 |
return y; |
| 110 |
} |
| 111 |
|
| 112 |
/* generates a random number on [0,1) with 53-bit resolution*/ |
| 113 |
static double genrand_res53(void) { |
| 114 |
uint32_t a = genrand_int32() >> 5, b = genrand_int32() >> 6; |
| 115 |
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); |
| 116 |
} |
| 117 |
/* These real versions are due to Isaku Wada, 2002/01/09 added */ |
| 118 |
|
| 119 |
/** |
| 120 |
@function math.random |
| 121 |
@arguments [[range_start, ] range_end] |
| 122 |
@return number |
| 123 |
|
| 124 |
If called with no arguments, returns a random number in the range [0, 1). |
| 125 |
If called with one argument, returns a random integer in the range |
| 126 |
[1, range_end]. If called with two or more arguments, returns a random |
| 127 |
integer in the range [range_start, range_end]. |
| 128 |
*/ |
| 129 |
static int l_random(lua_State* L) { |
| 130 |
lua_Integer min; |
| 131 |
uint32_t max; |
| 132 |
switch (lua_gettop(L)) { |
| 133 |
default: |
| 134 |
case 2: |
| 135 |
min = luaL_checkinteger(L, 1); |
| 136 |
max = (uint32_t)(luaL_checkinteger(L, 2) - min + 1); |
| 137 |
luaL_argcheck(L, max > 0, 2, "interval is empty"); |
| 138 |
lua_pushinteger(L, min + (lua_Integer)(genrand_int32() % max)); |
| 139 |
break; |
| 140 |
case 1: |
| 141 |
max = (uint32_t)luaL_checkinteger(L, 1); |
| 142 |
luaL_argcheck(L, 1 <= max, 1, "interval is empty"); |
| 143 |
lua_pushinteger(L, 1 + (lua_Integer)(genrand_int32() % max)); |
| 144 |
break; |
| 145 |
case 0: |
| 146 |
lua_pushnumber(L, (lua_Number)genrand_res53()); |
| 147 |
break; |
| 148 |
} |
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
@function math.randomdump |
| 154 |
@arguments |
| 155 |
@return string |
| 156 |
|
| 157 |
Returns a string which can later be passed to math.randomseed() to restore |
| 158 |
the random number generator to its current state. |
| 159 |
*/ |
| 160 |
static int l_randomdump(lua_State* L) { |
| 161 |
lua_pushlstring(L, (const char*)mt, N * 4); |
| 162 |
lua_pushlstring(L, (const char*)&mti, 2); |
| 163 |
lua_concat(L, 2); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
@function math.randomseed |
| 169 |
@arguments number |
| 170 |
@arguments string |
| 171 |
@return |
| 172 |
|
| 173 |
Seeds the random number generator using the given seed number, or restores |
| 174 |
the random number generator state from a string previously generated from |
| 175 |
math.randomdump(). |
| 176 |
*/ |
| 177 |
static int l_randomseed(lua_State* L) { |
| 178 |
if (lua_type(L, 1) == LUA_TSTRING) { |
| 179 |
int i; |
| 180 |
size_t len; |
| 181 |
const char* data = lua_tolstring(L, 1, &len); |
| 182 |
if (len != N * 4 + 2) luaL_argerror(L, 1, "Seed string wrong length"); |
| 183 |
for (i = 0; i < N; ++i) mt[i] = ((uint32_t*)data)[i]; |
| 184 |
mti = *(uint16_t*)(data + len - 2); |
| 185 |
} else { |
| 186 |
init_genrand((uint32_t)luaL_checkinteger(L, 1)); |
| 187 |
} |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
int luaopen_random(lua_State* L) { |
| 192 |
lua_getglobal(L, "math"); |
| 193 |
lua_pushliteral(L, "random"); |
| 194 |
lua_pushcfunction(L, l_random); |
| 195 |
lua_settable(L, -3); |
| 196 |
lua_pushliteral(L, "randomseed"); |
| 197 |
lua_pushcfunction(L, l_randomseed); |
| 198 |
lua_settable(L, -3); |
| 199 |
lua_pushliteral(L, "randomdump"); |
| 200 |
lua_pushcfunction(L, l_randomdump); |
| 201 |
lua_settable(L, -3); |
| 202 |
return 0; |
| 203 |
} |