Restructure repository to include all source folders

Move git root from Client/ to src/ to track all source code:
- Client: Game client source (moved to Client/Client/)
- Server: Game server source
- GameTools: Development tools
- CryptoSource: Encryption utilities
- database: Database scripts
- Script: Game scripts
- rylCoder_16.02.2008_src: Legacy coder tools
- GMFont, Game: Additional resources

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-29 20:17:20 +09:00
parent 5d3cd64a25
commit dd97ddec92
11602 changed files with 1446576 additions and 0 deletions

View File

@@ -0,0 +1,220 @@
#ifndef _MATH_CONVERT_INLINE_H_
#define _MATH_CONVERT_INLINE_H_
#include <cstdlib>
#include <tchar.h>
inline void Math::Convert::Hex08ToStr( TCHAR *szDest, BYTE hex )
{
*((WORD *) szDest) = m_FastHeToBi[ hex ]; szDest += 2;
*(szDest) = '\0';
}
inline void Math::Convert::Hex16ToStr( TCHAR *szDest, WORD hex )
{
LPBYTE pSrc = (LPBYTE) &hex;
#ifdef BIG_ENDIAN
*((WORD *) (szDest + 0)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 2)) = m_FastHeToBi[ *(pSrc++) ];
#else
*((WORD *) (szDest + 2)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 0)) = m_FastHeToBi[ *(pSrc++) ];
#endif
*(szDest + 4) = '\0';
}
inline void Math::Convert::Hex32ToStr( TCHAR *szDest, unsigned long hex )
{
LPBYTE pSrc = (LPBYTE) &hex;
#ifdef BIG_ENDIAN
*((WORD *) (szDest + 0)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 2)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 4)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 6)) = m_FastHeToBi[ *(pSrc++) ];
#else
*((WORD *) (szDest + 6)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 4)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 2)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 0)) = m_FastHeToBi[ *(pSrc++) ];
#endif
*(szDest + 8) = '\0';
}
inline void Math::Convert::Hex64ToStr( TCHAR *szDest, DWORD64 hex )
{
LPBYTE pSrc = (LPBYTE) &hex;
#ifdef BIG_ENDIAN
*((WORD *) (szDest + 0)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 2)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 4)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 6)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 8)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 10)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 12)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 14)) = m_FastHeToBi[ *(pSrc++) ];
#else
*((WORD *) (szDest + 14)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 12)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 10)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 8)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 6)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 4)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 2)) = m_FastHeToBi[ *(pSrc++) ];
*((WORD *) (szDest + 0)) = m_FastHeToBi[ *(pSrc++) ];
#endif
*(szDest + 16) = '\0';
}
//
// this function returns the equivalent binary value for an individual character specified in the ascii format
inline TCHAR Math::Convert::BiToHe(TCHAR cBin)
{
if((cBin >= '0') && (cBin <= '9'))
{
return (cBin - '0');
}
else if((cBin >= 'A') && (cBin <= 'F'))
{
return (cBin - 'A' + 0xA);
}
if((cBin >= 'a') && (cBin <= 'f'))
{
return (cBin -'a' + 0xA);
}
return cBin;
}
inline void Math::Convert::AcToHe(char *szDst, char *szSrc, int iCount)
{
while(iCount--)
{
*szDst = BiToHe(*szSrc) << 4;
*szSrc++;
*szDst += BiToHe(*szSrc);
*szSrc++;
*szDst++;
}
}
inline BYTE Math::Convert::StrToHex08(const TCHAR *szSrc)
{
const TCHAR* pStart = szSrc + 2;
BYTE cHex = 0;
TCHAR c1, c2;
for (int i=0; i<1; ++i)
{
c1 = BiToHe(*pStart++);
c1 <<= (8*(7-i)+4);
c2 = BiToHe(*pStart++);
c2 <<= (8*(7-i));
cHex += (c1 + c2);
}
return cHex;
}
inline WORD Math::Convert::StrToHex16(const TCHAR *szSrc)
{
const TCHAR* pStart = szSrc + 2;
WORD sHex = 0;
for (int i=0; i<2; i++)
{
WORD s1 = BiToHe(*pStart++);
s1 <<= (8*(1-i)+4);
WORD s2 = BiToHe(*pStart++);
s2 <<= (8*(1-i));
WORD sRet = s1 + s2;
sHex += sRet;
}
return sHex;
}
// convert string to hexadecimal value
inline unsigned long Math::Convert::StrToHex32(const TCHAR *szSrc)
{
const TCHAR* pStart = szSrc + 2;
unsigned long nHex = 0;
for (int i=0; i<4; i++)
{
unsigned long n1 = BiToHe(*pStart++);
n1 <<= (8*(3-i)+4);
unsigned long n2 = BiToHe(*pStart++);
n2 <<= (8*(3-i));
unsigned long nRet = n1 + n2;
nHex += nRet;
}
return nHex;
}
// convert string to hexadecimal value
inline DWORD64 Math::Convert::StrToHex64(const TCHAR *szSrc)
{
const TCHAR* pStart = szSrc + 2;
DWORD64 dlHex = 0;
for (int i=0; i<8; i++)
{
DWORD64 dl1 = BiToHe(*pStart++);
dl1 <<= (8*(7-i)+4);
DWORD64 dl2 = BiToHe(*pStart++);
dl2 <<= (8*(7-i));
DWORD64 dlRet = dl1 + dl2;
dlHex += dlRet;
}
return dlHex;
}
inline BYTE Math::Convert::Atoc(const TCHAR *szSrc)
{
return (0 == _tcsncmp(szSrc, _T("0x"), 2)) ? StrToHex08(szSrc) : (BYTE) _ttol(szSrc);
}
//
// TCHAR *<2A><> <20><>Ĩ<EFBFBD>ô<EFBFBD>! (By Standil)
//
inline WORD Math::Convert::Atos(const TCHAR *szSrc)
{
return (0 == _tcsncmp(szSrc, _T("0x"), 2)) ? StrToHex16(szSrc) : _ttoi(szSrc);
}
//
// TCHAR *<2A><> <20><>Ĩ<EFBFBD>ô<EFBFBD>! (By Standil)
//
inline unsigned long Math::Convert::Atoi(const TCHAR *szSrc)
{
return (0 == _tcsncmp(szSrc, _T("0x"), 2)) ? StrToHex32(szSrc) : _ttol(szSrc);
}
//
// TCHAR *<2A><> <20><>Ĩ<EFBFBD>ô<EFBFBD>! (By Standil)
//
inline DWORD64 Math::Convert::Atol64(const TCHAR *szSrc)
{
return (0 == _tcsncmp(szSrc, _T("0x"), 2)) ? StrToHex64(szSrc) : _ttoi64(szSrc);
}
#endif

