开发者

More efficient way to write this simple Python non-sequential number generator?

Is there a more efficient way to write this so it's not looping from 1 to n (which hangs on n == 2**32):

def ns_num(n, seed, modulo, incrementor):
    assert n < modulo

    cu开发者_如何学运维rrent = 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)


That's the same as

return (seed + (n - 1) * incrementor) % modulo

(Are you sure you want n - 1? That's what you current code does.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