From: Toni Wilen Date: Sun, 6 Nov 2022 16:52:09 +0000 (+0200) Subject: xorshift pseudo-random number generator X-Git-Tag: 41000~89 X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=bd2422678b1f8577db6d7562a60cea016456931e;p=francis%2Fwinuae.git xorshift pseudo-random number generator --- diff --git a/main.cpp b/main.cpp index 318109f5..10f1cf33 100644 --- a/main.cpp +++ b/main.cpp @@ -68,25 +68,33 @@ TCHAR optionsfile[256]; static uae_u32 randseed; static int oldhcounter; -uae_u32 uaesrand (uae_u32 seed) +static uae_u32 xorshiftstate = 1; +static uae_u32 xorshift32(void) +{ + uae_u32 x = xorshiftstate; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + xorshiftstate = x; + return xorshiftstate; +} + +uae_u32 uaesrand(uae_u32 seed) { oldhcounter = -1; randseed = seed; - //randseed = 0x12345678; - //write_log (_T("seed=%08x\n"), randseed); return randseed; } -uae_u32 uaerand (void) +uae_u32 uaerand(void) { if (oldhcounter != hsync_counter) { - srand (hsync_counter ^ randseed); + xorshiftstate = (hsync_counter ^ randseed) | 1; oldhcounter = hsync_counter; } - uae_u32 r = rand (); - //write_log (_T("rand=%08x\n"), r); + uae_u32 r = xorshift32(); return r; } -uae_u32 uaerandgetseed (void) +uae_u32 uaerandgetseed(void) { return randseed; }