View File

@@ -0,0 +1,100 @@
// FastMath.cpp: implementation of the CFastMath class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Math.h"
#include "Random.h"
Math::RandomInt st_random;
///////////////////////////////////////////////////////////////////////////////////////////////
// utility functions
// 0 -0x7FFFFFFF.
unsigned long Math::Random::ComplexRandom(int nEndExtent, int nBeginExtent)
{
// <20><><EFBFBD><EFBFBD> <20>ҽ<EFBFBD><D2BD><EFBFBD> 100<30><30> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0~99<39><39><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
// <20><><EFBFBD><EFBFBD> <20>ۼ<EFBFBD><DBBC><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 100<30><30> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0~100<30><30><EFBFBD><EFBFBD> <20><><EFBFBD>´<EFBFBD>.
// <20>׷<EFBFBD><D7B7><EFBFBD> -1<><31> <20>ϰ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>.
if(nBeginExtent == 0)
return st_random.Next(nEndExtent);
return st_random.Next(nBeginExtent, nEndExtent);
/*
if (nEndExtent < 1 || nEndExtent <= nBeginExtent)
{
return nBeginExtent;
}
static unsigned long x = 1;
static unsigned long c = 0;
static unsigned long a = 2083801278UL;
#define addto(rh,rl,ah,al) (rl) = ((rl) + (al)) & 0xFFFFFFFFUL; \
(rh) = ((rh) + (ah) + (((rl) < (al)) ? 1 : 0)) & 0xFFFFFFFFUL
unsigned long xl = (x & 0xFFFFUL);
unsigned long xh = (x >> 16) & 0xFFFFUL;
unsigned long al = (a & 0xFFFFUL);
unsigned long ah = (a >> 16) & 0xFFFFUL;
unsigned long tmp;
x = c;
c = 0;
tmp = xl * al;
addto(c, x, 0, tmp);
tmp = xl * ah;
addto(c, x, (tmp >> 16), (tmp & 0xFFFFUL) << 16);
tmp = xh * ah;
addto(c, x, tmp, 1);
tmp = xh * al;
addto(c, x, (tmp >> 16), (tmp & 0xFFFFUL) << 16);
return (x % (nEndExtent - nBeginExtent)) + nBeginExtent; // return x & 0x7FFFFFFFUL;
*/
}
int Math::Random::SimpleRandom(unsigned long dwSeed, int nEndExtent, int nBeginExtent)
{
return( nBeginExtent + ((((dwSeed * 214013L + 2531011L) >> 16) & 0x7FFF) % (nEndExtent - nBeginExtent)) );
}
// This table only works on Little Endian(Byte Order) machine.
const unsigned short Math::Convert::m_FastHeToBi[0x100] =
{
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730,
0x3830, 0x3930, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, 0x4630,
0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731,
0x3831, 0x3931, 0x4131, 0x4231, 0x4331, 0x4431, 0x4531, 0x4631,
0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732,
0x3832, 0x3932, 0x4132, 0x4232, 0x4332, 0x4432, 0x4532, 0x4632,
0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733,
0x3833, 0x3933, 0x4133, 0x4233, 0x4333, 0x4433, 0x4533, 0x4633,
0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734,
0x3834, 0x3934, 0x4134, 0x4234, 0x4334, 0x4434, 0x4534, 0x4634,
0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735,
0x3835, 0x3935, 0x4135, 0x4235, 0x4335, 0x4435, 0x4535, 0x4635,
0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736,
0x3836, 0x3936, 0x4136, 0x4236, 0x4336, 0x4436, 0x4536, 0x4636,
0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737,
0x3837, 0x3937, 0x4137, 0x4237, 0x4337, 0x4437, 0x4537, 0x4637,
0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738,
0x3838, 0x3938, 0x4138, 0x4238, 0x4338, 0x4438, 0x4538, 0x4638,
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739,
0x3839, 0x3939, 0x4139, 0x4239, 0x4339, 0x4439, 0x4539, 0x4639,
0x3041, 0x3141, 0x3241, 0x3341, 0x3441, 0x3541, 0x3641, 0x3741,
0x3841, 0x3941, 0x4141, 0x4241, 0x4341, 0x4441, 0x4541, 0x4641,
0x3042, 0x3142, 0x3242, 0x3342, 0x3442, 0x3542, 0x3642, 0x3742,
0x3842, 0x3942, 0x4142, 0x4242, 0x4342, 0x4442, 0x4542, 0x4642,
0x3043, 0x3143, 0x3243, 0x3343, 0x3443, 0x3543, 0x3643, 0x3743,
0x3843, 0x3943, 0x4143, 0x4243, 0x4343, 0x4443, 0x4543, 0x4643,
0x3044, 0x3144, 0x3244, 0x3344, 0x3444, 0x3544, 0x3644, 0x3744,
0x3844, 0x3944, 0x4144, 0x4244, 0x4344, 0x4444, 0x4544, 0x4644,
0x3045, 0x3145, 0x3245, 0x3345, 0x3445, 0x3545, 0x3645, 0x3745,
0x3845, 0x3945, 0x4145, 0x4245, 0x4345, 0x4445, 0x4545, 0x4645,
0x3046, 0x3146, 0x3246, 0x3346, 0x3446, 0x3546, 0x3646, 0x3746,
0x3846, 0x3946, 0x4146, 0x4246, 0x4346, 0x4446, 0x4546, 0x4646,
};

