避免随机数重复的方法——python
我在下面的代码中使用python中的随机数例程来创建噪声信号避免随机数重复的方法——python,python,arrays,random,noise,Python,Arrays,Random,Noise,我在下面的代码中使用python中的随机数例程来创建噪声信号 res = 10 # Add noise to each X bin accross the signal X = np.arange(-600,600,res) for i in range(10000): noise = [random.uniform(-2,2) for i in xrange(len(X))] # custom module to save output of X and noise to .fi
res = 10
# Add noise to each X bin accross the signal
X = np.arange(-600,600,res)
for i in range(10000):
noise = [random.uniform(-2,2) for i in xrange(len(X))]
# custom module to save output of X and noise to .fits file
wp.save_fits('test10000', X, noise)
plt.plot(V, I)
plt.show()
在本例中,我将生成10000个“noise.fits”文件,然后我希望将这些文件合并在一起,以显示叠加噪声均方根(rms)的预期1/sqrt(N)依赖性,作为合并对象数量的函数
我的问题是,rms遵循这种依赖性直到1000个对象,在这一点上它向上偏移,这表明随机数生成器
是否有一种常规或方法来构造代码,以避免或尽量减少这种重复?(理想情况下,数字是介于最大值和最小值>1之间的浮点数,我认为你不会接近RNG的周期。你可能有一个bug。不过,为了测试它是否是由于RNG中的弱点造成的,请尝试使用
random.SystemRandom
生成随机数。这是为了加密安全,所以不太可能有任何意外的统计结果。我已经运行了一些测试,但在使用random.SystemRandom时问题仍然存在。我还试图找到我的bug可能在哪里,但我正在努力。随机例程似乎听起来不错,所以可能在添加例程中,但找到哪里很棘手。有什么想法吗?请澄清这是我与Random.SystemRandomrnd=Random.SystemRandom()一起使用的代码,用于范围(9000)内的I:
X=np.arange(-600600,res)
X=X.tolist()noise=np.zero(len(X))
Y=[rnd.uniform(-2,2)用于范围(len(X))]noise=noise+Y
你不会碰巧有其他数据错误地进入计算,是吗?也许你的地球仪捕捉到了你不想要的东西。
import os
import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
import glob
rms_arr =[]
#vel_w_arr = []
filelist = glob.glob('/Users/thbrown/Documents/HI_stacking/mockcat/testing/test10000/M*.fits')
filelist.sort()
for i in (filelist[:]):
print(i)
#open an existing FITS file
hdulist = fits.open(str(i))
# assuming the first extension is the table we assign data to record array
tbdata = hdulist[1].data
#index = np.arange(len(filelist))
# Access the signal column
noise = tbdata.field(1)
# access the vel column
X = tbdata.field(0)
if i == filelist[0]:
stack = np.zeros(len(noise))
tot_rms = 0
#print len(stack)
# sum signal in loop
stack = (stack + noise)
rms = np.std(stack)
rms_arr = np.append(rms_arr, rms)
numgal = np.arange(1, np.size(filelist)+1)
avg_rms = rms_arr / numgal