# random.seed( ) in Python

## random.seed( ) in Python

`random()` function is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined.

`random()` function generates numbers for some values. This value is also called seed value.

How Seed Function Works ?
Seed function is used to save the state of a random function, so that it can generate same random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value). The seed value is the previous value number generated by the generator. For the first time when there is no previous value, it uses current system time.

Using `random.seed()` function

Here we will see how we can generate the same random number every time with the same seed value.

Example 1:

 `# random module is imported` `import` `random ` `for` `i ``in` `range``(``5``):` ` ` `    ``# Any number can be used in place of '0'.` `    ``random.seed(``0``)` ` ` `    ``# Generated random number will be between 1 to 1000.` `    ``print``(random.randint(``1``, ``1000``))  ` `    `

Output:

```865
865
865
865
865
```

Example 2:

 `# importing random module` `import` `random` ` ` `random.seed(``3``)` ` ` `# print a random number between 1 and 1000.` `print``(random.randint(``1``, ``1000``))` ` ` `# if you want to get the same random number again then,` `random.seed(``3``) ` `print``(random.randint(``1``, ``1000``))` ` ` `# If seed function is not used` ` ` `# Gives totally unpredictable responses.` `print``(random.randint(``1``, ``1000``))`

Output:

```244
244
607
```

On executing the above code, the above two print statements will generate a response 244 but the third print statement gives an unpredictable response.

Uses of random.seed()

1. This is used in the generation of a pseudo-random encryption key. Encryption keys are an important part of computer security. These are the kind of secret keys which used to protect data from unauthorized access over the internet.
2. It makes optimization of codes easy where random numbers are used for testing. The output of the code sometime depends on input. So the use of random numbers for testing algorithms can be complex. Also seed function is used to generate same random numbers again and again and simplifies algorithm testing process.

Last Updated on November 13, 2021 by admin

## Python – Find position of a character in given stringPython – Find position of a character in given string

Python | Find position of a character in given string Given a string and a

## How to install pip in macOS ?How to install pip in macOS ?

Before we start with how to install pip for Python on macOS, let’s first go

## Python String isalpha() MethodPython String isalpha() Method

Python String isalpha() Method Python String isalpha() method is a built-in method used for string handling. The

## Create a new column in Pandas DataFrame based on the existing columnsCreate a new column in Pandas DataFrame based on the existing columns

Create a new column in Pandas DataFrame based on the existing columns While working with

## Matplotlib.pyplot.xticks() in PythonMatplotlib.pyplot.xticks() in Python

Matplotlib.pyplot.xticks() in Python Matplotlib is a library in Python and it is numerical – mathematical extension

## Pandas dataframe groupby()Pandas dataframe groupby()

Python – Pandas dataframe groupby()   Pandas groupby is used for grouping the data according

## mode() function in Python statistics modulemode() function in Python statistics module

mode() function in Python statistics module The mode of a set of data values is the value

## Python String split() methodPython String split() method

Python String split() method split() method in Python split a string into a list of