View File

@@ -0,0 +1,99 @@
// FastMath.h: interface for the CFastMath class.
//
//////////////////////////////////////////////////////////////////////
#ifndef _CUSTOM_MATH_FUNCTIONS_H_
#define _CUSTOM_MATH_FUNCTIONS_H_
#include <winsock2.h>
#include <windows.h>
#include <cmath>
namespace Math
{
namespace Convert
{
extern const unsigned short m_FastHeToBi[0x100];
// SPX, IPX <20>ּ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD> <20>Լ<EFBFBD>
inline TCHAR BiToHe(TCHAR cBin);
inline void AcToHe(char *szDst, char *szSrc, int iCount);
// String<6E><67> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ٲ<EFBFBD>(String<6E><67> 0x<30><78> <20><><EFBFBD><EFBFBD> Hex string<6E>̴<EFBFBD>.)
inline BYTE StrToHex08(const TCHAR *szSrc);
inline WORD StrToHex16(const TCHAR *szSrc);
inline unsigned long StrToHex32(const TCHAR *szSrc);
inline DWORD64 StrToHex64(const TCHAR *szSrc);
// String<6E><67> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ٲ<EFBFBD>(0x<30><78> <20>ٴ<EFBFBD><D9B4><EFBFBD>, <20><><EFBFBD><EFBFBD> <20>ʴ<EFBFBD><CAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>ٲ<EFBFBD> <20><> <20>ִ<EFBFBD>)
inline BYTE Atoc(const TCHAR *szSrc);
inline WORD Atos(const TCHAR *szSrc);
inline unsigned long Atoi(const TCHAR *szSrc);
inline DWORD64 Atol64(const TCHAR *szSrc);
// <20><><EFBFBD><EFBFBD> Hex String<6E><67><EFBFBD><EFBFBD> <20>ٲ<EFBFBD>
inline void Hex08ToStr(TCHAR *szDest, BYTE hex);
inline void Hex16ToStr(TCHAR *szDest, WORD hex);
inline void Hex32ToStr(TCHAR *szDest, unsigned long hex);
inline void Hex64ToStr(TCHAR *szDest, DWORD64 hex);
};
namespace Random
{
// <20><><EFBFBD>ϰ<EFBFBD><CFB0><EFBFBD> nBeginExtent ~ nEndExtent - 1<><31> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>´<EFBFBD>.
unsigned long ComplexRandom(int nEndExtent, int nBeginExtent = 0); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>.
int SimpleRandom(unsigned long dwSeed, int nEndExtent, int nBeginExtent = 0);
};
namespace HashFunc
{
// -------------------------------------------------------------------------------------------------
// String-to-Hash <20>Լ<EFBFBD><D4BC><EFBFBD> : http://www.cs.yorku.ca/~oz/hash.html <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
inline unsigned long djb2Hash(const unsigned char *str); // first reported by dan bernstein
inline unsigned long sdbmHash(const unsigned char *str); // this is one of the algorithms used in berkeley db
inline unsigned long looseHash(const unsigned char *str); // <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ؽ<EFBFBD>. <20>׳<EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>.
};
namespace Const
{
static float PI = 3.14159f;
}
};
#include "Convert.inl"
inline unsigned long Math::HashFunc::djb2Hash(const unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++) { hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ }
return hash;
}
inline unsigned long Math::HashFunc::sdbmHash(const unsigned char *str)
{
unsigned long hash = 0;
int c;
while (c = *str++) { hash = c + (hash << 6) + (hash << 16) - hash; }
return hash;
}
inline unsigned long Math::HashFunc::looseHash(const unsigned char *str)
{
unsigned int hash = 0;
int c;
while (c = *str++) { hash += c; }
return hash;
}
#endif // !defined(AFX_FASTMATH_H__ED69578B_18C1_42EA_9C5E_888DC38101C2__INCLUDED_)

