How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Haskell programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Haskell programming language
Table of Contents
Problem Statement
numbers as shown above.
are as shown above
repetitions of
Is as follows:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Haskell programming language
The provided code has two main parts:
-
The
randoms
Function:- The
randoms
function generates a list of random integers using the Multiply-with-Carry (MWC) algorithm, which is a non-cryptographic pseudorandom number generator. - It takes an integer seed as an argument and uses a fixed set of constants
a1
,a2
,m1
, andm2
to generate the random numbers. - The function uses the
unfoldr
function from theData.List
module, which repeatedly applies a function to a seed value to generate a list. - The function also converts the generated integers to floating-point values using the
randomsFloat
function.
- The
-
The
MRG32k3a
Custom Random Generator:- The
MRG32k3a
type is a newtype that represents a custom random generator. - It takes a pair of lists,
[Int]
s, which are used as the internal state of the generator. - The
mkMRG32k3a
function creates anMRG32k3a
generator with a given seed. MRG32k3a
is an instance of theRandomGen
type class, which provides the functionality for generating random numbers.- The
next
function generates the next random value and updates the internal state of the generator. - The
split
function is not implemented, which means that this generator cannot be split into multiple independent generators.
- The
Source code in the haskell programming language
import Data.List
randoms :: Int -> [Int]
randoms seed = unfoldr go ([seed,0,0],[seed,0,0])
where
go (x1,x2) =
let x1i = sum (zipWith (*) x1 a1) `mod` m1
x2i = sum (zipWith (*) x2 a2) `mod` m2
in Just $ ((x1i - x2i) `mod` m1, (x1i:init x1, x2i:init x2))
a1 = [0, 1403580, -810728]
m1 = 2^32 - 209
a2 = [527612, 0, -1370589]
m2 = 2^32 - 22853
randomsFloat = map ((/ (2^32 - 208)) . fromIntegral) . randoms
import System.Random
newtype MRG32k3a = MRG32k3a ([Int],[Int])
mkMRG32k3a s = MRG32k3a ([s,0,0],[s,0,0])
instance RandomGen MRG32k3a where
next (MRG32k3a (x1,x2)) =
let x1i = sum (zipWith (*) x1 a1) `mod` m1
x2i = sum (zipWith (*) x2 a2) `mod` m2
in ((x1i - x2i) `mod` m1, MRG32k3a (x1i:init x1, x2i:init x2))
where
a1 = [0, 1403580, -810728]
m1 = 2^32 - 209
a2 = [527612, 0, -1370589]
m2 = 2^32 - 22853
split _ = error "MRG32k3a is not splittable"
You may also check:How to resolve the algorithm Secure temporary file step by step in the REXX programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Clojure programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the REXX programming language
You may also check:How to resolve the algorithm File input/output step by step in the Clojure programming language