0
def main():
    x = [randint(1,100) for i in range(1,100)]
    return x

This returns 100 random numbers btw 1 and 100. Each time I call the function, it returns a different sequence of numbers. What I want to, is to get the same sequence of numbers each time. maybe saving the results into sth?

theo bogner
  • 27
  • 1
  • 1
  • 1
  • you could use `pickle` or just generate once and hardcode it in your program. – Jean-François Fabre Mar 09 '17 at 07:37
  • you have to be more specific, there are a lot of ways to do that. – Jean-François Fabre Mar 09 '17 at 07:39
  • 1
    I don't think `pickle` is directly related and generate once is not what is being asked. – pvg Mar 09 '17 at 07:40
  • FWIW, you normally only call `main()` once each time you run a program. Do you want to get the same sequence of numbers each time the program is run? – PM 2Ring Mar 09 '17 at 07:51
  • 1
    You appear to be using the random module. [The documentation](https://docs.python.org/2/library/random.html) tells you how to initialise it using the [**`seed`**](https://docs.python.org/2/library/random.html#random.seed) function. – Peter Wood Mar 09 '17 at 07:57
  • Does this answer your question? [How to generate a repeatable random number sequence?](https://stackoverflow.com/questions/9023660/how-to-generate-a-repeatable-random-number-sequence) – Brent Aug 04 '20 at 19:29

3 Answers3

9

You can provide some fixed seed.

import random

def main():
    random.seed(9001)
    x = [random.randint(1,100) for i in range(1,100)]
    return x

For more information on seed: random.seed(): What does it do?

Community
  • 1
  • 1
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
  • That is incorrect. It doesn't provide the same results since it is a list of such random numbers. – shad0w_wa1k3r Mar 09 '17 at 07:37
  • @AshishNitinPatil can you be more specific? – Jean-François Fabre Mar 09 '17 at 07:38
  • You can do more than that. At any particular point you can retrieve the rng state with`random.getstate()` and then reset it with `random.setstate()` – pvg Mar 09 '17 at 07:39
  • You should put the `random.seed(9001)` in the function itself. Putting it outside of it doesn't solve OP's problem. – shad0w_wa1k3r Mar 09 '17 at 07:40
  • 1
    @AshishNitinPatil Fair point. OTOH, `main()` is normally only called once per program invocation, as the entrypoint to the program. But of course the OP may not be adhering to that convention. – PM 2Ring Mar 09 '17 at 07:44
  • I think this gives the impression that the state of an rng boils down to a single number. You _can_ use this to reinit the rng but the module provides a reliable way to recover and reset the entire current state of the rng and get reproducible results without ever having to think about seeds. You can, for instance, precisely reproduce two runs of rng output in your program that will be mutually identical but different on every invocation, without hardcoding any kind of magic constant. – pvg Mar 09 '17 at 08:15
6

Here's a basic example patterned after your code

import random
s = random.getstate()

print([random.randint(1,100) for i in range(10)])

random.setstate(s)
print([random.randint(1,100) for i in range(10)])

In both invocations, you get identical output. The key is, at any point you can retrieve and later reassign the current state of the rng.

pvg
  • 2,673
  • 4
  • 17
  • 31
  • Okay, but..how do you incorporate getstate() and setstate() into a program? Please write in easy English – theo bogner Mar 09 '17 at 18:29
  • Sure, you just have to explain in a little more detail what you mean by 'incorporate in a program' and what the behaviour your are looking for is. – pvg Mar 09 '17 at 18:34
  • It is safe to set state to original value after altering it. You can get into trouble if you use `randint` from other places in your application. Another way could be to use `random.Random` class instance and set seed on it – Murali KG Jan 25 '19 at 11:56
3
In [19]: for i in range(10):
    ...:     random.seed(10)
    ...:     print [random.randint(1, 100) for j in range(5)]
    ...:
    ...:
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]
[58, 43, 58, 21, 82]

The random.seed function has to be called just before a fresh call to random.

In [20]: random.seed(10)

In [21]: for i in range(10):
    ...:     print [random.randint(1,10) for j in range(10)]
    ...:
[5, 6, 3, 9, 9, 7, 2, 6, 4, 3]
[10, 10, 1, 9, 7, 4, 3, 7, 5, 7]
[7, 2, 8, 10, 10, 7, 1, 1, 2, 10]
[4, 4, 9, 4, 6, 5, 1, 6, 9, 2]
[3, 5, 1, 5, 9, 7, 6, 9, 2, 6]
[4, 7, 2, 8, 1, 2, 9, 10, 5, 5]
[3, 3, 7, 2, 2, 5, 2, 7, 9, 8]
[5, 4, 5, 1, 8, 4, 4, 1, 5, 6]
[4, 9, 7, 3, 6, 10, 6, 7, 1, 5]
[5, 5, 5, 6, 6, 5, 2, 5, 10, 5]
ssm
  • 5,277
  • 1
  • 24
  • 42