faif / python-patterns

A collection of design patterns/idioms in Python
40.58k stars 6.95k forks source link

random seed does not take effect in doctest #376

Closed qitianliang closed 2 years ago

qitianliang commented 3 years ago

Thanks for your awesome works on the pythonic design pattern, I'm reviewing some strategies in the design patterns. However, I tried your first abstract_factory.py random. seed(1234) in the main function will not take effect in doctest random. choice(), and it returns fake random depend on machine time or some other kind of random parameter. I tried to fix this problem by this

random.seed(1234) shop = PetShop(random_animal)

then you can get the same result every time you rerun it.

faif commented 3 years ago

Hi,

Please open a pull request so that we can get the fix in, and you get credit for your contribution :)

gyermolenko commented 2 years ago

maybe I misunderstand original message about

random. seed(1234) in the main function will not take effect in doctest random

but if I remove random.seed(1234) running python patterns/creational/abstract_factory.py produces ***Test Failed*** 1 failures

(and it produces nothing if random.seed(1234) is at place). So I believe random.seed is there works out for deterministic output.

What output do you have?

Mohamed-ElGemeie commented 2 years ago

I'll work on this.