0 people like it.

xorshift128plus PRNG that mimics System.Random

Faster with much better quality random numbers than System.Random. See https://en.wikipedia.org/wiki/Xorshift

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
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)
Multiple items
type XorshiftPRNG =
  new : unit -> XorshiftPRNG
  new : seed:int -> XorshiftPRNG
  member Next : max:int -> int
  member Next : min:int * max:int -> int
  member NextDouble : unit -> float

Full name: Script.XorshiftPRNG

--------------------
new : unit -> XorshiftPRNG
new : seed:int -> XorshiftPRNG
val seed : int
val mutable s : 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
module Array

from Microsoft.FSharp.Collections
val zeroCreate : count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
val sample : (unit -> uint64)
val mutable x : uint64
val y : uint64
val smpl : uint64
namespace System
type UInt64 =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 3 overloads
    static val MaxValue : uint64
    static val MinValue : uint64
    static member Parse : s:string -> uint64 + 3 overloads
    static member TryParse : s:string * result:uint64 -> bool + 1 overload
  end

Full name: System.UInt64
field uint64.MaxValue = 18446744073709551615UL
val x : XorshiftPRNG
member XorshiftPRNG.NextDouble : unit -> float

Full name: Script.XorshiftPRNG.NextDouble
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
member XorshiftPRNG.Next : max:int -> int

Full name: Script.XorshiftPRNG.Next
val max : int
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
member XorshiftPRNG.NextDouble : unit -> float
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
member XorshiftPRNG.Next : min:int * max:int -> int

Full name: Script.XorshiftPRNG.Next
val min : int
val r : int
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 1 overload
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption

Full name: System.Environment
property System.Environment.TickCount: int
Raw view Test code New version

More information

Link:http://fssnip.net/7Rb
Posted:7 years ago
Author:Faisal Waris
Tags: prng, pseudo-random number generator, random