Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to query seed used by random.random()?

Is there any way to find out what seed Python used to seed its random number generator?

I know I can specify my own seed, but I'm quite happy with Python managing it. But, I do want to know what seed it used, so that if I like the results I'm getting in a particular run, I could reproduce that run later. If I had the seed that was used then I could.

If the answer is I can't, then what's the best way to generate a seed myself? I want them to always be different from run to run---I just want to know what was used.

UPDATE: yes, I mean random.random()! mistake... [title updated]

like image 844
mix Avatar asked Feb 16 '11 04:02

mix


People also ask

How do I get random seeds in Python?

Python Random seed() Method The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time. Use the seed() method to customize the start number of the random number generator.

What does the seed do in random function?

Seed function is used to save the state of a random function, so that it can generate same random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value). The seed value is the previous value number generated by the generator.

How do you get a random seed in Java?

There are two ways we can generate random number using seed. Random random = new Random(long seed); Random random1 = new Random(); random1. setSeed(seed); The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).


1 Answers

It is not possible to get the automatic seed back out from the generator. I normally generate seeds like this:

seed = random.randrange(sys.maxsize) rng = random.Random(seed) print("Seed was:", seed) 

This way it is time-based, so each time you run the script (manually) it will be different, but if you are using multiple generators they won't have the same seed simply because they were created almost simultaneously.

like image 99
Zooba Avatar answered Sep 28 '22 03:09

Zooba