There is a need to generate random numbers when studying a model or behavior of a program for different range of values. Python can generate such random numbers by using the random module. In the below examples we will first see how to generate a single random number and then extend it to generate a list of random numbers.
Generating a Single Random Number
The random[] method in random module generates a float number between 0 and 1.
Example
import random n = random.random[] print[n]
Output
Running the above code gives us the following result −
0.2112200
Generating Number in a Range
The randint[] method generates a integer between a given range of numbers.
Example
import random n = random.randint[0,22] print[n]
Output
Running the above code gives us the following result −
2
Generating a List of numbers Using For Loop
We can use the above randint[] method along with a for loop to generate a list of numbers. We first create an empty list and then append the random numbers generated to the empty list one by one.
Example
import random randomlist = [] for i in range[0,5]: n = random.randint[1,30] randomlist.append[n] print[randomlist]
Output
Running the above code gives us the following result −
[10, 5, 21, 1, 17]
Using random.sample[]
We can also use the sample[] method available in random module to directly generate a list of random numbers.Here we specify a range and give how many random numbers we need to generate.
Example
import random #Generate 5 random numbers between 10 and 30 randomlist = random.sample[range[10, 30], 5] print[randomlist]
Output
Running the above code gives us the following result −
[16, 19, 13, 18, 15]
Updated on 08-Aug-2019 06:54:57
- Related Questions & Answers
- Generating Random Prime Number in JavaScript
- Generating random number in a range in C
- Generating random Id’s in Python
- Generating random numbers in Java
- Generating random numbers in C#
- Generating Random String Using PHP
- Generating Random id's using UUID in Python
- Generating random hex color in JavaScript
- Generating Random Short Id in Node.js
- Generating a random number that is divisible by n in JavaScript
- Generating random string of specified length in JavaScript
- Generating random strings until a given string is generated using Python
- Random Number Functions in Python
- Generating n random numbers between a range - JavaScript
- Generating random string with a specific length in JavaScript
Sometimes, in making programs for gaming or gambling, we come across the task of creating a list all with random numbers in Python. This task is to perform in general using loop and appending the random numbers one by one. But there is always a requirement to perform this in the most concise manner. Let’s discuss certain ways in which this can be done.
Random Number Using random module
Python Random module is an in-built module of Python which is used to generate random numbers. This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc.
Method 1: Using the random.randint[]
By using random.randint[] we can add random numbers into a list.
Python3
import
random
rand_list
=
[]
n
=
10
for
i
in
range
[n]:
rand_list.append[random.randint[
3
,
9
]]
print
[rand_list]
Output
[9, 3, 3, 6, 8, 5, 4, 6, 3, 7]
Method 2: Using random.sample[]
This single utility function performs the exact required as asked by the problem statement, it generated N no. of random numbers in a list in the specified range and returns the required list.
Python3
import
random
res
=
random.sample[
range
[
1
,
50
],
7
]
print
[
"Random number list is : "
+
str
[res]]
Output
Random number list is : [49, 20, 23, 34, 6, 29, 35]
Method 3: Using list comprehension + randrange[]
The naive method to perform this particular task can be shortened using list comprehension. randrange function is used to perform the task of generating the random numbers.
Python3
import
random
res
=
[random.randrange[
1
,
50
,
1
]
for
i
in
range
[
7
]]
print
[
"Random number list is : "
+
str
[res]]
Output
Random number list is : [32, 16, 9, 28, 19, 31, 21]
Method 4: using loop + randint[]
Python3
import
random
lis
=
[]
for
_
in
range
[
10
]:
lis.append[random.randint[
0
,
51
]]
print
[lis]
Output:
[3, 11, 48, 2, 48, 2, 8, 51, 8, 5]
Random Number Using Numpy
The random function provided by the Numpy module can be more useful for you as it provides little better functionality and performance as compared to the random module.
Method 1: Generating a list of random integers using numpy.random.randint function
This function returns random integers from the “discrete uniform” distribution of the integer data type.
Python3
import
numpy as np
print
[
list
[np.random.randint[low
=
3
,high
=
8
,size
=
10
]]]
print
[
list
[np.random.randint[low
=
3
,size
=
5
]]]
Output: [5, 3, 6, 7, 4, 5, 7, 7, 7, 7] [0, 2, 1, 2, 1]
Method 2. Generating a list of random floating values using numpy.random.random_sample function
This function return random float values in half open interval [0.0, 1.0].
Python3
import
numpy as np
print
[np.random.random_sample[size
=
4
]]
print
[np.random.random_sample[size
=
[
4
,
4
]]]
output: [0.08035145 0.94966245 0.92860366 0.22102797] [[0.02937499 0.50073572 0.58278742 0.02577903] [0.37892104 0.60267882 0.33774815 0.28425059] [0.57086088 0.07445422 0.86236614 0.33505317] [0.83514508 0.82818536 0.1917555 0.76293027]]
The benefit of using numpy.random over the random module of Python is that it provides a few extra probability distributions which can help in scientific research.