Updated April 17, 2023
Introduction to Random Number Generator in Python
A random number generator is a method or a block of code that generates different numbers every time it is executed based on a specific logic or an algorithm set on the code with respect to the client’s requirement. Python is a broadly used programming language that allows code blocks for functional methods like the random number generator. A few examples of this function are one-time password (OTP) generators for online banking, for login of any web-based application, gaming tools for choosing random opponents, token generation for accessing the secured environment, etc.
What is a Random Number Generator in Python?
A random number generator is a code that generates a sequence of random numbers based on some conditions that cannot be predicted other than by random chance. Random Number Generation is important while learning or using any language. For example, it is required in games, lotteries to generate any random number. It may also be required while writing code for a web application like you want an OTP generated. So it is better to know how to generate random numbers in Python.
For a random number generator, we will use a random package of python, which is inbuilt in python. It has many inbuilt functions inside it, which can be used to generate random numbers based on our requirements.
Random Number Generator Functions in Python
We are going to discuss below some random number functions in Python and execute them in Jupyter Notebook.
It is an inbuilt function in python that can be used to return random numbers from nonempty sequences like list, tuple, string. An example of this would be to select a random password from a list of passwords. However, we have to note one important aspect that the sequence used cannot be empty. In case it is empty, it will show an Index error.
import random sequence=[1,4,6,10] random. choice(sequence) #Here sequence is list or tuple or string
Now, we will see the output of the above example when executed in Jupyter Notebook.
As you can see, the output is randomly selected as 6.
This function, as the name suggests, returns a float random number between 0.0 and 1.0. So the lower limit is 0.0, and the upper limit is 1.0. One thing to note is that the value returned will be a float.
import random random.random()
Now we will run the code in Jupyter Notebook and see the output for the same. The below screenshot shows the output.
As we can see, the value returned is between 0.0 and 1.0.
This function returns a random based on the parameters supplied; as we can see, it has three parameters.
- Begin: This parameter says from where to begin. It will be included in the range.
- End: This parameter says where to stop. It is excluded from the range.
- Step: It is to skip numbers in range.
Example with Syntax:
import random random.randrange(10,20,2)
Now let’s run this example in Jupyter notebook and see the result. The operation and result are shown in the below screenshot.
This function takes two parameters. The syntax of the function is random.shuffle(x,random). In this, the parameter random is optional, whereas the x stands for sequence. This function returns a randomized sequence which means the places of the elements in the sequence are randomized, but the values remain the same. To better understand, we will write a few lines in python.
import random num_list = [7,8,10,12] print("List before using shuffle: ",num_list) random.shuffle(num_list) print("List after using shuffle method: ", num_list)
We will run the above instructions in Jupyter Notebook and have a look at the output.
As we can see above, the elements are the same in the second output, but their positions have randomly changed. This is the use of the shuffle() function.
This function returns a random number between two points a and b. point a is the lower limit I included, and point b is the upper limit that is not included. It takes two parameters, as can be seen. It should not be confused with random. random() as it is used to generate a number between 0 and 1, whereas this function is used to generate in a range.
import random random.uniform(3,5)
Now let’s run the same code in the Jupyter notebook.
As you can see, the random number returned is between 3 and 5.
Generation of Integers
Now we are going to generate random integers. We can use the randint() function from the random module of python and the seed function to generate random integer values.
It takes an integer value as an argument. This type of function is called deterministic, which means they will generate the same numbers given the same seed. In case we do not use the same value in the seed, the numbers generated will be different. We are going to call the seed function before using randomness.
from random import seed from random import randint #to generate seed number seed(101) #random number generation within 0 to 5 for _ in range(5): value = randint(0,5) print(value)
Now let’s run this code in Jupyter Notebook.
Generating Float Point Numbers
Now we are going to generate float point numbers. To generate random float point numbers, we are going to use the random() function, which will return random float point numbers between 0 and 1. Next, we will use the seed function, which takes an integer value as an argument. Since we are giving the range as 5, it will generate five random numbers as the for loop will iterate five times.
from random import seed from random import random #to generate seed number seed(101) #random float number generation for _ in range(5): value = random() print(value)
Now let’s run this code in Jupyter Notebook.
As you can see, we get five random float point numbers.
To conclude this article, we can say that random number becomes very useful in several applications, and there are different ways by which we can generate random numbers.
We hope that this EDUCBA information on “Random Number Generator in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.