Size of integers (2**32-1)

Paul Rubin phr-n2002a at nightsong.com
Tue Feb 5 05:28:50 EST 2002


Thomas Guettler <st-newsgroups at thomas-guettler.de> writes:
> The module random contains a lot of functions. I think getting a
> random 32 random number is needed often.
> 
> randrange(0,2147483647) is not a 32 Bit random number because
>   2**32-1 is 4294967295.

Woops, sorry, I misread 2**32-1 as 2**31-1 which is the largest
32-bit positive integer.  2**32-1 is an overflow if you use 32 bit
ints.  I guess you could try random(-2147483648, 2147483647)
if you want all 32 bits random.  Alternatively you could try
  (random(0,65536) << 16 | random(0,65536))



More information about the Python-list mailing list