Skip to content

Commit f5f06f5

Browse files
committed
feat: add binomialvariate func (simpliest realisation) with its own section
1 parent b3a4911 commit f5f06f5

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

pycustomrand/random_generator.py

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

137-
138137
# -------------------- Байтовые функции --------------------
139138

140139
@staticmethod
@@ -210,3 +209,22 @@ def sample(array: list[Any], k: int, counts: list[int] = None) -> list[Any]:
210209
result.append(choice)
211210

212211
return result
212+
213+
214+
# -------------------- Дискретные функции --------------------
215+
216+
@staticmethod
217+
def binomialvariate(n: int = 1, p: float = 0.5) -> int:
218+
"""
219+
Возвращает случайное число, распределённое по биномиальному закону.
220+
Простейшая реализация, неоптимизированная.
221+
222+
n - количество испытаний (целое число >= 0).
223+
p - вероятность успеха в каждом испытании (0.0 <= p <= 1.0).
224+
"""
225+
if n < 0:
226+
raise ValueError("Количество испытаний n не может быть отрицательным")
227+
if not (0.0 <= p <= 1.0):
228+
raise ValueError("Вероятность p должна быть в диапазоне [0, 1]")
229+
230+
return sum([PseudoRandom.random() < p for _ in range(n)])

0 commit comments

Comments
 (0)