### Illustration of the linear congruential method. ### Using Python code to generate random numbers. # develop the algorithm to run the generator. # Python code a = 5 c = 7 m = 8 n = 20 # number of random values to be generated x = [0]*n print(x) x[0] = 4 # seed or x_0 for i in range(1,n): x[i] = (a*x[i-1] + c) x[i] = x[i] % m # this calculates the remainder, i.e., calculates the mod x = x/m print(x) # Question: What does m control in this random number generator? Why should the value of m be large? ############################################################################################################# # A function that generates psudo random uniform values using the linear congruential method. import numpy as np def RANU(seed,n): # a,c,m are the values need for the linear congruential method. # seed is the value of the seed for the random number generator. # n is the length of the vector returned. a = 65539 c = 0 m = 2**31 x = np.zeros(n) # creates the output vecotor of length n x[0] = seed for i in range(1,n): x[i] = (a*x[i-1] + c) # this calculates the remainder, i.e., calculates the mod x[i] = x[i] % m # dividing by m gives values between [0,1) x = x/m return x # A function that generates psudo random exponential values using the inverse cdf method. def RANE(seed,n): # seed is the seed for the random number generator. # n is the length of the vector returned. x = RANU(seed,n) y = -np.log(x) return y ### Illustration of the RANU. seed = 4 n = 20 x = RANU(seed,n) print(x) # make a scatter plot of pairs of values. import numpy as np import matplotlib.pyplot as plt # Number of pairs n = 100 # Generate n random uniform values between 0 and 1 x_values = np.random.uniform(0, 1, n) y_values = np.random.uniform(0, 1, n) # Create sequential pairs pairs = [(x_values[i], y_values[i]) for i in range(n)] # Extract x and y coordinates from pairs x_coords = [pair[0] for pair in pairs] y_coords = [pair[1] for pair in pairs] # Create a scatter plot plt.figure(figsize=(8, 6)) plt.scatter(x_coords, y_coords, color='blue', alpha=0.5) plt.xlabel('X Values') plt.ylabel('Y Values') plt.title('Scatter Plot of Sequential Pairs of Random Uniform Values') plt.grid(True) # Show the plot plt.show() ## Illustration of the RANE. seed = 4 n = 20 x = RANE(seed,n) print(x) # make a scatter plot of pairs of values. import numpy as np import matplotlib.pyplot as plt # Number of pairs n = 100 # Generate n random values from an exponential distribution with lambda = 1 (default rate parameter) x_values = np.random.exponential(scale=1, size=n) y_values = np.random.exponential(scale=1, size=n) # Create sequential pairs pairs = [(x_values[i], y_values[i]) for i in range(n)] # Extract x and y coordinates from pairs x_coords = [pair[0] for pair in pairs] y_coords = [pair[1] for pair in pairs] # Create a scatter plot plt.figure(figsize=(8, 6)) plt.scatter(x_coords, y_coords, color='blue', alpha=0.5) plt.xlabel('X Values') plt.ylabel('Y Values') plt.title('Scatter Plot of Sequential Pairs of Exponential Random Variables') plt.grid(True) # Show the plot plt.show()