1 2from matplotlib import pyplot as plt 3 4import pandas as pd 5 6def linearCongruentialMethod(Xo, m, a, c, randomNums, U): 7 8 randomNums[0] = Xo 9 10 U[0] = randomNums[0] / m 11 12 for i in range(1, 10000): 13 14 # Follow the linear congruential method 15 16 randomNums[i] = ((randomNums[i - 1] * a) + c) % m 17 18 U[i] = randomNums[i] / m 19 20print("a = 1597, b = 0, m = 244944") 21 22a = 1597 23 24c = 0 25 26m = 244944 27 28i = 0.01 29 30for i in range(1, 6): 31 32 X0 = i * 0.01 33 34 print("\n X0 = ", X0, "\n") 35 36 noOfRandomNums = 10005 37 38 randomNums = [0] * (noOfRandomNums) 39 40 U = [0] * (noOfRandomNums) 41 42 linearCongruentialMethod(X0, m, a, c, randomNums, U) 43 44 intervals = 20 45 46 freq = [0] * (intervals) 47 48 for j in U: 49 50 x = 1 51 52 x = j * 100 / 5 53 54 freq[int(x)] = freq[int(x)] + 1 55 56 mydata = {'Interval ': ['[0.00,0.05)', '[0.05,0.10)', '[0.10,0.15)', '[0.15,0.20)', '[0.20,0.25)', '[0.25,0.30)', 57 58 '[0.30,0.35)', '[0.35,0.40)', '[0.40,0.45)', '[0.45,0.50)', '[0.50,0.55)', '[0.55,0.60)', 59 60 '[0.60,0.65)', '[0.65,0.70)', '[0.70,0.75)', '[0.75,0.80)', '[0.80,0.85)', '[0.85,0.90)', 61 62 '[0.90,0.95)', '[0.95,1.00)'], 63 64 'Freuency': freq} 65 66 df = pd.DataFrame(mydata) 67 68 print(df) 69 70 data = {'1': freq[0], '2': freq[1], '3': freq[2], '4': freq[3], '5': freq[4], '6': freq[5], '7': freq[6], 71 72 '8': freq[7], '9': freq[8], '10': freq[9], '11': freq[10], '12': freq[11], '13': freq[12], '14': freq[13], 73 74 '15': freq[14], '16': freq[15], '17': freq[16], '18': freq[17], '19': freq[18], '20': freq[19], } 75 76 interval = list(data.keys()) 77 78 frequency = list(data.values()) 79 80 fig = plt.figure(figsize=(10, 5)) 81 82 plt.bar(interval, frequency, color='maroon', width=0.4) 83 84 plt.show() 85 86print("\na = 51749, b = 0, m = 244944") 87 88a = 51749 89 90c = 0 91 92m = 244944 93 94i = 0.01 95 96for i in range(1, 6): 97 98 X0 = i * 0.01 99 100 print("\n X0 = ", X0, "\n") 101 102 noOfRandomNums = 10005 103 104 randomNums = [0] * (noOfRandomNums) 105 106 U = [0] * (noOfRandomNums) 107 108 linearCongruentialMethod(X0, m, a, c, randomNums, U) 109 110 intervals = 20 111 112 freq = [0] * (intervals) 113 114 for j in U: 115 116 x = 1 117 118 x = j * 100 / 5 119 120 freq[int(x)] = freq[int(x)] + 1 121 122 mydata = {'Interval ': ['[0.00,0.05)', '[0.05,0.10)', '[0.10,0.15)', '[0.15,0.20)', '[0.20,0.25)', '[0.25,0.30)', 123 124 '[0.30,0.35)', '[0.35,0.40)', '[0.40,0.45)', '[0.45,0.50)', '[0.50,0.55)', '[0.55,0.60)', 125 126 '[0.60,0.65)', '[0.65,0.70)', '[0.70,0.75)', '[0.75,0.80)', '[0.80,0.85)', '[0.85,0.90)', 127 128 '[0.90,0.95)', '[0.95,1.00)'], 129 130 'Freuency': freq} 131 132 df = pd.DataFrame(mydata) 133 134 print(df) 135 136 data = {'1': freq[0], '2': freq[1], '3': freq[2], '4': freq[3], '5': freq[4], '6': freq[5], '7': freq[6], 137 138 '8': freq[7], '9': freq[8], '10': freq[9], '11': freq[10], '12': freq[11], '13': freq[12], '14': freq[13], 139 140 '15': freq[14], '16': freq[15], '17': freq[16], '18': freq[17], '19': freq[18], '20': freq[19], } 141 142 interval = list(data.keys()) 143 144 frequency = list(data.values()) 145 146 fig = plt.figure(figsize=(10, 5)) 147 148 plt.bar(interval, frequency, color='blue', width=0.4) 149 150 plt.show()
好买网www,goodmai,com
