random number between -maxint and +maxint

Gregory Jorgensen gregj at pobox.com
Sun Mar 11 18:57:15 EST 2001


Use long integers instead of ints. The whrandom module's internal methods use
int() to make sure you passed an integer; they could just as well use long().
Look at the whrandom source.

Try this:

>>> import whrandom
>>> whrandom.seed()
>>> maxint = 2L**31
>>> r = long(whrandom.random() * (maxint + maxint + 1)) - maxint

The expression (maxint + maxint + 1) is the range of random long integers you
want: [0,maxint].


In article <K2Sq6.360684$Pm2.5406628 at news20.bellglobal.com>, Dan Parisien
says...
>
>I can't figure out a good way to do it, does anyone know a better solution 
>than
>
>from whrandom import whrandom
>rand = whrandom()
>max = 2**30 + (2**30-1) #one less than 2**31
>randomnumber = rand.randrange(0, max) - rand.randrange(0, max)
>
>thank you for your time,
>Dan

Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com



More information about the Python-list mailing list