Skip to content

Commit 64ff614

Browse files
committed
feat: add triangular func
1 parent f5f06f5 commit 64ff614

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

pycustomrand/random_generator.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ def random_float(start: int | float, end: int | float, digits: int = None) -> fl
134134
return true_round(result, digits)
135135
return result
136136

137+
@staticmethod
138+
def triangular(low: float = 0.0, high: float = 1.0, mode: float = None) -> float:
139+
"""
140+
Возвращает случайное число с треугольным распределением в диапазоне [low, high].
141+
Чаще всего выпадает значение около mode (вершина треугольника).
142+
"""
143+
uniform = PseudoRandom.random()
144+
cutoff = 0.5 if mode is None else (mode - low) / (high - low)
145+
if uniform > cutoff:
146+
uniform = 1.0 - uniform
147+
cutoff = 1.0 - cutoff
148+
low, high = high, low
149+
return low + (high - low) * (uniform * cutoff) ** 0.5
150+
137151
# -------------------- Байтовые функции --------------------
138152

139153
@staticmethod

0 commit comments

Comments
 (0)