Discussion:
Size of integers (2**32-1)
Paul Rubin
2002-02-05 10:28:50 UTC
Permalink
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))
Thomas Guettler
2002-02-05 10:34:18 UTC
Permalink
with the cygwin version "print 2**32-1" works
but with the zope version I get OverFlowError.
Zope uses Python 2.1, while your Cygwin Python is 2.2, right?
yes

[cut]
Btw. it looks like random.randint/randrange really want ints and won't
print random.randrange(0, sys.maxint) * 2L
This would give me a long. But I need a 32Bit Integer.
Unfortunately there is no sys.minint.

OK, it is not that important for me. But it would be nice to know
the answer (How to get a radom 32 Bit integer)

thomas
Paul Rubin
2002-02-05 21:42:04 UTC
Permalink
Post by Thomas Guettler
This would give me a long. But I need a 32Bit Integer.
Unfortunately there is no sys.minint.
OK, it is not that important for me. But it would be nice to know
the answer (How to get a radom 32 Bit integer)
The largest 32-bit integer (sys.maxint on a 32 bit system) is 2**31-1.
The smallest integer is -(2**31).
John Machin
2002-02-05 13:05:26 UTC
Permalink
On Tue, 05 Feb 2002 10:16:36 +0100, Thomas Guettler
with the cygwin version "print 2**32-1" works
but with the zope version I get OverFlowError.
I want to get a random 32-Bit integer like this randrange(0, 2*32-1)
thomas
Do you mean 2**31-1? Putting it another way, do you want a 32-bit
signed integer or an unsigned one?

It would help if you looked at the version *numbers* of the two
Pythons that you have; you will probably find that the "cygwin
version" is 2.2 and the "zope version" is 2.1

The problem is that 2**32 won't fit in 32 bits; Pythons before 2.2
cracked an overflow exception, whereas 2.1 promotes the result to a
long integer. Further, even 2**32-1 won't fit in a Python plain
integer which is a *signed* 32-bit integer on most/many platforms,
including yours.

Here is a quote from the Language Reference manual, section 3.2
"""
Plain integers
These represent numbers in the range -2147483648 through 2147483647.
(The range may be larger on machines with a larger natural word size,
but not smaller.) When the result of an operation would fall outside
this range, the exception OverflowError is raised.
"""
Yes, that's from the 2.2 version, and yes, it needs updating :-)

So ... if you really want the value 2**32-1 on older Pythons, you need
to say 2L**32-1

AND in any case it looks like you're plumb out of luck with
random.randrange() .... it seems to be expecting a plain integer ...

Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
import random
random.randrange(2L**32-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\python22\lib\random.py", line 287, in randrange
istart = int(start)
OverflowError: long int too large to convert to int

Do you really need a 32-bit integer? Would you like to be (brace
import sys
print random.randrange(sys.maxint)
1632975901
print random.randrange(sys.maxint)
156138328
print random.randrange(sys.maxint)
67281065

Oh and we'd better read the doco carefully: randrange(x) is designed
to act like Python's built-in range function --- 0 <= randrange(x) <
x.

To obtain a random 32-bit unsigned integer, you will need
long(random.random() * 2L ** 32)
Peter Hansen
2002-02-10 17:50:40 UTC
Permalink
Post by Paul Rubin
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))
Unfortunately, random.randrange(-2147483648, 2147483647) would
exclude 2147483647. randrange(1) produces only zero....
Paul Rubin
2002-02-05 10:29:33 UTC
Permalink
Btw. it looks like random.randint/randrange really want ints and won't
print random.randrange(0, sys.maxint) * 2L
That's always zero in the lowest bit.
Thomas Guettler
2002-02-05 09:53:53 UTC
Permalink
with the cygwin version "print 2**32-1" works
but with the zope version I get OverFlowError.
I want to get a random 32-Bit integer like this randrange(0, 2*32-1)
Values of sys.maxint must be different. Try: randrange(0,2147483647).
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.
Thomas Guettler
2002-02-05 09:16:36 UTC
Permalink
I have two python version installed:
with the cygwin version "print 2**32-1" works
but with the zope version I get OverFlowError.

Background:
I want to get a random 32-Bit integer like this randrange(0, 2*32-1)

thomas
Gerhard Häring
2002-02-05 09:49:34 UTC
Permalink
with the cygwin version "print 2**32-1" works
but with the zope version I get OverFlowError.
Zope uses Python 2.1, while your Cygwin Python is 2.2, right?

Python 2.2 does automagically change the type to LongType when the
integers get too big. Python 2.1 doesn't do this, yet.

The reason it fails in Python 2.1 is that 2**32 is not in the range of
the IntType (on your 32 bit platform), IOW it's greater than sys.maxint
(sys.maxint == 2**31-1) Note that in Python 2.1, you can't even say
"print 2**31-1" because it would first need to represent 2**31 as an
IntType, which it cannot.

To represent the number you want in Python < 2.2, you need to say
2L**32-1.
Background: I want to get a random 32-Bit integer like this
randrange(0, 2*32-1)
Btw. it looks like random.randint/randrange really want ints and won't
accept longs. A workaround could be to use a construction like:

print random.randrange(0, sys.maxint) * 2L

Gerhard
--
This sig powered by Python!
Au?entemperatur in M?nchen: 11.2 ?C Wind: 4.6 m/s
Paul Rubin
2002-02-05 09:35:34 UTC
Permalink
with the cygwin version "print 2**32-1" works
but with the zope version I get OverFlowError.
I want to get a random 32-Bit integer like this randrange(0, 2*32-1)
Values of sys.maxint must be different. Try: randrange(0,2147483647).
Continue reading on narkive:
Loading...