How to Create List of Random Numbers in Python?

By Hardik Savani October 30, 2023 Category : Python

Hello Developer,

In this tutorial, you will learn python generate list of random numbers. let us discuss about python generate random list of numbers numpy. We will use python generate list of random numbers in range. you can understand a concept of how to create list of random numbers in python. you will do the following things for python generate a list of random numbers.

If you are looking to generate a list of random numbers in python, there are several ways to create a list of random numbers in python. here, I will give you two examples with numpy and random library for creating new list random 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
  
# python generate list of random numbers
myList = random.sample(range(1, 30), 10)
  
print(myList)

Output:

[12, 26, 17, 5, 18, 16, 8, 21, 24, 2]

Example 2:

main.py

import numpy as np
  
# python generate list of random numbers
data = np.random.randint(1, 30, size=10)
myList = data.tolist()
  
print(myList)

Output:

[21, 23, 26, 1, 1, 22, 19, 29, 25, 5]

I hope it can help you...

Tags :
Shares