View File

@@ -0,0 +1,65 @@
// mtrand.cpp, see include file mtrand.h for information
#include "stdafx.h"
#include "PseudoRandom.h"
#include <time.h>
// non-inline function definitions and static member definitions cannot
// reside in header file because of the risk of multiple declarations
// initialization of static private members
/*unsigned long MersenneTwister::state[Constant1] = {0x0UL};
int MersenneTwister::position = 0;*/
namespace Math
{
MersenneTwister::MersenneTwister(BOOL useCurrentTime) {
if ( useCurrentTime) {
time_t t; time(&t);
SetSeed(unsigned long(t));
} else {
SetSeed(5489UL);
}
}
void MersenneTwister::GenerateState() { // generate new state vector
for (int i = 0; i < (Constant1 - Constant2); ++i)
state[i] = state[i + Constant2] ^ Twiddle(state[i], state[i + 1]);
for (int i = Constant1 - Constant2; i < (Constant1 - 1); ++i)
state[i] = state[i + Constant2 - Constant1] ^ Twiddle(state[i], state[i + 1]);
state[Constant1 - 1] = state[Constant2 - 1] ^ Twiddle(state[Constant1 - 1], state[0]);
position = 0; // reset position
}
void MersenneTwister::SetSeed(unsigned long s) { // initialized by 32 bit seed
for ( int i=0; i<Constant1; ++i) { state[i]= 0x0UL; }
//state[Constant1] = {0x0UL};
state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines
for (int i = 1; i < Constant1; ++i) {
state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
// see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier
// in the previous versions, MSBs of the seed affect only MSBs of the array state
// 2002/01/09 modified by Makoto Matsumoto
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
}
position = Constant1; // force GenerateState() to be called for next random number
}
void MersenneTwister::SetSeed(const unsigned long* array, int size) { // initialized by array
SetSeed(19650218UL);
int i = 1, j = 0;
for (int k = ((Constant1 > size) ? Constant1 : size); k; --k) {
state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL))
+ array[j] + j; // non linear
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
++j; j %= size;
if ((++i) == Constant1) { state[0] = state[Constant1 - 1]; i = 1; }
}
for (int k = Constant1 - 1; k; --k) {
state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i;
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
if ((++i) == Constant1) { state[0] = state[Constant1 - 1]; i = 1; }
}
state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
position = Constant1; // force GenerateState() to be called for next random number
}
}

