Python Create List from Random Numbers Tutorial Example

18-May-2023

.

Admin

Python Create List from Random Numbers Tutorial Example

Hi Dev,

In this post, we will learn to create a list of numbers from random in a Python tutorial example. Here you will learn Python to create a list random of numbers. you will learn to create a list of numbers Python random. This article goes into detail on how to create a list from random in Python. Follow the below tutorial step of Python to create a list of numbers from random.

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 a new list random numbers in Python. so, let's see the example code.

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]

#Python