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?

6
  • you could use pickle or just generate once and hardcode it in your program. Commented Mar 9, 2017 at 7:37
  • you have to be more specific, there are a lot of ways to do that. Commented Mar 9, 2017 at 7:39
  • 1
    I don't think pickle is directly related and generate once is not what is being asked. Commented Mar 9, 2017 at 7: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? Commented Mar 9, 2017 at 7:51
  • 1
    You appear to be using the random module. The documentation tells you how to initialise it using the seed function. Commented Mar 9, 2017 at 7:57

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

6 Comments

That is incorrect. It doesn't provide the same results since it is a list of such random numbers.
@AshishNitinPatil can you be more specific?
You can do more than that. At any particular point you can retrieve the rng state withrandom.getstate() and then reset it with random.setstate()
You should put the random.seed(9001) in the function itself. Putting it outside of it doesn't solve OP's problem.
@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.
|
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.

3 Comments

Okay, but..how do you incorporate getstate() and setstate() into a program? Please write in easy English
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.
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
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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.