Random Number Generation?

Alex Martelli aleax at mail.comcast.net
Sun Dec 11 13:08:28 EST 2005


Dimos <dimos_anastasiou at yahoo.com> wrote:

> Hello All,
> 
> 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 module lets you create floats and integers, not instances of
class decimal.Decimal (surely not in 2.3, which didn't even HAVE a
module decimal in its standard library!).  If you want floats, the way
to create a float with uniform distribution between 5 and 90 is to call
random.uniform(5, 90).  If you want 3000:

r3k = [random.uniform(5, 90) for i in xrange(3000)]

None of the 3k numbers will be integers, and it's unlikely that any of
the 3k floats happens to have a fractional part that's exactly 0.  If
you do want integers as well as floats, you'll have to decide with what
probability an integer must appear instead of a float, and do a second
pass on r3k to enforce this.


Alex



More information about the Python-list mailing list