Python Random seed() Method

Usage

The random.seed() method is used to initialize the internal state of Python’s pseudo-random number generator (PRNG).

A PRNG is actually an algorithm that requires an initial number, known as a seed, to be able to generate a sequence of numbers that appear random. If you provide the same seed value to the PRNG, it will generate the very same sequence of “random” numbers each time.

By default, Python’s PRNG uses the current system time as its seed. You can, however, use the random.seed() method to specify a custom seed value. By providing a seed value, you can ensure that the sequence of random numbers produced will be deterministic and reproducible.

Such predictability can be very useful in various scenarios, including debugging, testing, scientific simulations, or any situation where consistent output is necessary.

Syntax

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

Parameters

ParameterConditionDescription
aOptionalThe seed value needed to generate a random number.
versionOptionalAn integer specifying the version of the algorithm used to convert the ‘a’ parameter to an integer.

If a is an integer, it is used directly; otherwise, it is automatically converted into an integer. And If a is omitted or set to None, the generator uses the current system time.

The version parameter specifies the algorithm version used to convert the a parameter into an integer. There are two versions available:

  • With version 2 (default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used.
  • With version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.

Return Value

The method doesn’t return any value. It initializes (or re-initializes) the random number generator.

Module Import

To use random.seed(), you’ll first need to import the random module.

import random

Example

The random.seed() method is primarily used to set the starting point for the random number generator. By providing the same seed, you can reproduce the same sequence of random numbers across multiple runs.

Here’s a simple demonstration:

import random

# Without seeding
print(random.randint(1, 10))    # Possible output: 7
print(random.randint(1, 10))    # Possible output: 5

# Seeding the random number generator
random.seed(1234)

# After seeding
print(random.randint(1, 10))    # Output: 8
print(random.randint(1, 10))    # Output: 2

# Resetting the seed
random.seed(1234)

# Reproducing the same sequence as above
print(random.randint(1, 10))    # Output: 8
print(random.randint(1, 10))    # Output: 2

In the example above, the sequence of random numbers produced after seeding will be consistent every time you run the program with the same seed.

Points to Note

  • Seeding the generator ensures reproducibility, but it can also make your program more predictable if the seed is known. This could be a security concern in some scenarios, particularly in cryptographic contexts. For cryptographic purposes, consider using the secrets module.
  • The seed does not have to be an integer; strings, bytes, and even floats can be used as seed values.
  • While seeding makes sequences reproducible across runs, the sequences themselves are still random. That is, they do not follow a consistent pattern.
  • If you’re working with multiple threads, keep in mind that the random.seed() method will affect all threads. Therefore, when reproducibility across threads is necessary, synchronization must be taken into account.