【问题标题】:PDF of numbers from linear congruential random numbers来自线性同余随机数的数字 PDF
【发布时间】: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()

【问题讨论】:

    标签: python math random


    【解决方案1】:

    除法/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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多