Seed in Python

This is not genuinely random; rather, it is employed to produce pseudo-random values. This suggests that these random values can be predicted. For some instances, the random() method creates numbers. This quantity is also known as the seed value.

Syntax

Parameters:

i: Any value that is used as a seed to produce the random integer.

version: An integer specifies how to turn l into an integer.

Returns: A random value.

How does the Seed Function Works?

The seed method saves the state of the random number generator so that the generator can create the same random values on repeated implementations of the program on the same or other computers (for a particular seed value). The preceding value number created by the generator serves as the seed value. If there is no initial value, it employs the current system timestamp for the first time.

Using random.seed() function

We'll explore how to produce an identical random number with a particular seed value every time.

Code

Output

586
586
586
586
586
586 

Code

Output

Random numbers after specifying a unique seed value: 
244
244
Random number generated without specifying that particular or any seed value: 
607
558

Using Python Seed with the randrange Function

Let's look at how to apply the seed() method to generate the same random integer inside a specified range.

Code

Output

360
360

Using the Seed Function with the Choice Method

We use random.choice() method to select a random item from the given list or set. We can choose the same option every time by specifying a unique seed value.

Code

Output

The first random integer from the list:  9
The second random integer from the list after using the same seed value:  9

Using the Random Seed Function with a Sample Function

We may choose random items from the list or sequence data types using the random sample() method. Let's look at how to use the seed() and sample() functions to retrieve the same random sample from the list every time.

Code

Output

The first sample of integers after specifying a seed value  [4, 9, 3, 8]
The second sample of integers after stating the same seed value  [4, 9, 3, 8]

Using Random Seed Function with Shuffle Function

We can also combine the random module's seed() and shuffle() methods. The fundamental goal of combining the seed() with shuffle() functions is to create the same output after each shuffle. We will obtain the same element sequence if we use the exact seed value each time we execute the shuffle() method. That is, shuffling always delivers the same outcome.

Code

Output

The original list is:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Shuffled list the first time [2, 6, 7, 1, 0, 5, 8, 3, 9, 4]
Shuffled list the second time [2, 6, 7, 1, 0, 5, 8, 3, 9, 4]

When the preceding code is executed, the first print statement displays the original list before shuffling. We got a copy of the list because of the randome.shuffle() method shuffles the given list inplace. Then we specified a seed value and shuffled the first list. Then we used the same seed value to shuffle the copy of the original list. We got the same sequence of elements in both shuffling attempts.

Uses of the Random Seed Method in Python

  • This is used to create a pseudo-random encryption code. Encryption keys are a critical component of computer security. These types of secret keys are used to safeguard data against unwanted internet access.
  • Using random numbers helps in easily optimizing the code. Through the seed method, the task becomes much simpler. The code's output is sometimes dependent on the input. As a result, using random numbers to evaluate algorithms can be complicated. Also, the seed method is used to create the same random integers repeatedly, simplifying the algorithm testing procedure.





Latest Courses