3 people like it.

PCG 32bit RNG

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?

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
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
val randPcg32 : initState:uint64 -> initInc:uint64 -> (unit -> float32)

Full name: Script.Rng.randPcg32
val initState : uint64
Multiple items
val uint64 : value:'T -> uint64 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.uint64

--------------------
type uint64 = System.UInt64

Full name: Microsoft.FSharp.Core.uint64
val initInc : uint64
val mutable state : uint64
val mutable inc : uint64
val rand : (unit -> uint32)
val oldState : uint64
val xorshifted : uint32
Multiple items
val uint32 : value:'T -> uint32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.uint32

--------------------
type uint32 = System.UInt32

Full name: Microsoft.FSharp.Core.uint32
val rot : uint32
Multiple items
val int32 : value:'T -> int32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int32

--------------------
type int32 = System.Int32

Full name: Microsoft.FSharp.Core.int32
val next : (unit -> float32)
val x : float32
Multiple items
val float32 : value:'T -> float32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float32

--------------------
type float32 = System.Single

Full name: Microsoft.FSharp.Core.float32

--------------------
type float32<'Measure> = float32

Full name: Microsoft.FSharp.Core.float32<_>
Raw view Test code New version

More information

Link:http://fssnip.net/7W3
Posted:5 years ago
Author:Krunoslav Saho
Tags: random , rng , pcg