seeded_random.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
  2. # at the University of Antwerp (http://msdl.uantwerpen.be/).
  3. #
  4. # Licensed under the GNU Public License v3 (the "License");
  5. # you may not use this file except in compliance with the
  6. # License. You may obtain a copy of the License at
  7. # https://www.gnu.org/licenses/gpl-3.0.en.html
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an
  11. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. # either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the
  14. # License.
  15. import random
  16. class SeededRandom(random.Random):
  17. '''
  18. Random class wrapper, provided a seeded random number generator
  19. '''
  20. __instantiated = False
  21. def __init__(self, seed=0):
  22. '''
  23. Singleton class: the single instance "INFINITY" stands for infinity.
  24. '''
  25. if SeededRandom.__instantiated:
  26. raise NotImplementedError("singleton class already instantiated")
  27. SeededRandom.__instantiatiated = True
  28. random.Random.__init__(self)
  29. self.seed(seed)
  30. Random = SeededRandom()