我正在制作另一个python脚本来执行线性同余生成器来生成55个伪随机数,但是很难理解线性同余生成器的算法,以及我的脚本是如何工作的,即使脚本对我来说很简单。
基于python脚本,为什么输出在每个循环中都在变化?对于线性同余生成器,python脚本正确吗?
#!/usr/bin/python3
# Fix variable for the seed, modulus, a and c
seed = 8
a = 2
c = 2
m = 20
# counter for how many iterations we've run
counter = 0
#Perfom number of iterations requested by user
while counter < 50:
# Store value of each iteration
seed = (a * seed + c) % m
counter = counter + 1发布于 2021-05-24 02:15:33
由于要更新种子变量,每次迭代的输出都会发生变化,您可以使用上一次迭代中的种子来更改下一次迭代的种子。
seed = (a * seed + c) % m
在第一次迭代中,您的种子是5,所以
(3 * 5 + 3) % 19
给出18.关于第二次迭代
(3 * 18 + 3) % 19
给出0,因为3*18 +3是57,57模m是19是0。因此循环继续,每个先前的种子都会影响下一个种子。
https://stackoverflow.com/questions/67665466
复制相似问题