tibia-rme/source/mt_rand.cpp

122 lines
3 KiB
C++
Raw Permalink Normal View History

//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
2020-07-30 11:42:28 -03:00
// Remere's Map Editor 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.
//
// Remere's Map Editor 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 this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
2023-10-09 19:12:16 -07:00
static inline unsigned long int mt_get(void* vstate);
static double mt_get_double(void* vstate);
static void mt_set(void* state, unsigned long int s);
2023-10-09 19:12:16 -07:00
#define N 624 /* Period parameters */
#define M 397
/* most significant w-r bits */
2016-09-29 19:24:45 -03:00
static const unsigned long UPPER_MASK = 0x80000000UL;
/* least significant r bits */
2016-09-29 19:24:45 -03:00
static const unsigned long LOWER_MASK = 0x7fffffffUL;
typedef struct
2023-10-09 19:12:16 -07:00
{
unsigned long mt[N];
int mti;
} mt_state_t;
static inline unsigned long
2023-10-09 19:12:16 -07:00
mt_get(void* vstate) {
mt_state_t* state = (mt_state_t*)vstate;
2023-10-09 19:12:16 -07:00
unsigned long k;
unsigned long int* const mt = state->mt;
#define MAGIC(y) (((y)&0x1) ? 0x9908b0dfUL : 0)
2023-10-09 19:12:16 -07:00
if (state->mti >= N) { /* generate N words at one time */
int kk;
2023-10-09 19:12:16 -07:00
for (kk = 0; kk < N - M; kk++) {
unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + M] ^ (y >> 1) ^ MAGIC(y);
}
for (; kk < N - 1; kk++) {
unsigned long y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ MAGIC(y);
}
2023-10-09 19:12:16 -07:00
{
unsigned long y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ MAGIC(y);
}
2023-10-09 19:12:16 -07:00
state->mti = 0;
}
2023-10-09 19:12:16 -07:00
/* Tempering */
2016-09-29 19:24:45 -03:00
2023-10-09 19:12:16 -07:00
k = mt[state->mti];
k ^= (k >> 11);
k ^= (k << 7) & 0x9d2c5680UL;
k ^= (k << 15) & 0xefc60000UL;
k ^= (k >> 18);
2023-10-09 19:12:16 -07:00
state->mti++;
2023-10-09 19:12:16 -07:00
return k;
}
static double
2023-10-09 19:12:16 -07:00
mt_get_double(void* vstate) {
return mt_get(vstate) / 4294967296.0;
}
static void
2023-10-09 19:12:16 -07:00
mt_set(void* vstate, unsigned long int s) {
mt_state_t* state = (mt_state_t*)vstate;
int i;
2023-10-09 19:12:16 -07:00
if (s == 0) {
s = 4357; /* the default seed is 4357 */
}
2023-10-09 19:12:16 -07:00
state->mt[0] = s & 0xffffffffUL;
2023-10-09 19:12:16 -07:00
for (i = 1; i < N; i++) {
/* See Knuth's "Art of Computer Programming" Vol. 2, 3rd
Ed. p.106 for multiplier. */
2023-10-09 19:12:16 -07:00
state->mt[i] = (1812433253UL * (state->mt[i - 1] ^ (state->mt[i - 1] >> 30)) + i);
2016-09-29 19:24:45 -03:00
2023-10-09 19:12:16 -07:00
state->mt[i] &= 0xffffffffUL;
}
2023-10-09 19:12:16 -07:00
state->mti = i;
}
static mt_state_t mt_state;
void mt_seed(unsigned long s) {
mt_set(&mt_state, s);
}
unsigned long mt_randi() {
return mt_get(&mt_state);
}
double mt_randd() {
return mt_get_double(&mt_state);
}