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.

Author rhettinger
Recipients belopolsky, mark.dickinson, rhettinger, terry.reedy, vstinner
Date 2010-06-23.07:54:29
SpamBayes Score 0.5117326
Marked as misclassified No
Message-id <1277279672.45.0.454607609945.issue9025@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2010-06-23 07:54:32rhettingersetrecipients: + rhettinger, terry.reedy, mark.dickinson, belopolsky, vstinner
2010-06-23 07:54:32rhettingersetmessageid: <1277279672.45.0.454607609945.issue9025@psf.upfronthosting.co.za>
2010-06-23 07:54:30rhettingerlinkissue9025 messages
2010-06-23 07:54:29rhettingercreate