Question

In: Computer Science

Task 2: Random Number Generator (10 Pts) Random numbers are usually computed using a formula from...

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.

Solutions

Expert Solution

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


Related Solutions

Use a random number generator to produce 1000 uniformly distributed numbers with a mean of 10, a
Use a random number generator to produce 1000 uniformly distributed numbers with a mean of 10, a minimum of 2, and a maximum of 18. Obtain the mean and the histogram of these numbers, and discuss whether they appear uniformly distributed with the desired mean.
Questions 36-39: Six numbers are selected at random from a random number generator and entered into...
Questions 36-39: Six numbers are selected at random from a random number generator and entered into each of four rows. The summary statistics are presented in the chart below. Mean SD   Row 1 5.8  0.78 Row 2 3.6  0.23 Row 3 5.5  0.32 Row 4 4.0  0.59 Assume the populations are normal with equal variances. It is of interest to test the following: Ho: μ1 = μ2 = μ3 = μ4 36. What are the degrees of freedom for the...
1.) Using excel. A random number generator picks a number from one to nine in a...
1.) Using excel. A random number generator picks a number from one to nine in a uniform manner. X ~ _________ Graph the probability distribution. f(x) = _________ μ = _________ σ = _________ P(3.5 < x < 7.25) = _________ P(x > 5.67) P(x > 5|x > 3) = _________ Find the 90th percentile. 2) using excel A subway train on the Red Line arrives every eight minutes during rush hour. We are interested in the length of time...
Below are 50 random numbers taken from a random number generator. 0.534 0.401 0.401 0.445 0.445...
Below are 50 random numbers taken from a random number generator. 0.534 0.401 0.401 0.445 0.445 0.125 0.094 0.094 0.104 0.104 0.345 0.259 0.259 0.288 0.288 0.785 0.589 0.589 0.654 0.654 0.975 0.731 0.731 0.813 0.813 0.834 0.626 0.626 0.695 0.695 0.683 0.512 0.512 0.569 0.569 0.322 0.242 0.242 0.268 0.268 0.526 0.395 0.395 0.438 0.438 0.234 0.176 0.177 0.198 0.196 a) Using 5 class intervals, determine the computed chi-square value. Answer in 1 decimal place.    b) What is...
The Random class implements a random number generator, which produces sequences of numbers that appear to...
The Random class implements a random number generator, which produces sequences of numbers that appear to be random. To generate random integers, you construct an object of the Random class, and then apply the nextInt method. For example, the call generator.nextInt(6) gives you a random number between 0 and 5. Write a program DieSimulator that uses the Random class to simulate the cast of a die, printing a random number between 1 and 6 every time that the program is...
Suppose you have access to a random number generator Rng() that generates uniform random numbers in...
Suppose you have access to a random number generator Rng() that generates uniform random numbers in {0, 1, . . . , n − 1}. Design a function uses Rng() generate a uniform random number in {0, 1, . . . , m − 1}, where m ≤ n
Use a random number generator to produce 1000 normally distributed numbers with a mean of 20
Use a random number generator to produce 1000 normally distributed numbers with a mean of 20 and a variance of 4. Obtain the mean, variance, and histogram of these numbers, and discuss whether they appear normally distributed with the desired mean and variance.
We usually write numbers in decimal form (or base 10), meaning numbers are composed using 10...
We usually write numbers in decimal form (or base 10), meaning numbers are composed using 10 different “digits” {0,1,...,9}. Sometimes though it is useful to write numbers in hexadecimal or base 16. Now there are 16 distinct digits that can be used to form numbers: {0,1,...,9,A,B,C,D,E,F}. So for example, a 3 digit hexadecimal number might be 3B8. (a) How many 2-digit hexadecimals are there in which the first digit is E or F? Explain your answer in terms of the...
Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In...
Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In this task, a character buffer should be created (e.g., char buf[500]) in the server which is shared among threads that are created to handle communication with clients. Specifically, when the server receives a message (a string) from a client, the server should (1) store the message in the buffer, (2) broadcast the message to all connected clients, and (3) clear the buffer. Make sure...
1. A random number generator claims to randomly choose real numbers between 0 and 3000. (a)...
1. A random number generator claims to randomly choose real numbers between 0 and 3000. (a) If this is true, what kind of distribution would the randomly chosen numbers have? (b) What would be the mean of this distribution? (c) What would be the standard deviation of this distribution? (d) Say we take a sample of 65 generated numbers and obtain a sample mean of 1552. What do we know about the sampling distribution of the sample mean and how...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT