[Tutor] randomly gen values from .000001 to 10

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Mar 19 13:50:02 EST 2004



On Thu, 18 Mar 2004, kevin parks wrote:

> Ah -HA!! actually all i need it to take that and add 0.000001 to it to
> make sure i don't get anything lower (perhaps that is not even possible?
> Is it statistically possible that random.random() also put out an actual
> 0?

Hi Kevin,


Yes, it's possible for random.random() to emit zero.  In the 'random'
docs, there's a small note that:


"""
random()
    Return the next random floating point number in the range [0.0, 1.0).
"""


The documentation is actually using a math notation called "half-open"
intervals.  When it says [0.0, 1.0).  It translates to "anything between
zero and 1, including zero, but not including 1.0.


The notation

    [a, b)

tries to suggest that we're including the left endpoint, but not the right
endpoint.  Not sure if that makes sense visually --- are curvy surfaces
less adhesive than flat surfaces? --- but I hope that idea is a little
clearer.



But we've already seen something similar to half-open intervals:
Python's range() operator can take two endpoints, and returns all integers
in that range:

###
>>> range(7, 17)
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
###

Here, we see that range() returns all the integers in the "half-open"
interval [7, 17).



Half-intervals have nice properties.  In particular, they "glue"
together nicely:

    [a, b) + [b, c) ====> [a, c)


Hope this helps!




More information about the Tutor mailing list