How to use Python's random number generator with a local seed?

后端 未结 1 683
面向向阳花
面向向阳花 2020-12-17 14:57

Python\'s random seems are global, so modules changing it will effect each other.

While there are of course many 3rd party modules, is there a way using Python\'s st

相关标签:
1条回答
  • 2020-12-17 15:17

    From the docs:

    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 of Random to get generators that don’t share state.

    Make your own random.Random instance and use that.

    rng = random.Random(42)
    number = rng.randint(10, 20)
    
    0 讨论(0)
提交回复
热议问题