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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "/big" for BigInt

var Const1 = BigInt.fromBaseString("9e3779b97f4a7c15", 16) 
var Const2 = BigInt.fromBaseString("bf58476d1ce4e5b9", 16) 
var Const3 = BigInt.fromBaseString("94d049bb133111eb", 16)
var Mask64 = (BigInt.one << 64) - BigInt.one

class Splitmix64 {
    construct new(state) {
        _state  = state
    }

    nextInt {
        _state = (_state + Const1) & Mask64
        var z = _state
        z = ((z ^ (z >> 30)) * Const2) & Mask64
        z = ((z ^ (z >> 27)) * Const3) & Mask64
        return (z ^ (z >> 31)) & Mask64
    }

    nextFloat { nextInt.toNum / 2.pow(64) }
}

var randomGen = Splitmix64.new(BigInt.new(1234567))
for (i in 0..4) System.print(randomGen.nextInt)

var counts = List.filled(5, 0)
randomGen = Splitmix64.new(BigInt.new(987654321))
for (i in 1..1e5) {
    var i = (randomGen.nextFloat * 5).floor
    counts[i] = counts[i] + 1
}
System.print("\nThe counts for 100,000 repetitions are:")
for (i in 0..4) System.print("  %(i) : %(counts[i])")

  

You may also check:How to resolve the algorithm Unicode variable names step by step in the Stata programming language
You may also check:How to resolve the algorithm Count the coins step by step in the VBScript programming language
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the ATS programming language
You may also check:How to resolve the algorithm Combinations step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Ordered words step by step in the APL programming language