 # 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

## Random Numbers in PythonRandom Numbers in Python

Random Numbers in Python Python defines a set of functions that are used to generate or manipulate random numbers through the random module. Functions in the random module rely on a pseudo-random number generator function random(), which generates a random float number between 0.0 and 1.0. These particular

## Python program to count words in a sentencePython program to count words in a sentence

Python program to count words in a sentence Data preprocessing is an important task in text classification. With emergence of Python in the field of data science, it is essential to have certain shorthands to have upper hand among others.

## How to Encrypt and Decrypt Strings in Python?How to Encrypt and Decrypt Strings in Python?

How to Encrypt and Decrypt Strings in Python? In this article, we will learn about Encryption, Decryption and implement them with Python. Encryption: Encryption is the process of encoding the data. i.e converting plain text into ciphertext. This conversion is

## Start and stop a thread in PythonStart and stop a thread in Python

Start and stop a thread in Python The threading library can be used to execute any Python callable in its own thread. To do this, create a Thread instance and supply the callable that you wish to execute as a target as

## Python program to add two numbersPython program to add two numbers

Given two numbers num1 and num2. The task is to write a Python program to find the addition of these two numbers. Examples: Input: num1 = 5, num2 = 3 Output: 8 Input: num1 = 13, num2 = 6 Output:

## Python – Check if dictionary is emptyPython – Check if dictionary is empty

Python | Check if dictionary is empty Sometimes, we need to check if a particular dictionary is empty or not. And this particular task has its application in web development domain in which we sometimes need to test for results

## Returning Multiple Values in PythonReturning Multiple Values in Python

Returning Multiple Values in Python In Python, we can return multiple values from a function. Following are different ways 1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple

## Union() function in PythonUnion() function in Python

Union() function in Python Python set Union() function returns a new set which contains all the items from the original set. Union of two given sets is the smallest set which contains all the elements of both the sets. Union of two