This article** demonstrates how to seed the random number generator in Python to generate pseudo-random numbers. **Let’s learn the use of a `seed ()`

function of a random module to initialize the pseudo-random number generator to **generate the deterministic random data** you want.

- The article is part of a series
**Generate Random Data In Python.** - Solve our Free
**Python random data generation Exercise.****And**to master random data generation techniques.

Python Random data generation Quiz

**Goals of this lesson: –**

- Understand how to use
`random.seed()`

to initialize the pseudo-random number generator. - Determine the
**right seed value**to generate the deterministic random data you want. - choose single and multiple elements that you want from the list by using
`random.seed()`

. - Use a
`random.seed ()`

function with other random module functions.

## How to use random.seed() function

Let’s discuss **Few essential points on the working of a random module** before proceeding further.

Random number or data generated by **Python’s random module is not truly random, it is pseudo-random**(it is PRNG), i.e. deterministic. It produces the numbers from some value. This value is nothing but a seed value. i.e. The random module uses the seed value as a base to generate a random number.

Generally, the seed value is the previous number generated by the generator. However, When the first time you use the random generator, there is no previous value. So **by-default current system time is used as a seed value. **

**Note**: If you don’t initialize the pseudo-random number generator using a

`random.seed ()`

, internally random generator call the seed function and use current system current time value as the seed value. That’s why whenever we execute `random.random()`

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

Let see how to use the `random.seed()`

function.

**Syntax of random.seed()**:

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

This function accepts two arguments. **Both are the optional arguments**.

. If the`a`

is the seed value`a`

value is`None`

, then**by default, current system time is used**. If seed value is in the form of an integer is used as it is.- Version 2 is the default version. In this version, a string, bytes, or byte-array object gets converted to an int.

### 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 every time** before calling any other random module function. Let see how to use `random.seed()`

function to generate deterministic random data.

import random print ("Random number with seed 30") random.seed( 30 ) print ("first - ", random.randint(25,50)) #will generate a same random number as previous random.seed( 30 ) print ("Second - ", random.randint(25,50)) #will generate a same random number as previous random.seed( 30 ) print ("Third - ", random.randint(25,50))

Output: Execute Online

Random number with seed 30 first - 42 Second - 42 Third - 42

We got the same number as a result because** we passed the same seed value every time before calling random.randint().**

**Note**: If you call `random.randint()`

twice before calling `random.seed()`

you will get the different value. Moreover, if If you want to change your output then pass the different seed value before calling any other random module function. Let see the example.

import random print ("Random number with seed 30") random.seed( 30 ) #first call print ("first - ", random.randint(25,50)) #generate a different random number as previous value is used as a seed print ("Second - ", random.randint(25,50)) #will generate a same random number as first one because seed value is same random.seed( 30 ) # second call print ("Third - ", random.randint(25,50))

Output: **Execute Online**

Random number with seed 30 first - 42 Second - 50 Third - 42

As you can see we got a different number in the second place because we executed `random.randint()`

twice. So second execution used the previous random value generated by the generator as a seed value. i .e. it used 42 as a seed value.

**Random data generation Exercise**to master random data generation techniques in Python.

## Python random seed with randrange

Let see how to use seed() function to get the same **random number between the given range**.

#Random seed with range random.seed(350) print ("first random number between given range - ", random.randrange(300,500)) random.seed(350) print ("Second random number between given range - ", random.randrange(300,500)) random.seed(350) print ("Third random number between given range - ", random.randrange(300,500))

Output: **Execute Online**

first random number between given range - 392 Second random number between given range - 392 Third random number between given range - 392

## Use the Random seed and Choice method together

As you know, the **random.choice() **function is used to choose a random element from the list and set. what if you want to generate the same choice every time. Using seed and choice function together we can do this.

Let see how to use a `random.seed()`

and `random.choice()`

function together.

