2

I have read several questions including those aimed specifically at numpy. I am however simply interested in permanently setting seed in(with) random and hence generate the same random float with random.random() with each call. Here's what I did:

import random
random.seed(2) 

First call:

random.random()
0.9560342718892494

Second call:

random.random()
0.9478274870593494

I could keep calling random.seed but would appreciate if someone could explain what is going on or what exactly I can do to set seed once and generate the same random float with each random.random() call.

I am currently trying to learn python so my apologies if this is very basic and already has an answer. I have read several questions before posting.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 1
    You can set your seed as a environment variable and import it, or set it as a class attribute – Devesh Kumar Singh Jun 27 '19 at 07:50
  • 2
    Do you want to get the _same_ number for every call to `random()`? That's not very random. If you just want one random number then assign the random number to some variable and reuse that variable. – buzjwa Jun 27 '19 at 07:51
  • 1
    I need to return `0.9560342718892494` at each call. My perhaps naive understanding of `seed` is that it should aid "reproducibility." – NelsonGon Jun 27 '19 at 07:53
  • 4
    `seed()` initializes the random number generator, so every subsequent calls to `random()` produce same sequence of values based on this seed. Not that `random()` will return exact same value each call – Andrej Kesely Jun 27 '19 at 07:53
  • Ah, I think I get it. So `0.9560342718892494` is the first value in this sequence. Don't know if I should delete the question. – NelsonGon Jun 27 '19 at 07:54
  • 2
    @NelsonGon You can answer it for yourself and then close it. – Andrej Kesely Jun 27 '19 at 07:55
  • 1
    I would say answer and leave it. Someone else might have the same initial misunderstanding and find this useful. – buzjwa Jun 27 '19 at 07:55
  • 1
    Random depends on seed and time. So ask for random with same seed and diferent time will return 2 diferents results. – Wonka Jun 27 '19 at 07:57
  • 1
    Possible duplicate of [random.seed(): What does it do?](https://stackoverflow.com/questions/22639587/random-seed-what-does-it-do) – Georgy Jun 27 '19 at 08:29
  • @Wonka how does it depend on `time`? (it obviously depends on the number of iterations, but that is not *time*. `python3 -c "import random; random.seed(2); print(random.random())"` still returns `0.9560342718892494 `, even though 4 years have passed... – umläute Apr 18 '23 at 20:20

1 Answers1

4

As pointed out in the comments by @Andrej Kesely and @buzjwa , setting seed does not guarantee that you will get the same value at each call. What is really going on is that we set a random state that then determines the sequence of random floats generated.

When we call random.random() the first time, what we are really doing is initiate the process of random number generation and return the first value in this sequence of numbers.

That is once we set random.seed(2), each time we follow this with a random.random() call, we return 0.9560342718892494 which is the first value in this sequence of numbers. Subsequent random.random() calls, return the next values in the sequence as per the corresponding call number.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57