Suppose a situation arises where we want the computer to choose a random integer within a defined range, a random element from a list, a random card from a deck, flip a coin, and so on. For such purposes, we use the Python import random module. In this article, we will focus on the Python import random module, along with its different functions, its programming applications, and a program to generate random numbers in Python.
Python Random Module
First, we need to import the random module as following:
import random
The Python import random module in Python defines a series of functions for generating or manipulating random integers. Python random() is a pseudo-random number generator function that generates a random float number between 0.0 and 1.0, is used by functions in the random module.
Because the sequence of numbers created is determined by the seed, these are pseudo-random numbers. The sequence will be the same if the seeding value is the same. If you pick 2 as the seeding value, for example, you will always see the following sequence. These functions are used in a variety of games, lotteries, and other applications that require random number creation.
Example
import random
random.seed(5)
print(random.random())
print(random.random())
print(random.random())
Output
0.6229016948897019
0.7417869892607294
0.7951935655656966
Python Program to Generate Random Floats
The Python import random module is also used to generate random float values. This is done by using random.random() method that helps generate a random float value ranging from 0.0 to 1.0. There are no arguments required for the function.
Example
import random
print('Printing random number using random.random():')
print(random.random())
Output
Printing random number using random.random():
0.909824521137017
As you can see from the results, we got 0.90. It’s possible that you’ll obtain a different number.
- The random module’s most fundamental function is random.random().
- Almost all of the random module’s functions are dependent on the fundamental function random().
- random() returns the next random floating-point number between [0.0, 1.0]
Python Program to Generate Random Integers
In Python, the randint() method is used to generate a random number between the supplied integers. The random module defines this function. The syntax of this function is:
random.randint(a, b)
where,
a – starting integer value
b – ending integer value
This yields a number N in the inclusive range [a,b], implying that a <= N <= b, where the endpoints are included.
Example
# Program to generate a random number
# importing the random module
import random
print('Random number:', random.randint(0,10))
print('Random number:', random.randint(0,50))
print('Random number:', random.randint(0,100))
Output
Random number: 5
Random number: 32
Random number: 84
Note – Every time we run the above program, the Python interpreter will generate a different number.
Python Program to Generate Random Number from a List and a String
Python random.choice() is a built-in Python function that returns a randomly selected item from a list, tuple, or string.
The syntax is as follows:
random.choice(iterable)
where,
iterable – a sequence of list, tuple or a string
If the iterable passed to random.choice() is empty, Python interpreter will raise an exception.
Example
# Python program to illustrate choice() method
import random
# prints a random value from the list
list1 = [8, 5, 6, 2, 0, 4]
print('Random number from list:', random.choice(list1))
# prints a random item from the string
string = '9674271903'
print('Random number from string:', random.choice(string))
Output
Random number from list: 8
Random number from string: 3
List of Functions used to Generate Random Numbers
Function | Description |
getrandbits(k) | This function returns a Python integer with k random bits. |
randrange(start, stop, step) | By giving the step increment, this function returns a random integer number within a range. |
randint(a, b) | Create a random integer number between the range a to b. |
choice(seq) | Return an element from the non-empty sequence at random. |
random() | Return the next random floating point number between [0.0, 1.0]. |
uniform(a, b) | Returns a random floating-point value within a given range of a to b. |
triangular(low, high, mode) | Create a random floating-point number N with the specified mode between those bounds and low <= N <= high. |
Frequently Asked Questions
Q1. What is import random in Python?
The random module in Python defines a series of functions for generating or manipulating random integers. The import random loads the random module, which contains a number of random number generation-related functions. Python random() is a pseudo-random number generator function that generates a random float number between 0.0 and 1.0, is used by functions in the random module.
Q2. How do you import a random number in Python?
In Python, the randint() method is used to generate a random number between the supplied integers. The random module defines this function. The syntax of this function is:
random.randint(a, b)
Example
import random
print('Random number:', random.randint(0,5))
print('Random number:', random.randint(0,20))
print('Random number:', random.randint(0,50))
Output
Random number: 5
Random number: 19
Random number: 39
Q3. How do you generate a random number from a list?
Python random.choice() is a built-in Python function under the random module that returns a randomly selected item from a list, tuple, or string.
The syntax is as follows:
random.choice(iterable)
Example
# Python program to illustrate choice() method
import random
# prints a random value from the list
my_list = [2, 4, 6, 8, 3, 5, 7]
print('Random number from list:', random.choice(my_list))
print('Random number from list:', random.choice(my_list))
Output
Random number from list: 3
Random number from list: 5
Leave a Reply