Python Generate List of Random Float Numbers Example

By Hardik Savani October 30, 2023 Category : Python

Hello Developer,

In this example, you will learn python generate list of random float numbers. We will look at an example of python create list of random floats. I would like to show you python generate random list of floats. if you want to see an example of how to generate list of random float numbers in python then you are in the right place.

If you are looking to generate a list of random float numbers in python, there are several ways to create a list of random float numbers in python. here, I will give you two examples using random and numpy library for creating a new list of random float numbers in python. so, let's see the example code.

You can use these examples with python3 (Python 3) version.

let's see below a simple example with output:

Example 1:

main.py

import random
import numpy as np
  
low = 0.2
high = 2.9
  
# Python Generate List of Random Float Numbers Example
floatList = [random.uniform(low, high) for _ in range(5)]
  
print(floatList)

Output:

[1.2810449626587266, 1.5206898226328796, 1.9837497458591744, 0.8076086725933713, 0.6949857530454222]

Example 2: Random Float with 2 Decimal Point

main.py

import random
import numpy as np
  
low = 0.2
high = 2.9
  
# Python Generate List of Random Float Numbers Example
floatList = [round(random.uniform(low, high), 2) for _ in range(5)]
  
print(floatList)

Output:

[0.96, 1.77, 1.3, 1.83, 1.87]

I hope it can help you...

Tags :
Shares