# How to use Python random.seed() function

This article demonstrates how to use `random.seed()`

function to initialize the pseudo-random number generator in Python to get the deterministic random data you want. By setting the custom seed value we can get the determined sequence of random numbers.

**Goals of this lesson. **In this lesson you’ll learn how to:

- Use
`random.seed()`

to initialize the pseudo-random number generator. - Choose the same elements from the list randomly every time using
`random.seed()`

- Use a
`random.seed()`

function with other random module functions.

## How to use random.seed() function

Let’s understand the working of a random module before proceeding further. The random number or data generated by Python’s random module is not truly random; it is pseudo-random(it is PRNG), i.e., deterministic. The random module uses the seed value as a base to generate a random number.

**Syntax of random.seed()**

`random.seed(`*a*, *version*)

Parameter

**a**: Optional. The seed value needed to generate a random number.

If it is an integer it is used directly, if not it has to be converted into an integer. The default value is None, and if None, the generator uses the current system time.**version**: An integer specifying how to convert the`a`

parameter into an integer. Default value is 2

The seed value is very significant in the field of computer security to pseudo-randomly generate a secure secret encryption key. So using a custom seed value, you can initialize the strong and reliable pseudo-random number generator the way you want.

Also, `random.seed()`

is useful to reproduce the data given by a pseudo-random number generator. By re-using a seed value, we can regenerate the same data multiple times as multiple threads are not running.

When we supply a specific seed to the random generator, every time you execute a program, you will get the same numbers. That is useful when you need a **predictable source of random numbers**.

## Example to generate the same random number every time using random.seed()

If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function. Let’s see how to set random seed in Python.

`import random`

print ("Random number with seed 88")

random.seed(88)

print ("first Number ", random.randint(1,10))

random.seed(88)

print ("Second Number ", random.randint(1,10))

random.seed(88)

print ("Third Number ", random.randint(1,10))

We got the same number as a result because we seeded them with the same value before calling `random.randint()`

.

If you call `random.randint()`

twice before calling `random.seed()`

, you will get a different number. If If you want to change your output, then pass the different seed values before calling any other random module function.

`import random`

print("Random number with seed 88")

# first call

random.seed(88)

print("first Number", random.randint(1, 10))

# generate a different random number

print("Second Number ", random.randint(1, 10))

# will generate a same random number as first one because seed value is same

random.seed(88) # Third call

print("Third Number", random.randint(1, 10))

# Use random seed and sample function together

Using a random.sample() function, we can select multiple random items from the list and other sequence types. Let’s see how to get the same random items out of the list every time using seed() and sample() function.

`import random`

# using random.seed() and random.sample() together

fruit_list = ["Apple", "Mango", "Banana", "Apricot", "Cherries", "Grape", "Kiwi"]

random.seed(32)

sample_list = random.sample(fruit_list, 3)

print("First sample fruit list ", sample_list)

random.seed(32)

sample_list = random.sample(fruit_list, 3)

print("Second sample fruit list ", sample_list)

## Use random seed and shuffle function together

We can also use the seed and random.shuffle() function together. The primary purpose of using seed and shuffle function together is to produce the same result every time after each shuffle. If we set the same seed value every time before calling the shuffle function, we will get the same item sequence. I.e., shuffling produces the same result every time.

Let see how to use seed() and shuffle() function together.

`import random`

numbers = [10, 20, 30, 40, 50, 60]

print("Original list: ", numbers)

random.seed(42)

random.shuffle(numbers)

print("Shuffled list ", numbers)

numbers = [10, 20, 30, 40, 50, 60]

random.seed(42)

random.shuffle(numbers)

print("Reshuffled list ", numbers)