How to resolve the algorithm Linear congruential generator step by step in the Logo programming language
How to resolve the algorithm Linear congruential generator step by step in the Logo programming language
Table of Contents
Problem Statement
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula:
Where:
If one chooses the values of
a
{\displaystyle a}
,
c
{\displaystyle c}
and
m
{\displaystyle m}
with care, then the generator produces a uniform distribution of integers from
0
{\displaystyle 0}
to
m − 1
{\displaystyle m-1}
. LCG numbers have poor quality.
r
n
{\displaystyle r_{n}}
and
r
n + 1
{\displaystyle r_{n+1}}
are not independent, as true random numbers would be. Anyone who knows
r
n
{\displaystyle r_{n}}
can predict
r
n + 1
{\displaystyle r_{n+1}}
, therefore LCG is not cryptographically secure. The LCG is still good enough for simple tasks like Miller-Rabin primality test, or FreeCell deals. Among the benefits of the LCG, one can easily reproduce a sequence of numbers, from the same
r
0
{\displaystyle r_{0}}
. One can also reproduce such sequence with a different programming language, because the formula is so simple. The task is to replicate two historic random number generators. One is the rand() function from BSD libc, and the other is the rand() function from the Microsoft C Runtime (MSCVRT.DLL). Each replica must yield the same sequence of integers as the original generator, when starting from the same seed. In these formulas, the seed becomes
s t a t
e
0
{\displaystyle state_{0}}
. The random sequence is
r a n
d
1
{\displaystyle rand_{1}}
,
r a n
d
2
{\displaystyle rand_{2}}
and so on.
The BSD formula was so awful that FreeBSD switched to a different formula. More info is at Random number generator (included)#C.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Linear congruential generator step by step in the Logo programming language
Source code in the logo programming language
; Configuration parameters for Microsoft and BSD implementations
make "LCG_MS [214013 2531011 65536 2147483648]
make "LCG_BSD [1103515245 12345 1 2147483648]
; Default seed is 0
make "_lcg_value 0
; set the seed
to lcg_seed :seed
make "_lcg_value :seed
end
; generate the next number in the series using the given parameters
to lcg_rand [:config :LCG_MS]
local "a local "c local "d local "m
foreach [a c d m] [
make ? item # :config
]
make "_lcg_value (modulo (sum (product :a :_lcg_value) :c) :m)
output int quotient :_lcg_value :d
end
foreach (list :LCG_BSD :LCG_MS) [
lcg_seed 0
repeat 10 [
print (lcg_rand ?)
]
print []
]
bye
You may also check:How to resolve the algorithm Ackermann function step by step in the Scheme programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the DWScript programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Pascal Delphi Free Pascal programming language