OSDN Git Service

File system path for statndalone version
[minimpy2/mp2.git] / my_random.py
1 import random
2 import secrets
3
4 SECRETS = 'secrets'
5 RANDOM  = 'random'
6
7
8 class Random:
9     __instance = None
10
11     @staticmethod
12     def get_instance(name):
13         if Random.__instance is None:
14             Random(name)
15         return Random.__instance
16
17     def __init__(self, name):
18         if Random.__instance is not None:
19             raise Exception("This class is a singleton!")
20         else:
21             if name == RANDOM:
22                 self.engine = random
23             else:
24                 self.engine = secrets
25             self.name = name
26             Random.__instance = self
27
28     def seed(self, seed):
29         if self.name == SECRETS:
30             return
31         self.engine.seed(seed)
32
33     def randint(self, a, b):
34         a, b = int(a), int(b)
35         if self.name == RANDOM:
36             return self.engine.randint(a, b)
37         return a + self.engine.randbelow(b - a + 1)
38
39     def choice(self, seq):
40         return self.engine.choice(seq)
41
42     def uniform(self, a, b):
43         a, b = int(a), int(b)
44         if self.name == RANDOM:
45             return self.engine.uniform(a, b)
46         return self.engine.SystemRandom().uniform(a, b)