Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seed() and Random Numbers in Python [duplicate]

Tags:

python

random

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

Today, I was just told about the seed() function from a programmer much more experienced than me. I normally just call choice() with a list as an argument, as I don't need anymore random number functionality than that.

My programmer friend told me that calling seed is necessary because otherwise Python always begins random number operations with zero as the default seed. This means that although the numbers appear random, we are really getting the same sequence every time.

This strikes me as rather odd. Does the choice() function, for example, really not call seed before it does its thing? Or is the reason it can't programmatically change its seed because that in of itself would involve picking a random number, and obviously that it is a bit of a problem if our end goal is also to pick a random number!

I'm ranting a bit here, but I'm wondering if anyone has a clear idea of how all this was implemented.

like image 825
Josh Imhoff Avatar asked Dec 07 '11 22:12

Josh Imhoff


People also ask

What does seed () do in Python?

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.

How does Python generate random numbers with repetition?

You can use random. randint() and random. randrange() to generate the random numbers, but it can repeat the numbers. To create a list of unique random numbers, we need to use the sample() method.

Why is 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.


1 Answers

Your friend is dead wrong, and would know so if he read the documentation for the seed() function:

Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

(Emphasis mine.)

He's guessing based on his knowledge of how it works in other languages. The seed() function is mainly provided so that you can get a reproducible stream of pseudorandom numbers (which is necessary for some specific applications).

The functions you call directly from the random module are actually aliases to methods of a hidden instance of the random.Random class. Each instance, at least in effect, calls seed() within its __init__().

The choice() function obviously does not call seed() before operation, because that would mean re-seeding before every choice, which defeats the purpose of seeding.

like image 51
Karl Knechtel Avatar answered Oct 22 '22 02:10

Karl Knechtel