Home
Insert
Update snippet 'xorshift128plus PRNG that mimics System.Random'
Title
Description
Faster with much better quality random numbers than System.Random. See https://en.wikipedia.org/wiki/Xorshift
Source code
type XorshiftPRNG(seed) = let mutable s : uint64[] = Array.zeroCreate 2 do s.[1] <- uint64 seed let sample() = let mutable x = s.[0] let y = s.[1] s.[0] <- y x <- x ^^^ (x <<< 23) s.[1] <- x ^^^ y ^^^ (x >>> 17) ^^^ (y >>> 26) let smpl = s.[1] + y if smpl = System.UInt64.MaxValue then smpl - 1UL else smpl member x.NextDouble() = (float (sample())) / float System.UInt64.MaxValue member x.Next(max) = if max < 0 then failwith "max < 0" x.NextDouble() * (float max) |> int member x.Next(min:int,max:int) = if min > max then failwith "min > max" let r = max - min in (float r) * (x.NextDouble()) + (float min) |> int new()=XorshiftPRNG(System.Environment.TickCount)
Tags
prng, pseudo-random number generator, random
prng, pseudo-random number generator, random
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update