Skip to content

Commit 4eb50ac

Browse files
committed
feat: add three utils functions (bool, uuid4, color_hex)
1 parent 2346a61 commit 4eb50ac

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

pycustomrand/random_generator.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,38 @@ def binomialvariate(n: int = 1, p: float = 0.5) -> int:
259259
raise ValueError("Вероятность p должна быть в диапазоне [0, 1]")
260260

261261
return sum([PseudoRandom.random() < p for _ in range(n)])
262+
263+
# -------------------- Вспомогательные функции --------------------
264+
265+
@staticmethod
266+
def random_bool(true_chance: float = 0.5) -> bool:
267+
"""Возвращает True/False с вероятностью true_chance."""
268+
return PseudoRandom.random() < true_chance
269+
270+
@staticmethod
271+
def random_uuid4() -> str:
272+
"""
273+
Возвращает случайный UUID версии 4
274+
275+
Пример: 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
276+
"""
277+
# Генерирация 32 hex-цифры
278+
chars = [hex(PseudoRandom.random_integer(0, 15))[2:]
279+
for _ in range(32)]
280+
281+
# Согласно стандарту UUID v4:
282+
chars[12] = '4' # 13-й символ всегда '4'
283+
# 17-й символ должен быть одним из '8', '9', 'a', 'b'
284+
chars[16] = hex(PseudoRandom.choice([8, 9, 10, 11]))[2:]
285+
286+
return f"{''.join(chars[:8])}-{''.join(chars[8:12])}-{''.join(chars[12:16])}-{''.join(chars[16:20])}-{''.join(chars[20:])}"
287+
288+
@staticmethod
289+
def random_color_hex() -> str:
290+
"""
291+
Возвращает случайный цвет в формате hex.
292+
293+
Пример: '#ff0000'
294+
"""
295+
val = PseudoRandom.random_integer(0, 0xFFFFFF)
296+
return f"#{hex(val)[2:].zfill(6)}"

0 commit comments

Comments
 (0)