This post will discuss how to generate a random float between interval [0.0, 1.0) in Python.

1. Using random.uniform() function

You can use the random.uniform(a, b) function to generate a pseudorandom floating-point number n such that a <= n <= b for a <= b. To illustrate, the following generates a random float in the closed interval [0, 1]:

Download  Run Code

2. Using random.random() function

If you need to generate a random floating-point number in the half-open interval [0.0, 1.0), you can call the random.random() function.

Download  Run Code

3. Using random.randint() function

With the random.randrange() function, you can generate a random floating-point number in the half-open interval [0.0, 1.0) in the following manner:

Download  Run Code

4. Using numpy.random.random() function

If you prefer NumPy, you can use the numpy.random.random() function to generate random floats in the half-open interval [0.0, 1.0).

5. Using numpy.random.random_sample() function

Another solution to generate random floats in the half-open interval [0.0, 1.0) with NumPy is using the numpy.random.random_sample() function.

That’s all about generating a random float in Python.