## Introduction to Random Number Generator in Python

Python is one of the top languages currently in the world of programming languages. The USP of python is the open-source libraries that can be used to do all kinds of statistical and scientific tasks with minimal code as these libraries have all the inbuilt algorithms to do advanced tasks. This is one of the major reasons for the popularity of python especially in the field of data science. We are going to discuss the Random Number Generator in Python.

### What is 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. It is required in games, lotteries to generate any random number. It may also get required while writing code for a web application like you want an OTP to get generated for example. So it is better to know how to generate random numbers in Python.

For 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.

#### Choice()

It is an inbuilt function in python which 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. We have to note one important aspect that the sequence used cannot be empty. In case it is empty it will show Index error.

**Syntax:**

`import random`

sequence=[1,4,6,10]
random. choice(sequence) //Here sequence is list or tuple or string

4.8 (4,033 ratings)

View Course

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.

#### Random()

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 that the value returned will be a float.

**Example**

`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.

#### Randrange(Begin,End,Step)

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 is shown in below screenshot

#### Shuffle()

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.

**Example**

`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 in the second output the elements are the same but their positions have randomly changed. This is the use of shuffle() function.

#### Uniform(a,b)

This function returns a random number between two points a and b. point a is the lower limit which I included and point b is the upper limit which 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.

**Example**

`import random`

random.uniform(3,5)

Now let’s run the same code in 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. To generate random integer values we can use the randint() function from the random module of python and seed function

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.

**Example**

`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. We will use seed function which takes an integer value as an argument. Since we are giving the range as 5 so it will generate five random numbers as the for loop will iterate five times.

**Example**

`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.

### Conclusion

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.

### Recommended Articles

This is a guide to Random Number Generator in Python. Here we discuss the introduction and functions of Random Number Generator along with some examples. You may also look at the following articles to learn more –

- Constructor in Python
- 3d Arrays in Python
- Pointers in Python
- Python Comparison Operators
- Guide to Generate Random Number in C++
- 3D Arrays in C++
- Random Number Generator in Matlab
- Random Number Generator in C#
- Random Number Generator in PHP
- How Arrays and Lists Work in Python?
- Examples of Comparison Operators in PowerShell