integer type conversion problem/question

Josiah Carlson jcarlson at uci.edu
Sat Oct 9 21:43:25 CEST 2004


> It looks like using a random number generator which uses unsigned ints
> as its seeds with Python is probably close to impossible then. Can
> anyone suggest a good C/C++ random number implementation which can be
> used easily with Python in this fashion? I want something that is
> full-featured, ie. has reasonable support for different random number
> distributions. Also, something that was already packaged in a
> reasonable fashion as part of a shared library would be nice. I
> suppose something whose seeds are stored as ints or longs would work
> Ok.

Mersenne Twister is included with Python 2.3 and later (maybe even 2.2,
I can't remember that far back).

You can use it via:
import random

It includes various distributions, read the documentation.


> I did have one followup question. If Python implements its integers as
> signed C ints then surely 2^31 - 1 should be an integer rather than a
> long? But I get
> 
>  In [9]: 2**31 - 1
>  Out[9]: 2147483647L
> 
>  In [10]: type(2**31 - 1)
>  Out[10]: <type 'long'>

It first creates 2**31, then subtracts 1.
Try 2**30 + (2**30 -1).




More information about the Python-list mailing list