Python Generate List of Random Numbers Between 0 and 1 Example

By Hardik Savani October 30, 2023 Category : Python

Hi Friends,

In this quick example, let's see python generate list of random numbers between 0 and 1. We will use list of random numbers between 0 and 1 python. In this article, we will implement a python list generate range 0 to 1. This article will give you a simple example of python create list from 0 to 1.

If you are looking to generate a list of random float numbers between 0 to 1 in python, there are several ways to create a list of random float numbers between 0 to 1 in python. here, I will give you two examples using random and numpy library for creating a new list of random numbers between 0 to 1 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
high = 1
  
# Python Generate List of Random Numbers Between 0 to 1 Example
floatList = [random.uniform(low, high) for _ in range(5)]
  
print(floatList)

Output:

[0.025043928349008593, 0.5088841730535656, 0.8050595521426983, 0.9853555574643967, 0.5642210206465184]

Example 2: Random Float with 2 Decimal Point

main.py

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

Output:

[0.52, 0.05, 0.38, 0.23, 0.88]

I hope it can help you...

Tags :
Shares