Random Number Generation?

Fredrik Lundh fredrik at pythonware.com
Sun Dec 11 13:11:23 EST 2005


Dimos wrote:

> I need some help with random number generation. What I
> need exactly is:
>
> To create a few thousand numbers, decimal and
> integers, between 5 and 90,
> and then to export them as a single column at a
> spreadsheet.
>
> I am newbie, I was not able to create decimals with
> the random modules of
> Python 2.3.

the random function returns a random number between 0.0 and 1.0,
so

>>> import random
>>> help(random.random)
Help on built-in function random:

random(...)
    random() -> x in the interval [0, 1).

so to get what you want, all you have to do is to multiply by (90-5)
and add 5 at the end:

    v = random.random() * 85.0 + 5.0

the random.uniform() wrapper does exactly this:

    v = random.uniform(5.0, 90.0)

</F>






More information about the Python-list mailing list