[Tutor] Fun with random numbers

Magnus Lycka magnus@thinkware.se
Wed Jan 22 10:33:02 2003


At 09:03 2003-01-22 -0500, Michael Miller wrote:
>It appears my first post didn't go through. Grrr....
>How can I easily generate a bunch of random numbers? For example,
>543765843675465436788654365431885431 or something to that effect. I know the
>answer is staring me in the face somewhere in that module documentation

More or less...

>  (does
>that stuff make sense to anyone? ;)

Yes

>), but for the life of me I can't find
>it.

"import random" is a start

It's probably better if you can describe a little better what you
are after. It seems you are after very big numbers. What do you
intend to use them for?

random.random()

will return a random float between 0 and 1.

r = random.randrange(x, y)

will return a random integer such that x <= r < y.

I don't think there is a funktion to generate long integers,
but that's easy to fix. Go via a string.

For instance if you want a 27 digit random number:

 >>> print long("%09i" % random.randrange(0,1000000000)+
...            "%09i" % random.randrange(0,1000000000)+
...            "%09i" % random.randrange(0,1000000000))
502280329152143067453249503

If you have specific demands on random distribution this
might not do what you want. As I said, we really need to
know what you are after to give a good answer.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se