# Python random.seed() function

The **random module** in **Python** produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random numbers across various executions of the code on the same or different machines.

The generator's previous value number serves as the seed value. It uses the current system time for the first time if there isn't a previous value. A pseudo-random number generator uses the seed value as the starting point to produce random numbers.

Use a **random.seed() function** in conjunction with other random module functions to repeatedly reproduce their output. The random module generates a random number using the seed value as a starting point.

## When and Why to use the seed() function?

The seed value is very important in computer security because it is used to generate a secure secret encryption key pseudo-randomly. Therefore, you can initialise the trustworthy and robust pseudo-random number generator however you like by using a custom seed value. Additionally, data produced by a pseudo-random number generator can be replicated using random.seed (). Due to the absence of multiple threads, we can recreate or regenerate the same data repeatedly using a seed value.

You will receive the same numbers each time you run a program if we provide a specific seed to the random generator. It is helpful when you need a consistent source of random numbers.

## How to use random.seed() function

**Syntax**

random.seed(a=None, version=2)

It is the initial value. The system time is automatically used if the a is None.

Instead of using the system time, the operating system's randomness sources are employed. On Windows, for example, os.urandom() generates random data internally by calling CryptGenRandom().

When you pass a seed value in the form of an integer, it is used when it is needed.

If you do not initialize the pseudo-random number generator, the random generator sets the seed value using the OS's randomness sources. As a result, each time we call it random.random(), we get a different result. Python defaults to using the current system time as a seed value whenever it cannot access the OS-specific randomness source.

Use a random seed to produce the same number each time ().

If a programmer wishes to create the same number consistently, you must give the same seed value before calling any additional random module functions. Let's look at changing the seed in Python's pseudo-random number generator.

**Example**

```
import random
print(“Random number with seed 20”)
For i in range(2):
#random number with seed 20
Random.seed(20)
print(random.randint(15,40))
```

**Output**

```
random number with seed 20
38
38
38
```

We grouped them with the same value before calling random.randint, which is why we obtained the same number three times; as we can see from the output (), before calling seed(), users should call random.randint() twice to generate a different number each time. Pass the desired seed value before calling any other random module functions.

**Example**

```
import random
random.seed(30)
print(“first number”, random.randint(25,50))
print(“second number”, random.randint(25,50))
random.seed(30)
print(“third number”, random.randint(25,50))
```

**Output**

```
first number 42
second number 50
third number 42
```

### Getting a seed value by using a random generator

Reproducing data produced by a pseudo-random number generator can occasionally be useful. Because we seeded them with the same value before calling, we received the same number three times, as we can see from the output. Because no other threads are running, we can regenerate the same data multiple times by reusing a seed value.

As you may know, random data generation depends on a seed value. Because multiple threads are not running, we can regenerate the same data multiple times by reusing a seed value.

When you want reproducible results, you must use the current seed value. For example, suppose you want to replicate the results of a specific run. In such cases, you'd like to know the seed used to replicate the result.

When using a custom seed value, keep in mind that Python's Random generator does not store the seed in memory. It does not provide a method to obtain the current seed value. If you want to reuse the seed, you must save it.

### What is the purpose of np.random.seed(0) ?

In **NumPy**, we use np.random.seed to generate random numbers or simulate random processes. Because computers are generally deterministic, it is extremely difficult to generate truly "random" numbers on them. By using pseudo-random number generators, computers avoid this. These algorithms for pseudo-random number generation produce numbers that seem random but are not. Pseudorandom number generators require a starting input to function properly. This initial input is referred to as a "seed."

You can use the code to provide a seed for NumPy's pseudo-random number generator. random.seed(0). Then, to produce different kinds of random outputs, NumPy uses the seed and pseudo-random number generator along with other functions from the numpy. random namespace. Finally, using this method to generate pseudo-random numbers yields repeatable results, which are helpful for testing and code sharing. Understanding numpy. random.seed requires some knowledge of pseudo-random number generators.

**Exampe**

```
np.random.seed(0)
np.random.randint(99, size = 5)
```

**Output**

array([44, 47, 64, 67, 67])

In essence, an array of 5 integers with values ranging from 0 to 99 was produced by np. Random. randint. You will receive the same integers from np. Random.randint, it is important to note if you run this code again with the same seed. A different set of integers were produced by NumPy random randint using a different seed.

Everything else remains constant. The code is the same for np.random.randint. However, with a different seed, it yields a different result.

A number (the seed) is multiplied by a significant number, an offset is added, and the modulo of the result is then used to generate random numbers. The final number is then used as the seed for the following "random" number. Every time the seed is planted, the same action is taken, and the same outcomes are obtained.

If you want seemingly random numbers, set the seed. Setting the seed before each run can be very helpful for debugging code that uses random numbers because it ensures that the code behaves consistently.

To obtain the most random numbers for each run, call numpy.random.seed (). If neither of these is available, the clock is used to set the seed instead of a random number from /dev/urandom or its Windows equivalent.

### Random seed with randrange

**Example**

```
import random
random.seed(350)
print(random.randrange(300, 500))
random.seed(350)
print(random.randrange(300, 500))
```

**Output**

336

We used a random seed value to create training and test data sets. The goal is to use the same training and validation data sets while using different hyperparameters or machine learning algorithms to evaluate the performance of various models. Repeating or creating new pseudo-random numbers is possible using the numpy random seed. The value saves the randomness state in the numpy random seed. The computer repeatedly shows the same random numbers when we call the seed function with the value 1.

Pseudo-random number generators operate on values by performing some operation on them. This was typically the last number the generator produced. However, when you use the generator for the first time, there isn't a previous value.

### Advantages of random.seed(0)

Encryption keys are an essential part of computer security for dominant future provisions. These secret keys are used to safeguard data from unauthorized internet access. This function generates a pseudo-random encryption key.

When random numbers are used for testing, it simplifies code optimization. The output of the code is sometimes dependent on the input. As a result, testing algorithms with random numbers can be difficult.

Also, the seed function generates the same random numbers repeatedly, simplifying the algorithm testing process. A pseudo-random number generator is given its first "previous" value when seeded. Each seed value corresponds to a series of generated values for a specific random number generator. Random coordinate permutations are used to reduce the centered discrepancy.

Based on the centered discrepancy, the best design is continually updated. Compared to other measures, the centered discrepancy-based design exhibits better space-filling robustness toward 2D and 3D subprojects. In other words, you will get the same number of sequences if you use the same seed twice. A random seed is employed to ensure that the outcomes can be repeated. In other words, using this parameter ensures that anyone who runs your code again will get the same results. Reproducibility is a crucial concept in data science and other fields.