70 lines
1.3 KiB
C++
70 lines
1.3 KiB
C++
/* -----------------------------------------------------------------------------
|
|
GSFramework
|
|
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
|
----------------------------------------------------------------------------- */
|
|
|
|
|
|
#include "rand/rand.h"
|
|
|
|
using namespace GS;
|
|
|
|
|
|
#define __USE_CUSTOM_RAND 1
|
|
|
|
static uint high = 0xDEADBEEF, low = high ^ 0x49616E42;
|
|
static uint _rg_pn = 0;
|
|
|
|
//------------------------------------------------------------------------------
|
|
void Random::Seed(uint s)
|
|
{
|
|
#if __USE_CUSTOM_RAND
|
|
high = 0xDEADBEEF;
|
|
low = high ^ 0x49616E42;
|
|
for (uint n = 0; n < (s & 0x1fff); ++n)
|
|
Rand(1);
|
|
#else
|
|
srand(s);
|
|
#endif
|
|
}
|
|
uint Random::Rand(uint r)
|
|
{
|
|
if (!r)
|
|
return 0;
|
|
#if __USE_CUSTOM_RAND
|
|
high = (high << 16) + (high >> 16);
|
|
high += low;
|
|
low += high;
|
|
return high % r;
|
|
#else
|
|
return rand() % r;
|
|
#endif
|
|
}
|
|
float Random::FRand(float r)
|
|
{
|
|
return (float)Rand(65536) * r / 65536.0f;;
|
|
}
|
|
float Random::FRRand(float lo, float hi)
|
|
{
|
|
const float v = (float)(Rand(65536)) / 65536.0f;
|
|
return v * (hi - lo) + lo;
|
|
}
|
|
uint Random::CRand(uint r)
|
|
{
|
|
uint it;
|
|
uint _rg_cn = 0;
|
|
|
|
if (!r)
|
|
return 0;
|
|
|
|
it = 4;
|
|
while (it--)
|
|
{
|
|
_rg_cn = Rand(r);
|
|
if (_rg_cn != _rg_pn)
|
|
break;
|
|
}
|
|
_rg_pn = _rg_cn;
|
|
return _rg_cn;
|
|
}
|
|
//------------------------------------------------------------------------------
|