random.seed(): What does it do?

380,399

Solution 1

Pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value.

Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice.

Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.

Solution 2

All the other answers don't seem to explain the use of random.seed(). Here is a simple example (source):

import random
random.seed( 3 )
print "Random number with seed 3 : ", random.random() #will generate a random number 
#if you want to use the same random number once again in your program
random.seed( 3 )
random.random()   # same random number as before

Solution 3

>>> random.seed(9001)   
>>> random.randint(1, 10)  
1     
>>> random.seed(9001)     
>>> random.randint(1, 10)    
1           
>>> random.seed(9001)          
>>> random.randint(1, 10)                 
1                  
>>> random.seed(9001)         
>>> random.randint(1, 10)          
1     
>>> random.seed(9002)                
>>> random.randint(1, 10)             
3

You try this.

Let's say 'random.seed' gives a value to random value generator ('random.randint()') which generates these values on the basis of this seed. One of the must properties of random numbers is that they should be reproducible. When you put same seed, you get the same pattern of random numbers. This way you are generating them right from the start. You give a different seed- it starts with a different initial (above 3).

Given a seed, it will generate random numbers between 1 and 10 one after another. So you assume one set of numbers for one seed value.

Solution 4

A random number is generated by some operation on previous value.

If there is no previous value then the current time is taken as previous value automatically. We can provide this previous value by own using random.seed(x) where x could be any number or string etc.

Hence random.random() is not actually perfect random number, it could be predicted via random.seed(x).

import random 
random.seed(45)            #seed=45  
random.random()            #1st rand value=0.2718754143840908
0.2718754143840908  
random.random()            #2nd rand value=0.48802820785090784
0.48802820785090784  
random.seed(45)            # again reasign seed=45  
random.random()
0.2718754143840908         #matching with 1st rand value  
random.random()
0.48802820785090784        #matching with 2nd rand value

Hence, generating a random number is not actually random, because it runs on algorithms. Algorithms always give the same output based on the same input. This means, it depends on the value of the seed. So, in order to make it more random, time is automatically assigned to seed().

Solution 5

Seed() can be used for later use ---

Example:
>>> import numpy as np
>>> np.random.seed(12)
>>> np.random.rand(4)
array([0.15416284, 0.7400497 , 0.26331502, 0.53373939])
>>>
>>>
>>> np.random.seed(10)
>>> np.random.rand(4)
array([0.77132064, 0.02075195, 0.63364823, 0.74880388])
>>>
>>>
>>> np.random.seed(12) # When you use same seed as before you will get same random output as before
>>> np.random.rand(4)
array([0.15416284, 0.7400497 , 0.26331502, 0.53373939])
>>>
>>>
>>> np.random.seed(10)
>>> np.random.rand(4)
array([0.77132064, 0.02075195, 0.63364823, 0.74880388])
>>>
Share:
380,399
Ahaan S. Rungta
Author by

Ahaan S. Rungta

Student of the MIT Class of 2019

Updated on July 19, 2022

Comments

  • Ahaan S. Rungta
    Ahaan S. Rungta almost 2 years

    I am a bit confused on what random.seed() does in Python. For example, why does the below trials do what they do (consistently)?

    >>> import random
    >>> random.seed(9001)
    >>> random.randint(1, 10)
    1
    >>> random.randint(1, 10)
    3
    >>> random.randint(1, 10)
    6
    >>> random.randint(1, 10)
    6
    >>> random.randint(1, 10)
    7
    

    I couldn't find good documentation on this.

    • Asad Saeeduddin
      Asad Saeeduddin about 10 years
      Random number generation isn't truly "random". It is deterministic, and the sequence it generates is dictated by the seed value you pass into random.seed. Typically you just invoke random.seed(), and it uses the current time as the seed value, which means whenever you run the script you will get a different sequence of values.
    • Tymoteusz Paul
      Tymoteusz Paul about 10 years
      Passing the same seed to random, and then calling it will give you the same set of numbers. This is working as intended, and if you want the results to be different every time you will have to seed it with something different every time you start an app (for example output from /dev/random or time)
    • Blink
      Blink about 10 years
      The seed is what is fed to the RNG to generate the first random number. After that, they RNG is self-fed. You don't see the same answer consistently because of this. If you run this script again, you will get the same sequence of "random" numbers. Setting the seed is helpful if you want to reproduce results, as all the "random" numbers generated will always be the same.
    • ggorlen
      ggorlen almost 5 years
      Worth mentioning: the sequence shown in this post is in Python 2. Python 3 gives a different sequence.
    • Joachim Wagner
      Joachim Wagner over 4 years
      @Blink 's use of "random number" is misleading. The RNG has an internal state that is self-fed. From this internal state, output for randint(1,10) and other calls are derived. If the RNG was feeding from the output of randint(1,10) the sequence would collapse to 1 of at most 10 sequences and the sequence would repeat after at most 10 numbers.
  • Oliver Ni
    Oliver Ni over 7 years
    Shorter equality check: len(set(l))<=1
  • ViFI
    ViFI about 7 years
    May be it is worth mentioning that sometimes we want to give seed so that same random sequence is generated on every run of the program. Sometimes, randomness in software program(s) is avoided to keep program behavior deterministic and possibility of reproducing the issues / bugs.
  • shaneb
    shaneb almost 6 years
    Following what @ViFI said, keeping program behavior deterministic (with a fixed seed, or fixed sequence of seeds) can also allow you to better assess whether some change to your program is beneficial or not.
  • Shashank Vivek
    Shashank Vivek over 5 years
    would you mind explaining with some real life scenario. I cant understand a use case for the same. Do we have something similar to this in other programming language as well ?
  • Galen Long
    Galen Long about 5 years
    Here's a real life scenario: stackoverflow.com/questions/5836335/…. Random seeds are also common to create reproducible results for research. For example, if you're a data scientist and you want to publish your results with some kind of model that uses randomness (e.g. a random forest), you'll want to include a seed in your published code so people can make sure your calculations are reproducible.
  • Jitin
    Jitin over 3 years
    so seed literally works to de-randomize the random function in a way we want ... ?
  • Eric Finn
    Eric Finn over 3 years
    @Jitin Not quite. PRNGs actually produce a (very long) repeating sequence of numbers. That is, in theory, if you kept requesting random numbers for long enough, you'd eventually start getting the same numbers, in the same order, as when you started. The seed you provide just specifies where in this sequence you're starting.