How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Julia programming language
How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Julia 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 Julia programming language
The provided code implements a 32-bit random number generator (RNG) using the PCG32 algorithm. It defines a PCG32
struct to hold the state and increment values for the generator.
Initialization and Seeding:
- The constructor
PCG32(st, i)
initializes thePCG32
with optional seed valuesst
andi
. By default, it uses pre-defined constants (0x853c49e6748fea9b forst
and 0xda3e39cb94b95bdb fori
).
Random Number Generation:
-
The
next_int!
function generates a random 32-bit unsigned integer based on the current state and increment values. It uses bitwise operations to produce the random number and updates the state for subsequent calls. -
The
next_float!
function generates a random floating-point number between 0 and 1 by dividing the generated integer by 2^32. -
The
seed!
function allows you to initialize or reseed the generator with custom seed values.
Testing and Demonstration:
-
The
testPCG32
function demonstrates the usage of the RNG. It creates an instance, seeds it with specific values, and generates and prints random integers. -
It then re-seeds the generator and generates a histogram of 100_000 random floats divided into 5 bins. The histogram and bin counts are printed for analysis.
Overall, this code provides a simple and efficient implementation of the PCG32 random number generator in Julia, allowing for both integer and floating-point random number generation. The testing function demonstrates its functionality and provides insights into the distribution of generated numbers through a histogram.
Source code in the julia programming language
const mask32, CONST = 0xffffffff, UInt(6364136223846793005)
mutable struct PCG32
state::UInt64
inc::UInt64
PCG32(st=0x853c49e6748fea9b, i=0xda3e39cb94b95bdb) = new(st, i)
end
"""return random 32 bit unsigned int"""
function next_int!(x::PCG32)
old = x.state
x.state = (old * CONST) + x.inc
xorshifted = (((old >> 18) ⊻ old) >> 27) & mask32
rot = (old >> 59) & mask32
return ((xorshifted >> rot) | (xorshifted << ((-rot) & 31))) & mask32
end
"""return random float between 0 and 1"""
next_float!(x::PCG32) = next_int!(x) / (1 << 32)
function seed!(x::PCG32, st, seq)
x.state = 0x0
x.inc = (UInt(seq) << 0x1) | 1
next_int!(x)
x.state = x.state + UInt(st)
next_int!(x)
end
function testPCG32()
random_gen = PCG32()
seed!(random_gen, 42, 54)
for _ in 1:5
println(next_int!(random_gen))
end
seed!(random_gen, 987654321, 1)
hist = fill(0, 5)
for _ in 1:100_000
hist[Int(floor(next_float!(random_gen) * 5)) + 1] += 1
end
println(hist)
for n in 1:5
print(n - 1, ": ", hist[n], " ")
end
end
testPCG32()
You may also check:How to resolve the algorithm Create an HTML table step by step in the 11l programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Ring programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Lasso programming language
You may also check:How to resolve the algorithm Maze generation step by step in the Commodore BASIC programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the BBC BASIC programming language