GENERATING RANDOM NUMBER LIST IN PYTHON
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.
import random
n = random.random()
print(n)
Output
0.2112200
Generating Number in a Range
The randint() method generates a integer between a given range of numbers.
import random
n = random.randint(0,22)
print(n)
Output
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.
import random
randomlist = []
for i in range(0,5):
n = random.randint(1,30)
randomlist. append(n)
print(randomlist)
Output
[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.
import random
#Generate 5 random numbers between 10 and 30
randomlist = random. sample (range(10, 30), 5)
print(randomlist)
Output
[16, 19, 13, 18, 15]
Following functions deal with floating point random numbers
random.random() − This function randomly generates a floating point number between
0.0 and 1.0
>>> random.random()
0.668544544081956
random.uniform() − This function returns a floating point random number between two
parameters.
>>> random.uniform(0.5,1.5)
1.2760281470664903
>>> random.uniform(1,10)
7.336985794193224
>>> random.uniform(10,5)
7.817794757786727
Following functions act upon sequence objects viz. string, list or tuple
random. choice() − This function picks a random element from the sequence. If the
sequence is empty, IndexError is thrown.
>>> random.choice("Tutorialspoint")
'o'
>>> random.choice(range(10))
2
>>> random.choice([15,31,6,29,55, 5])
55
>>> random.choice((15,31,6,29,25, 55))
15
random.choices() − This function chooses multiple elements from a list in random
manner. First parameter to this function is the sequence and second parameter is the
number of choices to be made.
>>> name = "TutorialsPoint"
>>> random.choices(name, k = 2)
['T', 'o']
random.shuffle() − This function reorders elements in a mutable sequence and places
them randomly.
>>> num = [10,20,30,40,50]
>>> random.shuffle (num)
>>> num
[50, 20, 40, 30, 10]
Example: This function shuffles the list and randomly arranges them.
list = [1, 3, 5, 10, 4]
print (" list before shuffling: ", end="")
for j in range (0, len (list)):
print (list[j], end=" ")
print("\r")
random.shuffle (list)
print ("list after shuffling: ", end="")
for j in range(0, len(list)):
print (list[j], end=" ")
print("\r")
Output:
list before shuffling : 1 3 5 10 4
list after shuffling : 3 10 1 4 5
Source: https://www.tutorialspoint.com/random-numbers-in-python