How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Factor programming language
How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Factor 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 Factor programming language
Source code in the factor programming language
USING: io kernel math math.bitwise math.functions
math.statistics namespaces prettyprint sequences ;
SYMBOL: state
: seed ( n -- ) 64 bits state set ;
: next-int ( -- n )
0x9e3779b97f4a7c15 state [ + 64 bits ] change
state get -30 0xbf58476d1ce4e5b9 -27 0x94d049bb133111eb -31 1
[ [ dupd shift bitxor ] dip * 64 bits ] 2tri@ ;
: next-float ( -- x ) next-int 64 2^ /f ;
! Test next-int
"Seed: 1234567; first five integer values" print
1234567 seed 5 [ next-int . ] times nl
! Test next-float
"Seed: 987654321; first 100,000 float values histogram" print
987654321 seed 100,000 [ next-float 5 * >integer ] replicate
histogram .
You may also check:How to resolve the algorithm Sorting algorithms/Strand sort step by step in the PHP programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Prolog programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the beeswax programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the E programming language