View File

@@ -0,0 +1,224 @@
#pragma once
namespace Math
{
/**
The Mersenne Twister random number generator
usage:
PseudoRandomInt foo; //initializes random sequence by the current time
PseudoRandomInt foo(false); // the default seed 5489UL is used
foo.SetSeed(230194019);
foo.Next()
foo.Next(10,4300); //returns unsigned long between 10 and 4300
PseudoRandomFloat foo;
foo.Next(); // return double between [0..1)
foo.Next(245); // return double between [0..245)
PseudoRandomFloatClosed foo;
foo.Next(); // return double between [0..1]
PseudoRandomFloatOpen foo;
foo.Next(); // return double between (0..1)
details:
The Mersenne twister is a pseudorandom number generator that was developed in 1997 by Makoto Matsumoto
(<28><><EFBFBD><EFBFBD> <20><>) and Takuji Nishimura (<28><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>). It provides for fast generation of very high quality random
numbers - having been designed specifically to rectify many of the flaws found in older algorithms.
It has the following desirable properties:
It was designed to have a colossal period of 219937-1 (the creators of the algorithm proved this property).
It has a very high order of dimensional equidistribution (see linear congruential generator). Note that this means, by default, that there is negligible serial correlation between successive values in the output sequence.
It is faster than all but the most statistically unsound generators.
It is statistically random in all the bits of its output.
This implementation is based C++ code by Takuji Nishimura and Makoto Matsumoto, Jasper Bedaux, Isaku Wada
*/
class MersenneTwister {
public:
// default constructor: uses default seed only if this is the first instance
MersenneTwister(BOOL useCurrentTime=TRUE);
// constructor with 32 bit int as seed
MersenneTwister(unsigned long s) { SetSeed(s); }
// constructor with array of size 32 bit ints as seed
MersenneTwister(const unsigned long* array, int size) { SetSeed(array, size); }
// the two seed functions
void SetSeed(unsigned long); // seed with 32 bit integer
void SetSeed(const unsigned long*, int size); // seed with array
// overload operator() to make this a generator (functor)
//unsigned long operator()() { return GenerateRandom(); }
//unsigned long Next() { return (*this)(); }
~MersenneTwister() {} // destructor
protected: // used by derived classes, otherwise not accessible; use the ()-operator
unsigned long GenerateRandom() { // generate 32 bit random integer
if (position == Constant1) GenerateState(); // new state vector needed
// GenerateState() is split off to be non-inline, because it is only called once
// in every 624 calls and otherwise irand() would become too big to get inlined
unsigned long x = state[position++];
x ^= (x >> 11);
x ^= (x << 7) & 0x9D2C5680UL;
x ^= (x << 15) & 0xEFC60000UL;
return x ^ (x >> 18);
}
private:
enum { Constant1 = 624, Constant2 = 397 };
unsigned long state[Constant1]; // state vector array
int position; // position in state array
// private functions used to generate the pseudo random numbers
unsigned long Twiddle(unsigned long u, unsigned long v) {
return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1) ^ ((v & 1UL) ? 0x9908B0DFUL : 0x0UL);
} // used by GenerateState()
void GenerateState(); // generate new state
// make copy constructor and assignment operator unavailable, they don't make sense
MersenneTwister(const MersenneTwister&); // copy constructor not defined
void operator=(const MersenneTwister&); // assignment operator not defined
};
class PseudoRandomInt : public MersenneTwister {
public:
PseudoRandomInt(BOOL useCurrentTime=TRUE): MersenneTwister(useCurrentTime) {}
PseudoRandomInt(unsigned long seed) : MersenneTwister(seed) {}
PseudoRandomInt(const unsigned long* seed, int size) : MersenneTwister(seed, size) {}
unsigned long operator()() { return GenerateRandom(); }
unsigned long Next() { return (*this)(); }
unsigned long Next(unsigned long minval, unsigned long maxval) {
return minval+(Next()%(maxval-minval));
}
};
// generates double floating point numbers in the half-open interval [0, 1)
class PseudoRandomFloat : public MersenneTwister {
public:
PseudoRandomFloat(BOOL useCurrentTime=TRUE) : MersenneTwister(useCurrentTime) {}
PseudoRandomFloat(unsigned long seed) : MersenneTwister(seed) {}
PseudoRandomFloat(const unsigned long* seed, int size) : MersenneTwister(seed, size) {}
~PseudoRandomFloat() {}
double operator()() {
return static_cast<double>(GenerateRandom()) * (1. / 4294967296.); } // divided by 2^32
double Next() { return (*this)(); }
double Next(double mul) { return Next()*mul; }
private:
PseudoRandomFloat(const PseudoRandomFloat&); // copy constructor not defined
void operator=(const PseudoRandomFloat&); // assignment operator not defined
};
// generates double floating point numbers in the closed interval [0, 1]
class PseudoRandomFloatClosed : public MersenneTwister {
public:
PseudoRandomFloatClosed(BOOL useCurrentTime=TRUE) : MersenneTwister(useCurrentTime) {}
PseudoRandomFloatClosed(unsigned long seed) : MersenneTwister(seed) {}
PseudoRandomFloatClosed(const unsigned long* seed, int size) : MersenneTwister(seed, size) {}
~PseudoRandomFloatClosed() {}
double operator()() {
return static_cast<double>(GenerateRandom()) * (1. / 4294967295.); } // divided by 2^32 - 1
double Next() { return (*this)(); }
double Next(double mul) { return Next()*mul; }
private:
PseudoRandomFloatClosed(const PseudoRandomFloatClosed&); // copy constructor not defined
void operator=(const PseudoRandomFloatClosed&); // assignment operator not defined
};
// generates double floating point numbers in the open interval (0, 1)
class PseudoRandomFloatOpen : public MersenneTwister {
public:
PseudoRandomFloatOpen(BOOL useCurrentTime=TRUE) : MersenneTwister(useCurrentTime) {}
PseudoRandomFloatOpen(unsigned long seed) : MersenneTwister(seed) {}
PseudoRandomFloatOpen(const unsigned long* seed, int size) : MersenneTwister(seed, size) {}
~PseudoRandomFloatOpen() {}
double operator()() {
return (static_cast<double>(GenerateRandom()) + .5) * (1. / 4294967296.); } // divided by 2^32
double Next() { return (*this)(); }
double Next(double mul) { return Next()*mul; }
private:
PseudoRandomFloatOpen(const PseudoRandomFloatOpen&); // copy constructor not defined
void operator=(const PseudoRandomFloatOpen&); // assignment operator not defined
};
// generates 53 bit resolution doubles in the half-open interval [0, 1)
class PseudoRandomFloat53Bit : public MersenneTwister {
public:
PseudoRandomFloat53Bit(BOOL useCurrentTime=TRUE) : MersenneTwister(useCurrentTime) {}
PseudoRandomFloat53Bit(unsigned long seed) : MersenneTwister(seed) {}
PseudoRandomFloat53Bit(const unsigned long* seed, int size) : MersenneTwister(seed, size) {}
~PseudoRandomFloat53Bit() {}
double operator()() {
return (static_cast<double>(GenerateRandom() >> 5) * 67108864. +
static_cast<double>(GenerateRandom() >> 6)) * (1. / 9007199254740992.); }
double Next() { return (*this)(); }
double Next(double mul) { return Next()*mul; }
private:
PseudoRandomFloat53Bit(const PseudoRandomFloat53Bit&); // copy constructor not defined
void operator=(const PseudoRandomFloat53Bit&); // assignment operator not defined
};
// typedef MersenneTwister PseudoRandom;
// Pseudorandom series(21301391);
// mtrand.h
// C++ include file for MT19937, with initialization improved 2002/1/26.
// Coded by Takuji Nishimura and Makoto Matsumoto.
// Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/).
// The generators returning floating point numbers are based on
// a version by Isaku Wada, 2002/01/09
//
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. The names of its contributors may not be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Any feedback is very welcome.
// http://www.math.keio.ac.jp/matumoto/emt.html
// email: matumoto@math.keio.ac.jp
//
// Feedback about the C++ port should be sent to Jasper Bedaux,
// see http://www.bedaux.net/mtrand/ for e-mail address and info.
}