list = [100,200,300,400,500,600] random.seed(6) random_item = random.choice(list) print ("First random item from list ", random_item) random.seed(6) random_item = random.choice(list) print ("Second random item from list ", random_item) random.seed(6) random_item = random.choice(list) print ("Third random item from list ", random_item)

Output: **Execute Online**

First random item from list 500 Second random item from list 500 Third random item from list 500

## Find Seed value of a number to generate the random number that you want

Most of the time you want to **choose the random number that you want,** This is important when you want reproducible results.

As you already know Python random number generator is a pseudo-random number generator, i.e., it is deterministic, random number generation is dependent on a seed value.

**How to find seed root or seed value of a number?**

A number A is said to be ‘seed’ of number B if multiplying A by its digit equates to B. For example, 32 is a seed of 192 because of 32*3*2=192.

So let’s see how to choose the right seed value to generate the random data that you want. I have created an example which calculates the all seed value of a given number.

def prod(num): temp=num while(num!=0): temp*=num%10 num/=10 return temp number =24 i = 1 print("Finding seed value") print("Seed value of ", number,"is ") while(i*i<=number): if(number%i==0): if(prod(i)==number): print(i) if(number/i!=i and prod(number/i)==number): print(number/i) i+=1

Output: **Execute Online**

Seed value of , 192, is 32 24

Now we can use the same seed value in our code and generate the 516 number randomly. Let see this with an example.

import random list = [120, 230, 192, 45, 516, 456, 729] print ("Random number with seed 24") random.seed(24) #first call print ("First random choice is - ", random.choice(list)) # without any seed value print ("Second random choice without seed is - ", random.choice(list)) # with our seed value 24 random.seed(24) # Third call print ("Third random choice is - ", random.choice(list))

Output: Execute Online

Random number with seed 24 First random choice is - 456 Second random choice without seed is - 45 Third random choice is - 456

## Use random seed and sample function together

We can also use the seed and **random.sample() **function together. As you know using sample function, we can generate multiple random items from the list and other sequence types.

If you want to generate the same random items out of the list every time, then **set the same seed value before calling a sample** function.

Let see how to use a `random.seed()`

and `random.sample()`

function together.

import random #using random.seed() and random.sample() together fruit_list = ["Apple", "Mango", "Banana", "Apricot", "Cherries", "Grape", "Kiwi"] random.seed(3) sample_list = random.sample(fruit_list, 3) print("First sample fruit list ", sample_list) random.seed(3) sample_list = random.sample(fruit_list, 3) print("Second sample fruit list ", sample_list) random.seed(3) sample_list = random.sample(fruit_list, 3) print("Third sample fruit list ", sample_list)

Output: **Execute Online**

First sample fruit list ['Mango', 'Cherries', 'Grape'] Second sample fruit list ['Mango', 'Cherries', 'Grape'] Third sample fruit list ['Mango', 'Cherries', 'Grape']

## Use the random’s 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., **It is possible to shuffle list with a parameter such that the 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(4) random.shuffle(numbers) print("reshuffled list ", numbers) numbers = [10, 20, 30, 40, 50, 60] random.seed(4) random.shuffle(numbers) print("reshuffled list ", numbers)

Output: **Execute Online**

Original list: [10, 20, 30, 40, 50, 60] reshuffled list [40, 60, 50, 10, 30, 20] reshuffled list [40, 60, 50, 10, 30, 20]

## Next Steps

To practice what you learned in this article, I have created a Python random data generation Quiz and Exercise project.

- Solve our
**Python Random data generation Quiz**to test your random data generation concepts. - Solve the
**Free Random data generation Exercise**to practice and master the random data generation techniques in Python.

Let me know your comments and feedback in the section below.

pieterdetweede

A number A is said to be ‘seed’ of number B if multiplying A by its digit equates to B. For example, 32 is a seed of 192 because of 32*2*2=192.

– I think the last bit should be 32*3*2=192

Vishal

Yes right. It was a typing mistake.