vendredi 14 décembre 2018

What does random.Random (not random.random) from the random module actually do in Python?

I would like to get a lucid explanation on what the random.Random function/class actually does. This is what Python's random module has to say about it.

Random number generator base class used by bound module functions. Used to instantiate instances of Random to get generators that don't share state. Especially useful for multi-threaded programs, creating a different instance of Random for each thread, and using the jumpahead() method to ensure that the generated sequences seen by each thread don't overlap.

Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the following methods: random(), seed(), getstate(), setstate() and jumpahead(). Optionally, implement a getrandbits() method so that randrange() can cover arbitrarily large ranges.

I do not understand this because I am still very much a beginner at Python. I do know a bit about base and derived classes and this clearly seems to have something to do with this.

I tried to play around with the random.Random() function/class in Python's IDLE and found out the following.

  • It only seems to accept 1 argument.(string, int, float)
  • Doesn't seem to to take in any lists or dictionaries as an argument; states they are unhashable. (What does 'unhashable' mean?)
  • It only seems to return two values alternatively on repeatedly invoking it, regardless of the argument passed to it, the two values being 'random.Random object at 0x03F24E40' and 'random.Random object at 0x03F26B60'.

I hope I can get a simple explanation of what random.Random does and also an explanation as to why it only returns two values. (I am a beginner so forgive my ignorance on the subject!)

Any explanation on how functions like seed(), getstate(), setstate() and jumpahead() work or references to any documents/books that explain so are welcome.




Aucun commentaire:

Enregistrer un commentaire