View File

@@ -0,0 +1,63 @@
#include "stdafx.h"
#include "Random.h"
namespace Math
{
RandomInt::RandomInt()
{
SetSeed(GetTickCount());
}
RandomInt::RandomInt(unsigned long seed) : random(seed)
{
}
void RandomInt::SetSeed(unsigned long seed)
{
random.SetSeed(seed);
}
unsigned int RandomInt::Next()
{
return random.Next();
}
unsigned int RandomInt::Next(unsigned int excludedMax)
{
if(excludedMax == 0)
return excludedMax;
return Next(0, excludedMax);
}
unsigned int RandomInt::Next(unsigned int includedMin, unsigned int excludedMax)
{
if(includedMin == excludedMax)
return includedMin;
return random.Next(includedMin, excludedMax);
}
RandomDouble::RandomDouble()
{
SetSeed(GetTickCount());
}
RandomDouble::RandomDouble(unsigned long seed) : random(seed)
{
}
void RandomDouble::SetSeed(unsigned long seed)
{
random.SetSeed(seed);
}
double RandomDouble::Next()
{
return random.Next();
}
}

View File

@@ -0,0 +1,45 @@
#pragma once
#include "PseudoRandom.h"
namespace Math
{
class RandomInt
{
private:
PseudoRandomInt random;
// unable to copy
RandomInt(const RandomInt& );
void operator =(const RandomInt&);
public:
RandomInt();
RandomInt(unsigned long seed);
void SetSeed(unsigned long seed);
virtual unsigned int Next();
virtual unsigned int Next(unsigned int excludedMax);
virtual unsigned int Next(unsigned int includedMin, unsigned int excludedMax);
};
class RandomDouble
{
private:
PseudoRandomFloatClosed random;
// unable to copy
RandomDouble(const RandomDouble& );
void operator =(const RandomDouble&);
public:
RandomDouble();
RandomDouble(unsigned long seed);
void SetSeed(unsigned long seed);
virtual double Next(); // generates a float number [0, 1]
};
}