Skip to content

Commit 2346a61

Browse files
committed
refactor: simplification and rework shuffle func + small reformat the entire file
1 parent c550463 commit 2346a61

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

pycustomrand/random_generator.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class PseudoRandom:
88
# Переменная класса для хранения seed генератора псевдослучайных чисел
99
_seed = None
1010

11-
1211
# -------------------- Основные функции генерации случайных чисел --------------------
1312

1413
@classmethod
@@ -67,7 +66,6 @@ def random() -> float:
6766
raw_str = str(raw_int).zfill(16)
6867
return float("0." + raw_str)
6968

70-
7169
# -------------------- Числовые функции --------------------
7270

7371
@staticmethod
@@ -118,7 +116,6 @@ def random_integer(start: int, end: int = None, step: int = 1) -> int:
118116

119117
return start + (random_step_index * step)
120118

121-
122119
# -------------------- Функции для чисел с плавающей точкой --------------------
123120

124121
@staticmethod
@@ -160,7 +157,7 @@ def gauss(mu: float = 0.0, sigma: float = 1.0) -> float:
160157
"""
161158
u1 = PseudoRandom.random()
162159
u2 = PseudoRandom.random()
163-
z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * pi * u2)
160+
z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * pi * u2)
164161
return z0 * sigma + mu
165162

166163
@staticmethod
@@ -172,15 +169,13 @@ def expovariate(lambd: float = 1.0) -> float:
172169
"""
173170
return -log(1 - PseudoRandom.random()) / lambd
174171

175-
176172
# -------------------- Байтовые функции --------------------
177173

178174
@staticmethod
179175
def random_bytes(count: int) -> bytes:
180176
"""Возвращает случайные байты в количестве count."""
181177
return bytes([PseudoRandom.random_integer(0, 255) for _ in range(count)])
182178

183-
184179
# -------------------- Функции для последовательностей --------------------
185180

186181
@staticmethod
@@ -214,11 +209,9 @@ def choices(array: list[Any], k: int, weights: list[int] = None) -> list[Any]:
214209
@staticmethod
215210
def shuffle(array: list[Any]) -> None:
216211
"""Перемешивает массив на месте."""
217-
limit = len(array)-1
218-
for _ in range(len(array)*2):
219-
x1 = PseudoRandom.randrange(limit)
220-
x2 = PseudoRandom.randrange(limit)
221-
array[x1], array[x2] = array[x2], array[x1]
212+
for i in range(len(array) - 1, 0, -1):
213+
j = PseudoRandom.randrange(i + 1)
214+
array[i], array[j] = array[j], array[i]
222215

223216
@staticmethod
224217
def sample(array: list[Any], k: int, counts: list[int] = None) -> list[Any]:
@@ -249,7 +242,6 @@ def sample(array: list[Any], k: int, counts: list[int] = None) -> list[Any]:
249242

250243
return result
251244

252-
253245
# -------------------- Дискретные функции --------------------
254246

255247
@staticmethod

0 commit comments

Comments
 (0)