Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's random: What happens if I don't use seed(someValue)?

a)In this case does the random number generator uses the system's clock (making the seed change) on each run?

b)Is the seed used to generate the pseudo-random values of expovariate(lambda)?

like image 763
andandandand Avatar asked May 03 '09 18:05

andandandand


People also ask

Does Python random need to be seeded?

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.

Why do we need random seed?

We use random seed value while creating training and test data set. The goal is to make sure we get the same training and validation data set while we use different hyperparameters or machine learning algorithms in order to assess the performance of different models.

Why do we use random seed 42?

What is the significance of random. seed(42) ? It's a pop-culture reference! In Douglas Adams's popular 1979 science-fiction novel The Hitchhiker's Guide to the Galaxy, towards the end of the book, the supercomputer Deep Thought reveals that the answer to the great question of “life, the universe and everything” is 42.

Why do we use NP random seed?

The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.


2 Answers

"Use the Source, Luke!"...;-). Studying https://svn.python.org/projects/python/trunk/Lib/random.py will rapidly reassure you;-).

What happens when seed isn't set (that's the "i is None" case):

if a is None:
    try:
        a = long(_hexlify(_urandom(16)), 16)
    except NotImplementedError:
        import time
        a = long(time.time() * 256) # use fractional seconds

and the expovariate:

random = self.random
u = random()
while u <= 1e-7:
    u = random()
return -_log(u)/lambd

obviously uses the same underlying random generator as every other method, and so is identically affected by the seeding or lack thereof (really, how else would it have been done?-)

like image 67
Alex Martelli Avatar answered Sep 21 '22 17:09

Alex Martelli


a) It typically uses the system clock, the clock on some systems may only have ms precision and so seed twice very quickly may result in the same value.

seed(self, a=None) Initialize internal state from hashable object.

None or no argument seeds from current time or from an operating
system specific randomness source if available.

http://pydoc.org/2.5.1/random.html#Random-seed

b) I would imagine expovariate does, but I can't find any proof. It would be silly if it didn't.

like image 31
Ethan Heilman Avatar answered Sep 18 '22 17:09

Ethan Heilman