How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the F# programming language

Table of Contents

Problem Statement

Splitmix64 is the default pseudo-random number generator algorithm in Java and is included / available in many other languages. It uses a fairly simple algorithm that, though it is considered to be poor for cryptographic purposes, is very fast to calculate, and is "good enough" for many random number needs. It passes several fairly rigorous PRNG "fitness" tests that some more complex algorithms fail. Splitmix64 is not recommended for demanding random number requirements, but is often used to calculate initial states for other more complex pseudo-random number generators. The "standard" splitmix64 maintains one 64 bit state variable and returns 64 bits of random data with each call. Basic pseudocode algorithm: The returned value should hold 64 bits of numeric data. If your language does not support unsigned 64 bit integers directly you may need to apply appropriate bitmasks during bitwise operations. In keeping with the general layout of several recent pseudo-random number tasks:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the F# programming language

Source code in the fsharp programming language

// Pure F# Implementation of SplitMix64
let a: uint64 = 0x9e3779b97f4a7c15UL

let nextInt (state: uint64) =
    let newstate = state + (0x9e3779b97f4a7c15UL)
    let rand = newstate
    let rand = (rand ^^^ (rand >>> 30)) * 0xbf58476d1ce4e5b9UL
    let rand = (rand ^^^ (rand >>> 27)) * 0x94d049bb133111ebUL
    let rand = rand ^^^ (rand >>> 31)
    (rand, newstate)

let nextFloat (state: uint64) =
    let (rand, newState) = nextInt state
    let randf = (rand / (1UL <<< 64)) |> float
    (randf, newState)

[<EntryPoint>]
let main argv =
    let state = 1234567UL
    let (first, state) = nextInt state
    let (second, state) = nextInt state
    let (third, state) = nextInt state
    let (fourth, state) = nextInt state
    let (fifth, state) = nextInt state
    printfn "%i" first
    printfn "%i" second
    printfn "%i" third
    printfn "%i" fourth
    printfn "%i" fifth
    0


  

You may also check:How to resolve the algorithm Thue-Morse step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the Wren programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the zkl programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Factor programming language
You may also check:How to resolve the algorithm 15 puzzle solver step by step in the Picat programming language