How to Create List of Random Numbers in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Create List from 1 to 10 Example
- Python Create List from 1 to 1000 Example
- How to Create a List of Numbers from 1 to 100 in Python?
- How to Create List of Numbers from Range in Python?
- How to Convert Dictionary Values into List in Python?
- Python Create JSON File from List Example
- How to Check If a List is Empty or Not in Python?
- How to Add Element at the End of List in Python?
- How to Count Number of Elements in a List in Python?
- Python List Print All Elements Except Last Example
- How to Convert List into String in Python?
- How to Remove Duplicate Values from List in Python?
- Python Remove Empty String from List Example