-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributedRandom.py
48 lines (43 loc) · 2.03 KB
/
distributedRandom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import random
import time
from rich.console import Console
console = Console()
class DistributedRandom:
def __init__(self, client: int = 1000,
min_amount: int = 0,
max_amount: int = 3000000,
distribution: int = 1000000) -> None:
# if max_amount / distribution < client or distribution/max_amount >= 0.5:
# raise ValueError("sample size is not large enough for the specified amount of client")
"""Initialize the distributed random number generator."""
with console.status("[bold green]Computing generated numbers...",
spinner_style="moon") as status:
self.possibilityList = []
self.count = 0
self.client = client
self.distribution = distribution
iteration = 0
max_iterations = 100 * client
max
while len(self.possibilityList) < client:
if iteration >= max_iterations:
raise RuntimeError("Reached max iterations without finding sufficient numbers.")
x = random.randint(min_amount, max_amount)
if len(self.possibilityList) == 0:
self.possibilityList.append(x)
else:
if self.checkNumber(x, self.possibilityList[len(self.possibilityList) - 1]):
self.possibilityList.append(x)
# print('list',sorted(self.possibilityList))
iteration += 1
# console.log(f'[bold][red]Done Generating Distributed Numbers!')
status.stop()
time.sleep(1)
def checkNumber(self, x: int, y: int) -> bool:
"""Check if the difference between two numbers meets the distribution requirement."""
return abs(x - y) >= self.distribution
def next(self):
"""Return the next number in the generated sequence."""
result = self.possibilityList[self.count]
self.count = self.count + 1 if self.client - self.count > 1 else 0
return result