xorshift32() has a short random sequence of states. It's only 32bits. You can see this in the random walk test. Wait a few minutes(on a i5 3470) and you will see a fractal like pattern.

Code: Select all

`'XorShift PRNG variations`

'Translated from https://en.wikipedia.org/wiki/Xorshift

'seeds must be initialize other than zero or they will fail.

type u128longint

a as ulongint

b as ulongint

end type

type u128int

a as ulong

b as ulong

c as ulong

d as ulong

end type

type xorwow128

a as ulong

b as ulong

c as ulong

d as ulong

e as ulong 'counter

end type

declare function xorshift32(byref seed as ulong) as ulong

declare function xorshift64(byref seed as ulongint) as ulongint

declare function xorshift64s(byref seed as ulongint) as ulongint

declare function xorshift128(byref seed as u128int) as ulong

declare function xorshift128p(byref seed as u128longint) as ulongint

declare function xorwow128(byref seed as xorwow128) as ulong

dim as ulong s32

dim as ulongint s64

dim as u128int s128

dim as u128longint p128

dim as xorwow128 w128

'initialize the seeds with some random integer (zero will fail). use timer for different seeds when progam starts.

s32 = 6765

s64 = 1456

s128.a = 4567

s128.b = 4635656

s128.c = 3485789

s128.d = 43588923

w128.a = 3405

p128.a = 4395879

const sx = 1280 'screeen resolution

const sy = 720

const zoomfactor = 7' sets the zoom out factor for random walk (2^zoomfactor)

dim as integer x, y

screenres sx, sy, 32, 1

'random point test

do

x = xorshift32(s32) mod sx

y = xorshift32(s32) mod sy

pset (x,y), rgb(255,255,255)

loop until inkey <> ""

cls

'random walk test

x = (sx \ 2) shl zoomfactor 'start at center of screen

y = (sy \ 2) shl zoomfactor

do

x = x + xorshift32(s32) mod 3 - 1

y = y + xorshift32(s32) mod 3 - 1

pset (x shr zoomfactor,y shr zoomfactor), rgb(255,255,255)

loop until inkey <> ""

function xorshift32(byref seed as ulong) as ulong

seed = seed xor (seed shr 13)

seed = seed xor (seed shl 17)

seed = seed xor (seed shr 5)

return seed

end function

function xorshift64(byref seed as ulongint) as ulongint

seed = seed xor (seed shr 13)

seed = seed xor (seed shl 7)

seed = seed xor (seed shr 17)

return seed

end function

function xorshift64s(byref seed as ulongint) as ulongint

seed = seed xor (seed shr 12)

seed = seed xor (seed shl 25)

seed = seed xor (seed shr 27)

return seed * &h2545F4914F6CDD1D

end function

function xorshift128(byref seed as u128int) as ulong

' Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" */

dim as ulong t = seed.d

dim as ulong s = seed.a

seed.d = seed.c

seed.c = seed.b

seed.b = s

t = t xor (t shr 11)

t = t xor (t shl 8)

seed.a = (t xor s) xor (s shl 19)

return seed.a

end function

function xorwow128(byref seed as xorwow128) as ulong

' Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs" */

dim as ulong t = seed.d

dim as ulong s = seed.a

seed.d = seed.c

seed.c = seed.b

seed.b = s

t = t xor (t shl 2)

t = t xor (t shr 1)

t = (t xor s) xor (s shr 4)

seed.a = t

seed.e = seed.e + 362437

return t + seed.e

end function

function xorshift128p(byref seed as u128longint) as ulongint

dim as ulongint t = seed.a

dim as ulongint s = seed.b

seed.a = s

t = t xor (t shl 23)

t = t xor (t shr 17)

t = t xor s xor (s shr 26)

seed.b = t

return t + s

end function