From bd2422678b1f8577db6d7562a60cea016456931e Mon Sep 17 00:00:00 2001 From: Toni Wilen Date: Sun, 6 Nov 2022 18:52:09 +0200 Subject: [PATCH] xorshift pseudo-random number generator --- main.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) 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; } -- 2.47.3