【发布时间】:2021-02-10 19:40:35
【问题描述】:
我尝试使用 python 在给定的间隔上生成随机整数的均匀分布(它是否包含其上限并不重要)。我使用了下一个 sn-p 代码来执行此操作并绘制结果:
import numpy as np
import matplotlib.pyplot as plt
from random import randint
propsedPython = np.random.randint(0,32767,8388602)%2048
propsedPythonNoMod = np.random.randint(0,2048,8388602)
propsedPythonNoModIntegers = np.random.random_integers(0,2048,8388602)
propsedPythonNoModRandInt = np.empty(8388602)
for i in range(8388602):
propsedPythonNoModRandInt[i] = randint(0,2048)
plt.figure(figsize=[16,10])
plt.title(r'distribution $\rho_{prop}$ off all the python simulated proposed indices')
plt.xlabel(r'indices')
plt.ylabel(r'$\rho_{prop}$')
plt.yscale('log')
plt.hist(propsedPython,bins=1000,histtype='step',label=r'np.random.randint(0,32767,8388602)%2048')
plt.hist(propsedPythonNoMod,bins=1000,histtype='step',label=r'np.random.randint(0,2048,8388602')
plt.hist(propsedPythonNoModIntegers,bins=1000,histtype='step',label=r'np.random.random_integers(0,2048,8388602)')
plt.hist(propsedPythonNoModRandInt,bins=1000,histtype='step',label=r'for i in range(8388602):propsedPythonNoModRandInt[i] = randint(0,2048)')
plt.legend(loc=0)
结果图是: 有人能指出正确的方向,为什么这些尖峰出现在所有不同的情况下,或者给出一些建议,使用哪个例程来获得均匀分布的随机整数?
非常感谢!
【问题讨论】:
-
是否需要使用 Numpy?
-
@Alex,是的。我刚刚用“导入部分”编辑了我的问题。
-
你能显示你用来创建情节的代码吗?我怀疑尖峰是使用的分箱的产物。它们不太可能代表基础 PRNG 的缺陷。
-
我同意@MarkDickinson 的观点,但我们很难确认您是否发布了一个说明问题的工作示例。尝试运行您发布的内容会产生
name 'proposedIndices' is not defined。 -
我同意@MarkDickinson 和pjs,这就是我将答案手动分箱的原因。只需插入您的 RNG 并尝试重新绘制您的图表
标签: python random numbers integer uniform-distribution