如何在Python中实现随机生成器

如何在Python中实现随机生成器

本文介绍了如何在Python中实现随机生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个公式

xi + 1 =(p1 * xi + p2)%(2 ** N)

i需要生成2个列表:

xlist和ylist,其中xlist = [X0,X1,X2,..... Xn-1]



ylist = [X1,X2 ,..... Xn]其中:

X0 =种子为56例如,n为样本数



i需要你的帮助我不想再赚python了,我尝试了一些解决方案:



我尝试过:



i have this formula
xi+1 = (p1*xi + p2)%(2**N)
i need to generate 2 lists:
xlist and ylist where xlist=[X0, X1,X2,.....Xn-1]
and
ylist = [X1,X2,.....Xn] where:
X0=Seed as 56 for example , n is number of samples

i need your help i wont like to earn python any more and i have tried some solution:

What I have tried:

p1 = 15436
p2 = 65
n = 22
seed = 56
list1 = []
def squares(N):
	seed = 56
	x0 = seed
	for i in range(N):
		xi = (p1*x0 + p2)%(2**N)
		x0 = xi
		yield xi
for n in squares(1000):
	print(n)

推荐答案


这篇关于如何在Python中实现随机生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:33