//--------------------------------------------------------------------------- #include #include #include #pragma hdrstop #include "TexpiaRandom.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- int TTexpiaRandom::Instance = 0; unsigned char *TTexpiaRandom::Index = NULL; int *TTexpiaRandom::Value = NULL; int TTexpiaRandom::Count = 0; //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- void __fastcall TTexpiaRandom::Exit() { if (Value) { delete Value; Value = NULL; } if (Index) { delete Index; Index = NULL; } Count = 0; } //--------------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- __fastcall TTexpiaRandom::TTexpiaRandom(int c) { Instance++; if (Count) Seed(); else Init(c); } //--------------------------------------------------------------------------- __fastcall TTexpiaRandom::~TTexpiaRandom() { Instance--; if (Instance==0) Exit(); } //--------------------------------------------------------------------------- bool __fastcall TTexpiaRandom::Init(int c) { c = 1 << c; if (Count) Exit(); if ((Index = new unsigned char[c])==NULL) goto fail; if ((Value = new int[c])==NULL) goto fail; Count = c-1; Seed(); return true; fail: Exit(); return false; } //--------------------------------------------------------------------------- void __fastcall TTexpiaRandom::Seed() { srand(clock()); int *vp = Value; unsigned char *ip = Index; for (int i=0; i<=Count; i++) { *vp++ = rand(); *ip++ = rand()&0xFF; } vn = rand()&Count; in = rand()&Count; } //--------------------------------------------------------------------------- int __fastcall TTexpiaRandom::Get(int r) { in++; in &= Count; vn += Index[in]; vn &= Count; return Value[vn]%r; } //---------------------------------------------------------------------------