Random Number Generation Functions
The random module of Python provides a variety of functions to manipulate and create random numbers.
The following include some common functions of the random module used for random number generation and manipulation:e”
Function | Description |
---|---|
randint(a,b) | Generates and returns a random number in a specified range. The term 'a,b' is the range. For example, if we write randint(1,7), then this function will return a random number value between 1 and 17. |
choice() | Selects and returns the element from the given numbers. The numbers could be in a container, i.e., a list. |
random() | Used to generate a float random number. Returns a float random number between 0 and 1. |
randrange() | Used to generate a random number between a given sequence. Takes the start value, end value, and a number that you want to exclude from your choice. |
shuffle() | Takes the container or list as an argument and changes the sequence of the elements. |
uniform() | Returns a float random number in a given range. |
These are the most common functions used to generate random numbers in Python.
Now, let us see some examples of these functions.
The randint(a,b) Function
The following is an example of the randint(a,b) function. In this example, we specify the range to (1,20) and generate a random number. The program will be run multiple times so that we can obtain different random numbers in the given range.
# importing the random moduleimport random
# printing the random number between 1 to 20
print("The random number is: ",random.randint(1,20))
Output
The output is displayed in the Python console. In the output, you can see that the random numbers were generated between 1 and 20.
If we change the range of the randint() function to (1,10), then a random number will be generated in the range between 1 to 10. The program will be run multiple times so that we can obtain different random numbers in the given range.
# importing the random moduleimport random
# printing the random number between 1 to 10
print("The random number is: ",random.randint(1,10))
Output
The output is displayed in the Python console. In the output, you can see that the random numbers were generated between 1 and 10.
The choice() Function
As discussed earlier, the choice() function selects and returns the element from the given numbers. The numbers could be in a container, i.e., a list. Let us see an example of the list.
# importing the random moduleimport random
# defining the list of numbers
list=[1,2,3,4,44,5,65,99,10,100]
# printing the random choice
print(random.choice(list))
Output
The output is displayed in the Python console. The program is executed multiple times so that we can obtain different random choices from the given list.
Similarly, we can define a list of strings and make a random choice by using the choice() function.
# importing the random moduleimport random
# defining the list of words
list=["Hello","Welcome","to","the","linuxhint"]
# printing the random choice
print(random.choice(list))
Output
The output is displayed in the Python console. The program is executed multiple times so that we can obtain different random choices from the given list.
The random() Function
The random function is used to generate a floating-point random number between 0 and 1.
# importing the random moduleimport random
# printing the random floating-point number between 0 and 1.
print(random.random())
Output
The output is displayed in the Python console. The program is executed multiple times so that we can obtain different random choices from the given list.
If we want to take the sum of one random number between 1 and 10, and one floating-point number, then we can do it in this way.
# importing the random moduleimport random
#declaring num1 variable and storing random number between 1 to 10
num1= random.randint(1,10)
#declaring num2 variable and storing random floating-point number between 0 to 1
num2= random.random()
# printing the sum of num1 and num 2
print("The sum is: ",num1+num2)
Output
The output is displayed in the Python console. Most probably, the new random numbers are generated every time. Therefore, the program is executed multiple times to obtain different sum values.
The randrange() Function
As discussed earlier, the randrange() function is used to generate a random number in a given sequence. It takes the start value, end value, and a number that you want to exclude from your choice.
# importing the random moduleimport random
#printing the random number between 1 to 10 and excluding number 2
print(random.randrange(1,10,2))
Output
The output is displayed in the Python console. The program is executed multiple times.
The shuffle() Function
The shuffle() function takes the container or list as an argument and changes the sequence of the elements.
# importing the random moduleimport random
# defining the list of numbers
list=[1,2,3,4,44,5,65,99,10,100]
#printing the original list
print("The original list is \n", list)
#shuffling the list by calling the shuffle() function
random.shuffle(list)
# printing the shuffled list
print("The shuffled list is \n",list)
Output
The output is displayed in the Python console.
Similarly, we can also shuffle the list of words using the shuffle() function.
# importing the random moduleimport random
# defining the list of words
list=["Hello","Welcome","to","the","linuxhint"]
#printing the original list
print("The original list is \n", list)
#shuffling the list by calling the shuffle() function
random.shuffle(list)
# printing the shuffled list
print("The shuffled list is \n",list)
Output
The output is displayed in the Python console.
The uniform() Function
The uniform() function returns the float random number in a given range.
# importing the random moduleimport random
#printing the random floating-point number between 1 to 10
print(random.uniform(1,10))
Output
The output is displayed in the Python console. The program is executed multiple times to generate multiple floating-point numbers between 1 and 10.
Conclusion
This article explains random number generation in Python with some simple examples. There are various functions in Python that you can use for random number generation, including randint(), random(), and more. This article should help beginners to better understand random number generation in Python.