Home
Insert
Update snippet 'PCG 32bit RNG'
Title
Description
I have tested this against the C code I compiled on the PCG website (http://www.pcg-random.org/download.html). It seems to work fine. Nothing special, I just really loathe System.Random. Why has the de-facto RNG for the .NET library not been updated?
Source code
module Rng = let randPcg32 (initState: uint64) (initInc: uint64) = let mutable state = initState let mutable inc = initInc let rand () = let oldState = state state <- oldState * 6364136223846793005UL + (inc ||| 1UL) let xorshifted = (uint32)(((oldState >>> 18) ^^^ oldState) >>> 27) let rot = (uint32)(oldState >>> 59) (xorshifted >>> (int32)rot) ||| (xorshifted <<< ((-(int32)rot) &&& 31)) let next () = let x = float32 (rand()) x * 2.0f**(-32.0f) next
Tags
random
rng
pcg
random
rng
pcg
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