Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of a random seed in Python?

If I use the Python function random.seed(my_seed) in one class in my module, will this seed remain for all the other classes instantiated in this module?

like image 809
Lyrositor Avatar asked Sep 11 '12 11:09

Lyrositor


People also ask

What does random seed in python do?

random. seed(a, version) in python is used to initialize the pseudo-random number generator (PRNG). PRNG is algorithm that generates sequence of numbers approximating the properties of random numbers. These random numbers can be reproduced using the seed value.

What is the use of random seed in Numpy?

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.

What is the purpose of this random seed number?

Definition and Usage The seed() method is used to initialize the random number generator. 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.

What is meant by random seed?

A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator. For a seed to be used in a pseudorandom number generator, it does not need to be random.


1 Answers

Yes, the seed is set for the (hidden) global Random() instance in the module. From the documentation:

The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances ofRandom to get generators that don’t share state.

Use separate Random() instances if you need to keep the seeds separate; you can pass in a new seed when you instantiate it:

>>> from random import Random >>> myRandom = Random(anewseed) >>> randomvalue = myRandom.randint(0, 10) 

The class supports the same interface as the module.

like image 72
Martijn Pieters Avatar answered Oct 20 '22 15:10

Martijn Pieters