In this article, I will show you how to **generate random float numbers** in Python. This article will help you to generate a random float number between 0 and 1. Also, you will learn to generate a **random float number between any range**.

**Goals of this lesson. **In this lesson, you’ll learn how to:

- Generate random float numbers in Python
- Generate a random float number between a float range
- Generate a random floating-point number with two precision in python.
- Produce a
**secure random float number**. - Use a
**numpy.random package**to generate a random array of float numbers.

Recommended Exercise and Quiz:

**Further Reading**: A Detailed Tutorial on **Generate Random Data In Python (11 Articles)**

## random.random() to generate a random floating-point number

Let see how to use the `random.random()`

function of a random module to get a random float number. The `random.random()`

function generates a random float number uniformly in the semi-open range [**0.0, 1.0**). Let see this with an example.

import random print("Random float number is ", random.random()) print("Random float number is ", random.random()) print("Random float number is ", random.random())

**Output**: Run Online

Printing random float number 0.8504482249878023 Printing random float number 0.5769147269189085 Printing random float number 0.7774750819971197

Note: A `random.random()`

function can only provide float numbers between 0.1. to 1.0. What if you want a random float number between any two numbers? We can use `random.uniform()`

.

## random.uniform() to get a random float number between a float range

Suppose you want to generate a random float number between 10.5 to 99.5 or from 50 to 550.50. You can do this using a `random.uniform()`

function. The `random.uniform()`

function returns a random floating-point number between the given range. Let see now how to use it.

### Syntax of random.uniform()

random.uniform(start, stop)

The `random.uniform()`

function returns a random floating-point number `N`

such that `start <= N <= stop`

.

- In simple word, if
`start <= stop`

the`random.uniform`

function generates a random float number which is greater than or equal to the start number and less than or equal to the stop number. - If
`stop <= start`

, the`random.uniform`

function generates a random float number which is greater than or equal to the stop number and less than or equal to the start number. - For example, you can generate a random float number between 10 to 100 and also from 100 to 10. Both treated the same.

**Note**: *The end-point value stop may or may not be included in the range depending on floating-point rounding*.

**Arguments**

- A
`random.uniform`

function has two arguments and both the arguments are compulsory. - If you miss any of them, you will get a
`TypeError: uniform() missing 1 required positional argument`

.

### random.uniform() function Examples to get a random float number between float range

Let see how to use the `random.uniform()`

function to get a random float number between the given range.

import random print("Random float number between given range is ", random.uniform(10.5,100.5)) print("Random float number between given range is ", random.uniform(10.5,100.5)) print("Random float number between 10 and 100 is ", random.uniform(10,100)) print("Random float number between 10 and 100 is ", random.uniform(10,100)) print("Random float number between 100 and 50 is ", random.uniform(100,50)) print("Random float number between 100 and 50 is ", random.uniform(100,50))

**Output**: Run Online

Random float number between given range is 15.774675259307998 Random float number between given range is 40.20762839321159 Random float number between 10 and 100 is 10.091116283165412 Random float number between 10 and 100 is 94.51261450871945 Random float number between 100 and 50 is 79.38837140752604 Random float number between 100 and 50 is 85.31946104301423

As you can see in the output every time we got a different random float number between **10 to 100**.

## Generate a floating-point random number with two precision

As you can see the above examples generated a random float number but with 15 decimal digits. So how to **generate a random float number with limited decimal digits**.

You can use the `round()`

function to round the float number. Use the `round`

function with the `random.random`

and `random.uniform`

function to limit float number precision to two decimal places. Let see this with an example program.

import random print("Printing random float number with two decimal places is ", round(random.random(), 2)) print("Random float number between 2.5 and 22.5 with two decimal places is ", round(random.uniform(2.5,22.5), 2))

Output:

Printing random float number with two decimal places is 0.8 Random float number between 2.5 and 22.5 with two decimal places is 9.5

## Difference between random.uniform() and random.random()

- The
`random.random()`

function takes no parameters while`random.uniform()`

takes two parameters, i.e., start and stop. - The
`random.random()`

function generates a random float number between 0.0 to 1.0, but never return upper bound. I.e., It never returns 1.0. On the other side`random.uniform(start, stop)`

generates a random float number between the start and stop number. Due to the rounding effect, it can return a stop number.

## Generate a cryptographically secure random float number

**Note**: Above all methods are not cryptographically secure. To cryptographically secure random output use `random.SystemRandom()`

class. Refer to How to Cryptographically secure random data in python for more details.

**Example**

import random secure_random = random.SystemRandom() randomfloat = secure_random.random() print("Secure random float number is ", randomfloat) randomfloat = secure_random.uniform(12.5, 77.5) print("Secure random float number is ", randomfloat)

**Output**: Run Online

Secure random float number is 0.800760229291861 Secure random float number is 35.0470859352744

## Use Numpy.random to generate a random array of float numbers

NumPy isn’t a part of a standard python library. However, it has various functions to generate a random array of float numbers. Python’s NumPy module has a numpy.random package to generate random data.

You can install NumPy using pip command. Let see how to use those.

**Printing a random float number using a numpy**

import numpy random_float = numpy.random.randn() print("printing random float number using a numpy \n") print(random_float,"\n")

**Output**: Run Online

printing random float number using a numpy 0.833280610141848

**Generate a random float number using a numpy between any float range**

import numpy random_float_number = numpy.random.uniform(49.5, 99.5) print("random float array in [49.5, 99.5] \n", random_float_number,"\n")

Output:

random float array in [49.5, 99.5] 59.11713060647423

**Use a numpy.random.rand() to create an n-dimensional array of float numbers** and populate it with random samples from a uniform distribution over

**[0, 1)**.

import numpy random_float_array = numpy.random.rand(2,3) print("printing random float array 2X3 \n") print(random_float_array,"\n")

**Output**: Run Online

printing random float array 2X3 [[0.34344184 0.8833125 0.76322675] [0.17844326 0.7717775 0.86237081]]

**Use a numpy.random.uniform() to create an n-dimensional array of float numbers** between any float range.

import numpy #2 X 2 dimensional float array within 77.5 to 125.5 random_float_array = numpy.random.uniform(75.5, 125.5, size=(2, 2)) print("random float array in [77.5, 125.5] \n", random_float_array,"\n")

**Output**: Run Online

random float array in [77.5, 125.5] [[107.50697835 123.84889979] [106.26566127 101.92179508]]

## Next Steps

To practice what you learned in this article, I have created a Python random data generation **Quiz and Exercise project**.

- Solve our
**Python Random data generation Quiz**to test your random data generation concepts. - Solve the
**Python Random data generation Exercise**to practice and master the random data generation techniques.

Let me know your comments and feedback in the section below.