现在的位置: 首页 > 综合 > 正文

随机采样系列1:线性同余发生器:生成伪随机数

2017年12月12日 ⁄ 综合 ⁄ 共 325字 ⁄ 字号 评论关闭

参考资料:http://www.cnblogs.com/xkfz007/archive/2012/03/27/2420154.html

python脚本:

class Samples:
    def __init__(self):
        pass
    def rand(self, num, seed = 1):
        m = math.pow(2, 32)
        a = 214013
        c = 2531011
        i = 1
        x = np.zeros(num)
        x[0] = seed
        while(i < num):
            x[i] = (a * x[i-1] + c) % m
            i += 1
        return x
if __name__=='__main__':
    s = Samples()
    x00 = s.rand(1000)
    plt.hist(x00,200)
    plt.show()

样本的直方图如下:

抱歉!评论已关闭.