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
Given a list and our task is to randomly select elements from the list in Python using various functions. Selecting random numbers from a list can be used sometimes while building games, choosing a random range, etc.
Example :
Input: [2, 3, 4 , 5, 6 ] Output: 2 Input: [3, 5, 6, 3, 2] Output: 6
Using random.choice[] to select random value from a list
This random.choice[] function is designed for getting a Random sampling from a list in Python and hence is the most common method to achieve this task of fetching a random number from a list.
Python3
import
random
test_list
=
[
1
,
4
,
5
,
2
,
7
]
print
[
"Original list is : "
+
str
[test_list]]
random_num
=
random.choice[test_list]
print
[
"Random selected number is : "
+
str
[random_num]]
Output:
Original list is : [1, 4, 5, 2, 7] Random selected number is : 1
Using random.randrange[] to select random value from a list
random.randrange[] method is used to generate a random number in a given range, we can specify the range to be 0 to the length of the list, and get the index, and then the corresponding value.
Python3
import
random
test_list
=
[
1
,
4
,
5
,
2
,
7
]
print
[
"Original list is : "
+
str
[test_list]]
rand_idx
=
random.randrange[
len
[test_list]]
random_num
=
test_list[rand_idx]
print
[
"Random selected number is : "
+
str
[random_num]]
Output:
Original list is : [1, 4, 5, 2, 7] Random selected number is : 7
Using random.randint[] to select random value from a list
random.randint[] is used to generate the random number, also this can be used to generate any number in a range, and then using that number, we can find the value at the corresponding index, just like the above-mentioned technique. But it differs by the fact that it requires 2 mandatory arguments for range.
Python3
import
random
test_list
=
[
1
,
4
,
5
,
2
,
7
]
print
[
"Original list is : "
+
str
[test_list]]
rand_idx
=
random.randint[
0
,
len
[test_list]
-
1
]
random_num
=
test_list[rand_idx]
print
[
"Random selected number is : "
+
str
[random_num]]
Output:
Original list is : [1, 4, 5, 2, 7] Random selected number is : 4
Using random.random[] to select random value from a list
random.random[] method generates the floating-point numbers in the range of 0 to 1. We can also get the index value of list using this function by multiplying the result and then typecasting it to integer so as to get the integer index and then the corresponding list value.
Python3
import
random
test_list
=
[
1
,
4
,
5
,
2
,
7
]
print
[
"Original list is : "
+
str
[test_list]]
rand_idx
=
int
[random.random[]
*
len
[test_list]]
random_num
=
test_list[rand_idx]
print
[
"Random selected number is : "
+
str
[random_num]]
Output:
Original list is : [1, 4, 5, 2, 7] Random selected number is : 7
Using random.sample[] to select random value from a list
Python has a built-in function called random.sample[]. The random module contains the random.sample[] function. It has the ability to choose multiple items from a list.
Python3
import
random
test_list
=
[
1
,
4
,
5
,
2
,
7
]
print
[
"Original list is : "
+
str
[test_list]]
print
[
"Random element is :"
, random.sample[test_list,
5
]]
Output:
Original list is : [1, 4, 5, 2, 7] Random element is : [7, 4, 1, 5, 2]
Using random.choices[] to select random value from a list
The random.choices function is stored in the random module []. Selecting numerous items from a list or a single item from a particular sequence is handy with the help of random.choices function.
Python3
import
random
test_list
=
[
11
,
44
,
55
,
22
,
77
]
print
[
"Original list is : "
+
str
[test_list]]
print
[
"Random element is :"
, random.choices[test_list, k
=
4
]]
Output:
Original list is : [11, 44, 55, 22, 77] Random element is : [11, 11, 44, 77]
Select k random value from a list
Here we have grouped all elements in a pair of size k.
Python3
import
random
def
select_random_Ns[l, k]:
random.shuffle[l]
res
=
[]
for
i
in
range
[
0
,
len
[l], k]:
res.append[l[i:i
+
k]]
return
res
l
=
[
'G'
,
'E'
,
'E'
,
'K'
,
'S'
,
'F'
,
'O'
,
'R'
,
'G'
,
'E'
,
'E'
,
'K'
,
'S'
]
print
[select_random_Ns[l,
3
]]
Output:
[['G', 'G', 'R'], ['K', 'K', 'E'], ['O', 'F', 'E'], ['S', 'E', 'S'], ['E']]