【发布时间】:2020-05-23 15:50:12
【问题描述】:
我正在编写一个 python 代码来创建由 线性同余随机数在 0 和 1 之间生成的数字的概率分布。我没有得到我期望的密度图。它不应该是一个高度为 1 且 x 跨度为 0 到 1 的矩形吗?
import numpy as np
import matplotlib.pyplot as plt
m=2**31 #these numbers are same as used by C in rand()
a=1103515245
c=12345
n = 10000
x = np.zeros(n)
x[0]=1 #Seed
for i in range(n-1):
x[i+1] = ((a*x[i]+c)%m)/m #Divided by m so that numbers are scaled to the range [0,1]
plt.hist(x,density=True)
plt.show()
【问题讨论】: