Python - random.seed( ) method Last Updated : 15 Mar, 2025 Comments Improve Suggest changes Like Article Like Report 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:Machine Learning- to ensure model consistencySimulations- to repeat experimentsGame Development- for consistent gameplay mechanics during testingTesting- to verify output correctnessLet'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)) Output21 537 865 865 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.Syntaxrandom.seed(a=None, version=2)Parametersa (Optional): it's the seed value (integer, float, str, bytes, or bytearray). If None, the system time is used.version(Optional): defaults value is 2, using a more advanced seeding algorithm. version=1 uses an older method.Return Typerandom.seed() method does not return any value.Let's look at some of the examples.Reproducing Same Random ListsWe 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:random.seed(9) sets the seed value to 9. It ensures that random values generated are same for every run.random.sample(range(1,50), 6) creates a sequence of numbers from 1 to 49 and randomly selects 6 unique numbers from it.Reproducible Data SplittingIn 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:list(range(10) gives us a list of integers from 0 to 9.random.seed(10) sets the seed to 10 ensuring consistant results in each run.random.shuffle(a) shuffles the list 'a' randomly but because of the seed() method we get the same shuffled list every time. Comment More infoAdvertise with us Next Article random.getstate() in Python deepak_jain Follow Improve Article Tags : Python Python-Functions Python-random Practice Tags : pythonpython-functions Similar Reads Python Random Module Python Random module generates random numbers in Python. These are pseudo-random numbers means they are not truly random. This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc. It is an in-built function in Python.Appli 6 min read Python - random.seed( ) method 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:Machine Learning- to ensure model cons 4 min read random.getstate() in Python random() module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getstate() The getstate() method of the random module returns an object with the curr 1 min read random.setstate() in Python Random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.setstate() The setstate() method of the random module is used in conjugation with the g 2 min read random.getrandbits() in Python random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.getrandbits() The getrandbits() method of the random module is used to return an intege 1 min read randrange() in Python The randrange() function in Python's random module is used to generate a random number within a specified range. It allows defining a start, stop, and an optional step value to control the selection of numbers. Unlike randint(), which includes the upper limit, randrange() excludes the stop value. Ex 4 min read randint() Function in Python randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python.Python randint() Method SyntaxSyntax: randint(sta 6 min read Python Numbers | choice() function choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string. Syntax: random.choice(sequence) Parameters: sequence is a mandatory parameter that can be a list, tuple, or string. Returns: The choice() returns a random item. Note:We have to im 1 min read Python - random.choices() method The choices() method returns multiple random elements from the list with replacement. Unlike random.choice(), which selects a single item, random.choices() allows us to select multiple items making it particularly useful for tasks like sampling from a population or generating random data.Example:Pyt 3 min read random.sample() function - Python sample() is an built-in function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Example:Pythonfrom random import sample a = [1, 2, 3, 4, 5] print(sample(a,3))Output[2, 5 2 min read Like