Question: Implement Python programs that can find the cycle length of a linear congruential random number generator, using Floyd's algorithm. The terms in the problem statement
Implement Python programs that can find the cycle length of a linear congruential
random number generator, using Floyd's algorithm.
The terms in the problem statement are likely to be unfamiliar to you, but they are not
difficult to understand and are described in detail below. In the end, this assignment
involves only a few lines of Python code. In order to complete it you need to be able
to write loops for and while and branch statements ifelse in Python.
For the purposes of this assignment, a linear congruential random number
generator is defined in terms of four integers: the multiplicative constant a the
additive constant the starting point or seed and the modulus m The purpose of the
generator is to produce a sequence of integers between and by starting with
and iterating:
In Python, the operator means modulus or remainder: this keeps the iterates
between and M For example, the following table shows the sequences that result
for various choices of and M
As with the linear feedback shift register, these sequences are not random, but for
proper choices of the parameters they exhibit many properties of random sequences.
For small values of the parameters, the nonrandomness is particularly evident:
whenever we generate a value that we have seen before, we enter into a cycle, where
we continually generate the same subsequence over and over again.
In the first two examples above, the cycles are and
of lengths and respectively.
Since there are only M different possibilities, we always must enter such a cycle, but
we want to avoid short cycles.
Your first task is to write a program to print out the sequence for small M Your
second task is to write a program to compute the cycle length for any M
Part a: print iterates. Write a Python program that reads in four integers a b c
and M in this order and prints out the first values produced by the linear
congruential random number generator for these parameters. Use a for loop and the
following update formula:
&
Part b: print iterates nicely. Print the iterates in a nice table with values per
line and four characters per iterate. Use an if statement with the operator to print a
newline character after every iterations. Use an appropriate print formatting
instruction to get four characters per iterate. Assume that M is or less so that
this is a sufficient amount of space.
Name your program trace. Verify that it behaves as follows:
Perspective. In practice, we often want random integers in a small range, say between
and Typically we do so by using a large M in a linear congruential random
number generator, and then use arithmetic to bring them down into a small range. For
example, the leading digits of the first terms in the sequence above are:
which is a randomlooking sequence of integers between and although the pattern
repeats itself every iterations But if we use this method for the last example
above, we get stuck in the cycle
which is not a very randomlooking sequence. This phenomenon can happen even for
huge M so we are interested in knowing that our choice of parameters does not lead
to a small cycle.
Floyd's algorithm. The second part of this assignment is to implement a method
known as Floyd's algorithm for finding the cycle length or period. Floyd's algorithm
consists of two phases: i find a point on the cycle, and ii compute the cycle length.
Phase I. To find a point on the cycle, we use the following clever idea: run two
versions of the generator concurrently, one iterating twice as fast as the other,
until both versions have the same value. At some point, both of them are on the
cycle and we have a race on the cycle, with the gap between the faster one and
the slower one shrinking by one on each iteration. Eventually, the faster one
catches up to the slower one, and the two generators are at the same point on
the cycle.
Above is an example of the process with and In
this example, the cycle is and its length is
Somewhat coincidentally, this happens to be equal to the number of steps until
the fast and slow generators first meet, but this will not be true in general. The
fast generator happens to be at when the slow one first hits the cycle at
The fast one is six steps behind the slow one, and catches up one step at a time.
Phase II Once we know a value on the cycle, one more trip around th
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
