Implement the same random generator as DF uses for DFHack.
parent
d7e35c2d23
commit
9e81d27cd1
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
https://github.com/peterix/dfhack
|
||||||
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any
|
||||||
|
damages arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any
|
||||||
|
purpose, including commercial applications, and to alter it and
|
||||||
|
redistribute it freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must
|
||||||
|
not claim that you wrote the original software. If you use this
|
||||||
|
software in a product, an acknowledgment in the product documentation
|
||||||
|
would be appreciated but is not required.
|
||||||
|
|
||||||
|
2. Altered source versions must be plainly marked as such, and
|
||||||
|
must not be misrepresented as being the original software.
|
||||||
|
|
||||||
|
3. This notice may not be removed or altered from any source
|
||||||
|
distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef CL_MOD_RANDOM
|
||||||
|
#define CL_MOD_RANDOM
|
||||||
|
/**
|
||||||
|
* \defgroup grp_translation Translation: DF word tables and name translation/reading
|
||||||
|
* @ingroup grp_modules
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Export.h"
|
||||||
|
#include "Module.h"
|
||||||
|
#include "Types.h"
|
||||||
|
|
||||||
|
#include "DataDefs.h"
|
||||||
|
|
||||||
|
namespace DFHack
|
||||||
|
{
|
||||||
|
namespace Random
|
||||||
|
{
|
||||||
|
class DFHACK_EXPORT MersenneRNG
|
||||||
|
{
|
||||||
|
static const unsigned MT_LEN = 624;
|
||||||
|
|
||||||
|
unsigned mt_index;
|
||||||
|
uint32_t mt_buffer[MT_LEN];
|
||||||
|
|
||||||
|
void twist();
|
||||||
|
void prefill(unsigned step, int twist_cnt);
|
||||||
|
|
||||||
|
public:
|
||||||
|
void init(const uint32_t *pseed, unsigned cnt, int twist_cnt = 1);
|
||||||
|
|
||||||
|
void init(); // uses time
|
||||||
|
void init(uint32_t seed, int twist_cnt = 1) { init(&seed, 1, twist_cnt); }
|
||||||
|
|
||||||
|
// [0, 2^32)
|
||||||
|
uint32_t random() {
|
||||||
|
if (mt_index >= MT_LEN) twist();
|
||||||
|
return mt_buffer[mt_index++];
|
||||||
|
}
|
||||||
|
// [0, limit)
|
||||||
|
uint32_t random(uint32_t limit) {
|
||||||
|
return uint32_t(uint64_t(random())*limit >> 32);
|
||||||
|
}
|
||||||
|
// (0, 1)
|
||||||
|
double drandom0() {
|
||||||
|
return (double(random())+1)/4294967297.0;
|
||||||
|
}
|
||||||
|
// [0, 1)
|
||||||
|
double drandom() {
|
||||||
|
return double(random())/4294967296.0;
|
||||||
|
}
|
||||||
|
// [0, 1]
|
||||||
|
double drandom1() {
|
||||||
|
return double(random())/4294967295.0;
|
||||||
|
}
|
||||||
|
// [-1, 1]
|
||||||
|
double unitrandom() {
|
||||||
|
return drandom1()*2.0 - 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two exact replicas of functions in DF code
|
||||||
|
int32_t df_trandom(uint32_t max=2147483647LU);
|
||||||
|
int32_t df_loadtrandom(uint32_t max=2147483647LU);
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void unitvector(T *p, int size);
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void permute(T *p, int size) {
|
||||||
|
while(size > 1)
|
||||||
|
{
|
||||||
|
int j = random(size--);
|
||||||
|
T c = p[j]; p[j] = p[size]; p[size] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern template void MersenneRNG::unitvector<float>(float *p, int size);
|
||||||
|
extern template void MersenneRNG::unitvector<double>(double *p, int size);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
https://github.com/peterix/dfhack
|
||||||
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any
|
||||||
|
damages arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any
|
||||||
|
purpose, including commercial applications, and to alter it and
|
||||||
|
redistribute it freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must
|
||||||
|
not claim that you wrote the original software. If you use this
|
||||||
|
software in a product, an acknowledgment in the product documentation
|
||||||
|
would be appreciated but is not required.
|
||||||
|
|
||||||
|
2. Altered source versions must be plainly marked as such, and
|
||||||
|
must not be misrepresented as being the original software.
|
||||||
|
|
||||||
|
3. This notice may not be removed or altered from any source
|
||||||
|
distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Internal.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
#include "modules/Random.h"
|
||||||
|
#include "VersionInfo.h"
|
||||||
|
#include "MemAccess.h"
|
||||||
|
#include "Types.h"
|
||||||
|
#include "ModuleFactory.h"
|
||||||
|
#include "Core.h"
|
||||||
|
#include "Error.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
using namespace DFHack;
|
||||||
|
using namespace df::enums;
|
||||||
|
|
||||||
|
using namespace DFHack::Random;
|
||||||
|
|
||||||
|
//public domain RNG stuff by Michael Brundage
|
||||||
|
//modified to be compatible with the version in DF
|
||||||
|
|
||||||
|
#define MT_IA 397
|
||||||
|
#define MT_IB (MT_LEN - MT_IA)
|
||||||
|
#define UPPER_MASK 0x80000000
|
||||||
|
#define LOWER_MASK 0x7FFFFFFF
|
||||||
|
#define MATRIX_A 0x9908B0DF
|
||||||
|
#define TWIST(b,i,j) ((b)[i] & UPPER_MASK) | ((b)[j] & LOWER_MASK)
|
||||||
|
#define MAGIC(s) (((s)&1)*MATRIX_A)
|
||||||
|
|
||||||
|
void MersenneRNG::twist()
|
||||||
|
{
|
||||||
|
uint32_t *b = mt_buffer;
|
||||||
|
uint32_t s;
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
for (; i < MT_IB; i++) {
|
||||||
|
s = TWIST(b, i, i+1);
|
||||||
|
b[i] = b[i + MT_IA] ^ (s >> 1) ^ MAGIC(s);
|
||||||
|
}
|
||||||
|
for (; i < MT_LEN-1; i++) {
|
||||||
|
s = TWIST(b, i, i+1);
|
||||||
|
b[i] = b[i - MT_IB] ^ (s >> 1) ^ MAGIC(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
s = TWIST(b, MT_LEN-1, 0);
|
||||||
|
b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s);
|
||||||
|
|
||||||
|
mt_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MersenneRNG::prefill(unsigned step, int twist_cnt)
|
||||||
|
{
|
||||||
|
for(unsigned i=step;i<MT_LEN;i++)
|
||||||
|
{
|
||||||
|
//2010: better init line from wikipedia, ultimate source unknown
|
||||||
|
mt_buffer[i]=1812433253UL * (mt_buffer[i-step] ^ (mt_buffer[i-step]>>30)) + i;
|
||||||
|
}
|
||||||
|
|
||||||
|
mt_index = 0;
|
||||||
|
|
||||||
|
for(int j=0;j<twist_cnt;j++)
|
||||||
|
twist();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MersenneRNG::init()
|
||||||
|
{
|
||||||
|
init(Core::getInstance().p->getTickCount(), 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MersenneRNG::init(const uint32_t *pseed, unsigned cnt, int twist_cnt)
|
||||||
|
{
|
||||||
|
memcpy(mt_buffer, pseed, cnt*sizeof(uint32_t));
|
||||||
|
|
||||||
|
prefill(cnt, twist_cnt);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t MersenneRNG::df_trandom(uint32_t max)
|
||||||
|
{
|
||||||
|
if(max<=1)return 0;
|
||||||
|
uint32_t seed=random();
|
||||||
|
seed=seed%2147483647LU;
|
||||||
|
seed=seed/((2147483647LU/max)+1);
|
||||||
|
|
||||||
|
return((int32_t)seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t MersenneRNG::df_loadtrandom(uint32_t max)
|
||||||
|
{
|
||||||
|
uint32_t seed=random();
|
||||||
|
seed=seed%max;
|
||||||
|
|
||||||
|
return((int32_t)seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void MersenneRNG::unitvector(T *p, int size)
|
||||||
|
{
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
T rsqr = 0;
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
p[i] = (T)unitrandom();
|
||||||
|
rsqr += p[i]*p[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rsqr > 0 && rsqr <= 1)
|
||||||
|
{
|
||||||
|
rsqr = std::sqrt(rsqr);
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
p[i] /= rsqr;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template void MersenneRNG::unitvector<float>(float *p, int size);
|
||||||
|
template void MersenneRNG::unitvector<double>(double *p, int size);
|
Loading…
Reference in New Issue