Python:实现linear congruential generator线性同余发生器算法(附完整源码)

51CTO 2022-08-06 09:34:40 阅读数:622

Python实现generatorlinearcongruential


Python:实现linear congruential generator线性同余发生器算法

__author__ = "Tobias Carryer"

from time import time


class LinearCongruentialGenerator:

def __init__( self, multiplier, increment, modulo, seed = int( time())): # noqa: B008

self. multiplier = multiplier
self. increment = increment
self. modulo = modulo
self. seed = seed

def next_number( self):

self. seed = ( self. multiplier * self. seed + self. increment) % self. modulo
return self. seed


if __name__ == "__main__":
# Show the LCG in action.
lcg = LinearCongruentialGenerator( 1664525, 1013904223, 2 << 31)
while True:
print( lcg. next_number())
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.


版权声明:本文为[51CTO]所创,转载请带上原文链接,感谢。 https://blog.51cto.com/u_11929818/5551016