A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/random-seed-in-python/ below:

Python - random.seed( ) method

Python - random.seed( ) method

Last Updated : 11 Jul, 2025

random.seed() method in Python is used to initialize the random number generator, ensuring the same random numbers on every run. By default, Python generates different numbers each time, but using .seed() allows result reproducibility.

It's most commonly used in:

Let's understand with an example.

Python
import random 

for i in range(2):
    # Generated random number will be between 1 to 1000 but different in every run.
    print(random.randint(1, 1000))  

for i in range(2):
    # Any number can be used in place of '0'.
    random.seed(0)
    # Generated random number will be between 1 to 1000 but same in every run.
    print(random.randint(1, 1000))  
    

Explanation: Notice that generating random numbers without using the .seed() method results in different outputs on each run, whereas using it ensures consistent results every time.

Syntax

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

Parameters Return Type

random.seed() method does not return any value.

Let's look at some of the examples.

Reproducing Same Random Lists

We can produce the same random list for multiple executions using random.seed() method.

Python
import random

random.seed(9)
print(random.sample(range(1, 50), 6))

Output
[30, 40, 24, 18, 9, 12]

Explanation:

Reproducible Data Splitting

In machine learning, we often split data into training and testing sets. Using a seed ensures that the split remains the same across multiple runs.

Python
import random

a = list(range(10))  # Sample dataset
random.seed(10)
random.shuffle(a)
print(a)  # The order will always be the same when using seed

Output
[5, 2, 7, 1, 8, 4, 3, 6, 0, 9]

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4