是否有一种更有效的方法来编写它,因此它不会从1循环到n(它挂在n == 2 ** 32):
def ns_num(n, seed, modulo, incrementor):
assert n < modulo
current = seed # some start value
for i in xrange(1, n):
current = (current + incrementor) % modulo
return current
print ns_num(5, 3250, 87178291199, 17180131327)
print ns_num(2**32, 3250, 87178291199, 17180131327)
答案 0 :(得分:7)
与
相同return (seed + (n - 1) * incrementor) % modulo
(您确定要n - 1
吗?这就是您当前代码的作用。)