Numpy Random Seed - Please explain

Hi. Can someone explain random.seed in numpy please and any possible applications of it?

https://numpy.org/doc/stable/reference/random/generated/numpy.random.seed.html?highlight=seed#numpy.random.seed

Also the link in the mission is broken: https://app.dataquest.io/m/377/estimating-probabilities/4/repeating-an-experiment

from numpy.random import seed, randint

seed(1)

def coin_toss():
    if randint(0, 2) == 1:
        return 'HEAD'
    else:
        return 'TAIL'

coin_toss()

I do not understand what seed is doing here.

Hey.

In that screen there is a link to a previous mission that explains the random_state argument present in many methods. NumPy also offers this functionality and one of the ways that it does that is numpy.seed.

I won’t explain it here because it is explained in the content. If there’s somehting in the content that you find confusing, do ask about it.

It serves to reproduce results. I suspect that the real reason behind us using it here (other that reproducing results) is that we use it for answer checking. If we allowed you to not use a specific seed, the results could vary making it very hard to validate the answer.

There are other good reasons, though. One of the tenets of the scientific method is reproducibility:

  1. Define a question
  2. Gather information and resources (observe)
  3. Form an explanatory hypothesis
  4. Test the hypothesis by performing an experiment and collecting data in a reproducible manner
  5. Analyze the data
  6. Interpret the data and draw conclusions that serve as a starting point for new hypothesis
  7. Publish results
  8. Retest (frequently done by other scientists)

The example in this screen is fairly simple. Suppose, however, that it is a complicated experiment where are at some point you randomly select some data points in the middle of the experiment.

Suppose further that they lead to some unexpected results much later in the experiment. Being able to identify them so that the experiment can be reproduced with them is important.

1 Like

Perfect. Makes much more sense. Appreciate it Bruno.

Good explanation if anyone needs it.

1 Like