원하던 랜덤함수는 짧은시간내에도 다른 값을 뱉어내는 함수를 원했는데... 요함수는 서버랑 싱크를 맞출때 쓰는 함수인것같다..
C에서
#define W 32
#define R 16
#define P 0
#define M1 13
#define M2 9
#define M3 5
#define MAT0POS(t,v) (v^(v>>t))
#define MAT0NEG(t,v) (v^(v<<(-(t))))
#define MAT3NEG(t,v) (v<<(-(t)))
#define MAT4NEG(t,b,v) (v ^ ((v<<(-(t))) & b))
#define V0 STATE[state_i ]
#define VM1 STATE[(state_i+M1) & 0x0000000fU]
#define VM2 STATE[(state_i+M2) & 0x0000000fU]
#define VM3 STATE[(state_i+M3) & 0x0000000fU]
#define VRm1 STATE[(state_i+15) & 0x0000000fU]
#define VRm2 STATE[(state_i+14) & 0x0000000fU]
#define newV0 STATE[(state_i+15) & 0x0000000fU]
#define newV1 STATE[state_i ]
#define newVRm1 STATE[(state_i+14) & 0x0000000fU]
#define FACT 2.32830643653869628906e-10
static unsigned int state_i = 0;
static unsigned int STATE[R];
static unsigned int z0, z1, z2;
void InitWELLRNG512a (unsigned int *init){
int j;
state_i = 0;
for (j = 0; j < R; j++)
STATE[j] = init[j];
}
double WELLRNG512a (void){
z0 = VRm1;
z1 = MAT0NEG (-16,V0) ^ MAT0NEG (-15, VM1);
z2 = MAT0POS (11, VM2) ;
newV1 = z1 ^ z2;
newV0 = MAT0NEG (-2,z0) ^ MAT0NEG(-18,z1) ^ MAT3NEG(-28,z2) ^ MAT4NEG(-5,0xda442d24U,newV1) ;
state_i = (state_i + 15) & 0x0000000fU;
return ((double) STATE[state_i]) * FACT;
}
class Well512Random
{
protected:
unsigned int state[16];
unsigned int index;
public:
Well512Random(unsigned int nSeed)
{
index = 0;
unsigned int s = nSeed;
for (int i = 0; i < 16; i++)
{
state[i] = s;
s += s + 73;
}
}
unsigned int Next(int minValue, int maxValue)
{
return (unsigned int)((Next() % (maxValue - minValue)) + minValue);
}
unsigned int Next(unsigned int maxValue)
{
return Next() % maxValue;
}
unsigned int Next()
{
unsigned int a, b, c, d;
a = state[index];
c = state[(index + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = state[(index + 9) & 15];
c ^= (c >> 11);
a = state[index] = b ^ c;
d = a ^ ((a << 5) & 0xda442d24U);
index = (index + 15) & 15;
a = state[index];
state[index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
return state[index];
}
};
C#에서
public class Well512Random
{
uint[] state = new uint[16];
uint index = 0;
public Well512Random(uint nSeed)
{
uint s = nSeed;
for (int i = 0; i < 16; i++)
{
state[i] = s;
s += s + 73;
}
}
internal uint Next(int minValue, int maxValue)
{
return (uint)((Next() % (maxValue - minValue)) + minValue);
}
public uint Next(uint maxValue)
{
return Next() % maxValue;
}
public uint Next()
{
uint a, b, c, d;
a = state[index];
c = state[(index + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = state[(index + 9) & 15];
c ^= (c >> 11);
a = state[index] = b ^ c;
d = a ^ ((a << 5) & 0xda442d24U);
index = (index + 15) & 15;
a = state[index];
state[index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
return state[index];
}
}
'개발' 카테고리의 다른 글
hyper V / CentOS / MySQL + Node js + express 설치 (0) | 2014.05.29 |
---|---|
CentOS[Linux] Node js+express 설치시 bash : express : command not found 일때 (0) | 2014.05.29 |
STL List 등에서 순차삭제 (0) | 2014.04.30 |
GetFileSize 파일 용량 측정 및 단위 생성 (0) | 2014.04.29 |
GetFileName (0) | 2014.04.29 |