Random number function python that includes 1?
Question: I am new to Python and am trying to create a program for a project- firstly, I
need to generate a point between the numbers 0-1.0, including 0 and 1.0 ([0,
1.0]). I searched the python library for functions
(<https://docs.python.org/2/library/random.html>) and I found this function:

    
    
    random.random()
    

This will return the next random floating point number in the range [0.0,
1.0). This is a problem, since it does not include 1. Although the chances of
actually generating a 1 are very slim anyway, it is still important because
this is a scientific program that will be used in a larger data collection.

I also found this function:

    
    
    rand.randint
    

This will return an integer, which is also a problem.

I researched on the website and previously asked questions and found that this
function:

    
    
    random.uniform(a, b)
    

will only return a number that is greater than or equal to a and less than b.

Does anyone know how to create a random function on python that will include
[0, 1.0]?

Please correct me if I was mistaken on any of this information. Thank you.

*The random numbers represent the x value of a three dimensional point on a sphere.


Answer: Consider the following function built on top of `random.uniform`. I believe
that the re-sampling approach should cause all numbers in the desired interval
to appear with equal probability, because the probability of returning
`candidate > b` is `0`, and originally all numbers should be equally likely.

    
    
    import sys
    import random
    
    
    def myRandom(a, b):
        candidate = uniform.random(a, b + sys.float_info.epsilon)
        while candidate > b:
           candidate = uniform.random(a, b + sys.float_info.epsilon)
        return candidate
    

As gnibbler mentioned below, for the general case, it may make more sense to
change both the calls to the following. Note that this will only work
correctly if `b > 0`.

    
    
     candidate = uniform.random(a, b*1.000001)
    

