➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

作者 rhettinger
收信人 belopolsky, mark.dickinson, rhettinger, terry.reedy, vstinner
日期 2010-06-23.07:54:29
SpamBayes Score 0.5117326
Marked as misclassified 否
Message-id <1277279672.45.0.454607609945.issue9025@psf.upfronthosting.co.za>
In-reply-to
内容
FWIW, here are two approaches to getting an equi-distributed version of int(n*random()) where 0 < n <= 2**53.  The first mirrors the approach currently in the code.  The second approach makes fewer calls to random().

def rnd1(n):
    assert 0 < n <= 2**53
    N = 1 << (n-1).bit_length()
    r = int(N * random())
    while r >= n:
        r = int(N * random())
    return r

def rnd2(n, N=1<<53):
    assert 0 < n <= N
    NN = N - (N % n) + 0.0  # largest multiple of n <= N
    r = N * random()        # a float with an integral value
    while r >= NN:
        r = N * random()
    return int(r) % n
历史
日期 用户 动作 参数
2010-06-23 07:54:32rhettinger修改recipients: + rhettinger, terry.reedy, mark.dickinson, belopolsky, vstinner
2010-06-23 07:54:32rhettinger修改messageid: <1277279672.45.0.454607609945.issue9025@psf.upfronthosting.co.za>
2010-06-23 07:54:30rhettinger链接issue9025 messages
2010-06-23 07:54:29rhettinger创建