来自线性全余随机数的数字的 PDF



我正在编写一个python代码来创建由0到1之间的线性同余随机数生成的数字的概率分布。我没有得到我预期的密度图。它不是一个高度为 1 的矩形,从 0 到 1 跨度为 x 吗?

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()

除法项/mx[i+1]抛入范围[0, 1),这打破了线性同余生成器的形式。 尝试将代码分为"生成器"和"规范化"部分(分别在此处yx[i](:

y = x[0]
for i in range(n-1):
y = (a*y+c)%m
x[i+1] = y/m

最新更新