In: Computer Science
Task 2: Random Number Generator (10 Pts)
Random numbers are usually computed using a formula from discrete mathematics. This type of random number generator is called the linear congruential generator and it is described by the following formula
Xn+1=(aXn+c)modm
where a, c and m are parameters of the generator, with Xn being the previous random number, and Xn+1 being the next random number. Variable X0 is referred to as the seed. Note: this is integer arithmetic, not floating-point arithmetic. Term mod is symbol % in Python.
This algorithm is used extensively throughout the computing world, e.g., the random number generator implemented in the C programming language, and available in the glibc library, has parametric values of
a = 1,103,515,245 c = 12,345 m=231 -1
Numerical random number generators are not random; they are deterministic! Given a seed, the sequence of random numbers that follows will be the same. Such methods are referred to as being pseudo-random, because they seem to behave as randomly generated, but are reproducible given the starting condition.
In practice, one selects different seeds with each application that is run that relies on a random number generator, like for a monte carlo simulation. What I typically do in these cases is to set the seed (first call to the generator) using the current
time stamp supplied by the operating system, expressed as an
integer.
Write a Python function
def randomNbr(rnPrev):
that implements the above algorithm. Here rnPrev is Xn in the above
formula. Then write a script
def runTask2():
that calls this function five successive times, where the returned random number from the prior call becomes the argument rnPrev in the next call to the generator, with the first call having an argument of 543,210. Print these integers out to the command window using the following format:
random number 1 is <computed random number> random number 2 is <computed random number> random number 3 is <computed random number> random number 4 is <computed random number> random number 5 is <computed random number>
where the first call to the random number generator uses 543,210 as rnPrev.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#method to generate next pseudo random number given
previous
#value
def randomNbr(rnPrev):
#defining parameters a,b and c
a=1103515245
c=12345
m=230
#generating next number using the given
equation
nextNum=(a*rnPrev +c)%m
#returning it
return nextNum
#method to execute the above method 5 times
def runTask2():
#using 543210 as starting value
current=543210
#looping for 5 times (i=1 to i=5)
for i in
range(1,6):
#using current
number, generating next number, assigning to current
#itself.
current=randomNbr(current)
#printing the
generated value
print("random number {} is
{}".format(i,current))
#running runTask2()
runTask2()
#output
random number 1 is 45
random number 2 is 70
random number 3 is 125
random number 4 is 200
random number 5 is 135