How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Go programming language
How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Go 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 Go programming language
- The code implements a Splitmix64 PRNG (Pseudo Random Number Generator) in Go.
- It defines a type
Splitmix64
that has astate
field of typeuint64
and two methods:nextInt
andnextFloat
. - The
nextInt
method generates a random integer by incrementing thestate
by a constant value, performing some bitwise operations on the result, and returning the final result. - The
nextFloat
method generates a random float by callingnextInt
and dividing the result by(1 << 64)
. - In the
main
function, aSplitmix64
PRNG is initialized with a seed of1234567
andnextInt
is called five times to print five random integers. - Then, a new
Splitmix64
PRNG is initialized with a seed of987654321
andnextFloat
is called1e5
times to generate a sequence of random floats between0
and1
. - The generated floats are used to generate five integers between
0
and4
by taking the floor of the float multiplied by5
, and the counts of each integer are incremented. - Finally, the counts are printed to the console to show the distribution of the generated integers.
Source code in the go programming language
package main
import (
"fmt"
"math"
)
type Splitmix64 struct{ state uint64 }
func Splitmix64New(state uint64) *Splitmix64 { return &Splitmix64{state} }
func (sm64 *Splitmix64) nextInt() uint64 {
sm64.state += 0x9e3779b97f4a7c15
z := sm64.state
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9
z = (z ^ (z >> 27)) * 0x94d049bb133111eb
return z ^ (z >> 31)
}
func (sm64 *Splitmix64) nextFloat() float64 {
return float64(sm64.nextInt()) / (1 << 64)
}
func main() {
randomGen := Splitmix64New(1234567)
for i := 0; i < 5; i++ {
fmt.Println(randomGen.nextInt())
}
var counts [5]int
randomGen = Splitmix64New(987654321)
for i := 0; i < 1e5; i++ {
j := int(math.Floor(randomGen.nextFloat() * 5))
counts[j]++
}
fmt.Println("\nThe counts for 100,000 repetitions are:")
for i := 0; i < 5; i++ {
fmt.Printf(" %d : %d\n", i, counts[i])
}
}
You may also check:How to resolve the algorithm Long multiplication step by step in the APL programming language
You may also check:How to resolve the algorithm String length step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Jakt programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the Python programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the FreeBASIC programming language