random number including 1 - i.e. [0,1]

Jussi Piitulainen jpiitula at ling.helsinki.fi
Wed Jun 10 03:35:01 EDT 2009

Esmail writes:

> random.random() will generate a random value in the range [0, 1).
> Is there an easy way to generate random values in the range [0, 1]?
> I.e., including 1?
> I am implementing an algorithm and want to stay as true to the
> original design specifications as possible though I suppose the
> difference between the two max values might be minimal.

You could generate from a larger range and reject the values that
you do not want: generate from [0, 2), say, until you get a value
in [0, 1].

If you generate from [0, 1 + epsilon) with small epsilon,
rejections will be rare.

(I didn't notice this suggestion in the thread, so I'm voicing it
just in case it's not there yet.)

More information about the Python-list mailing list