random 64-bit int

Jeff Davis jdavis at empires.org
Thu Jul 25 01:53:05 EDT 2002


I need to get a random 64-bit integer (positve or negative). Here is my 
current algorithm:

--------
from struct import pack,unpack
from random import randint
max = 2**16-1

r12 = pack('H',randint(0,max))
r34 = pack('H',randint(0,max))
r56 = pack('H',randint(0,max))
r78 = pack('H',randint(0,max))

unpack('q',r12+r34+r56+r78)[0]
---------

It seems like the above works, but I'd like to know whether I am losing 
randomness, or whether there is a more efficient or faster way to 
accomplish that. Would it be a good idea to include a call to 
random.seed()?

Also, it would be *really* nice if there was a good way to do that in 
python2.1, which does not seem to allow the "q" type for unpack().

Thanks,
        Jeff




More information about the Python-list mailing list