Random Module in Python Programming
Modules in Python
In this lesson, we will understand what is Random Module in Python Programming and how to implement them along with some examples.
What is Random Module in Python?
The Random module is used in python to generate random numbers. It contains important functions that generate random numbers between a given range.
Let's discuss the important functions of random module one by one with examples.
random.choice()
The random.choice() function returns a randomly selected item from a sequence. The sequence can be a string, list, tuple or any other sequence.
Example
import random
st = "PYTHON"
animals = ['Tiger', 'Elephant', 'Snake', 'Duck', 'Cat', 'Dog']
print('Pick a random character from the string PYTHON: %s' %(random.choice(st)))
print('Pick a random item from the animals list: %s' %(random.choice(animals)))
Output
Pick a random character from the string PYTHON: T Pick a random item from the animals list: Duck
random.choices()
The random.choices() function returns a list that contains a randomly selected items from a sequence. We can also specify the possibility of selecting an item from a sequence over other items and the number of items the returned list will contain.
Example
import random
animals = ['Tiger', 'Elephant', 'Cat', 'Dog']
print(random.choices(animals, weights=[5,3,1,1], k=10))
Output
['Dog', 'Tiger', 'Cat', 'Dog', 'Elephant', 'Cat', 'Cat', 'Tiger', 'Elephant', 'Tiger']
The weights=[5, 3, 1, 1] means that the possibility of selecting Tiger is 5 times more than the other three items. The k=10 means that the return list will contain 18 randomly selected items.
Note: The output will be different every time as the program returns randomly selected items from the specified list.
random.shuffle()
The random.shuffle() function changes the element's positions in a list.
Example
import random
animals = ['Tiger', 'Elephant', 'Cat', 'Dog']
random.shuffle(animals)
print(animals)
Output
['Elephant', 'Tiger', 'Cat', 'Dog']
random.random()
The random.random() function returns a random floating point number between the range 0.0 to 1, in which 0.0 and 1 are excluded from random floating point numbers.
Example
import random
print(random.random())
Output
0.714437035908325
random.uniform()
The random.uniform() function returns a random floating point number from a specified start and stop range where both start and stop are included in the random floating point numbers.
Example
import random
print(random.uniform(1,5))
Output
2.953048089322235
random.randint()
The random.randint() function returns a random integer number from a specified start and stop range where the start integer is included, but the stop integer is excluded from the random numbers.
Example
import random
print(random.randint(1, 10))
Output
5
random.randrange()
The random.randrange() function returns a random integer number from a specified start and stop range with an optional step integer. If we want to randomly select an even number between the range of 10 to 50, then the step value will be 2.
In random.randrange() function, the start integer is included, but the stop integer is excluded from the random numbers.
Example
import random
print(random.randrange(10, 50, 2))
Output
18