How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Factor programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Factor programming language
Table of Contents
Problem Statement
PCG32 has two unsigned 64-bit integers of internal state:
Values of sequence allow 2**63 different sequences of random numbers from the same state.
The algorithm is given 2 U64 inputs called seed_state, and seed_sequence. The algorithm proceeds in accordance with the following pseudocode:-
Note that this an anamorphism – dual to catamorphism, and encoded in some languages as a general higher-order unfold
function, dual to fold
or reduce
.
numbers using the above.
are: 2707161783 2068313097 3122475824 2211639955 3215226955
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Factor programming language
Source code in the factor programming language
USING: accessors kernel locals math math.bitwise math.statistics
prettyprint sequences ;
CONSTANT: const 6364136223846793005
TUPLE: pcg32 state inc ;
: <pcg32> ( -- pcg32 )
0x853c49e6748fea9b 0xda3e39cb94b95bdb pcg32 boa ;
:: next-int ( pcg -- n )
pcg state>> :> old
old const * pcg inc>> + 64 bits pcg state<<
old -18 shift old bitxor -27 shift 32 bits :> shifted
old -59 shift 32 bits :> r
shifted r neg shift
shifted r neg 31 bitand shift bitor 32 bits ;
: next-float ( pcg -- x ) next-int 1 32 shift /f ;
:: seed ( pcg st seq -- )
0x0 pcg state<<
seq 0x1 shift 1 bitor 64 bits pcg inc<<
pcg next-int drop
pcg state>> st + pcg state<<
pcg next-int drop ;
! Task
<pcg32> 42 54 [ seed ] keepdd 5 [ dup next-int . ] times
987654321 1 [ seed ] keepdd
100,000 [ dup next-float 5 * >integer ] replicate nip
histogram .
You may also check:How to resolve the algorithm Sparkline in unicode step by step in the Sidef programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the REXX programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Nim programming language
You may also check:How to resolve the algorithm 21 game step by step in the Arturo programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Prolog programming language