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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
import _random
import sys
from _typeshed import SupportsLenAndGetItem
from collections.abc import Callable, Iterable, MutableSequence, Sequence, Set as AbstractSet
from fractions import Fraction
from typing import Any, ClassVar, NoReturn, TypeVar, overload
from typing_extensions import Self, deprecated
__all__ = [
"Random",
"seed",
"random",
"uniform",
"randint",
"choice",
"sample",
"randrange",
"shuffle",
"normalvariate",
"lognormvariate",
"expovariate",
"vonmisesvariate",
"gammavariate",
"triangular",
"gauss",
"betavariate",
"paretovariate",
"weibullvariate",
"getstate",
"setstate",
"getrandbits",
"choices",
"SystemRandom",
"randbytes",
]
if sys.version_info >= (3, 12):
__all__ += ["binomialvariate"]
_T = TypeVar("_T")
class Random(_random.Random):
VERSION: ClassVar[int]
def __init__(self, x: int | float | str | bytes | bytearray | None = None) -> None: ... # noqa: Y041
# Using other `seed` types is deprecated since 3.9 and removed in 3.11
# Ignore Y041, since random.seed doesn't treat int like a float subtype. Having an explicit
# int better documents conventional usage of random.seed.
if sys.version_info < (3, 10):
# this is a workaround for pyright correctly flagging an inconsistent inherited constructor, see #14624
def __new__(cls, x: int | float | str | bytes | bytearray | None = None) -> Self: ... # noqa: Y041
def seed(self, a: int | float | str | bytes | bytearray | None = None, version: int = 2) -> None: ... # type: ignore[override] # noqa: Y041
def getstate(self) -> tuple[Any, ...]: ...
def setstate(self, state: tuple[Any, ...]) -> None: ...
def randrange(self, start: int, stop: int | None = None, step: int = 1) -> int: ...
def randint(self, a: int, b: int) -> int: ...
def randbytes(self, n: int) -> bytes: ...
def choice(self, seq: SupportsLenAndGetItem[_T]) -> _T: ...
def choices(
self,
population: SupportsLenAndGetItem[_T],
weights: Sequence[float | Fraction] | None = None,
*,
cum_weights: Sequence[float | Fraction] | None = None,
k: int = 1,
) -> list[_T]: ...
if sys.version_info >= (3, 11):
def shuffle(self, x: MutableSequence[Any]) -> None: ...
else:
@overload
def shuffle(self, x: MutableSequence[Any]) -> None: ...
@overload
@deprecated("The `random` parameter is deprecated since Python 3.9; removed in Python 3.11.")
def shuffle(self, x: MutableSequence[Any], random: Callable[[], float] | None = None) -> None: ...
if sys.version_info >= (3, 11):
def sample(self, population: Sequence[_T], k: int, *, counts: Iterable[int] | None = None) -> list[_T]: ...
else:
def sample(
self, population: Sequence[_T] | AbstractSet[_T], k: int, *, counts: Iterable[int] | None = None
) -> list[_T]: ...
def uniform(self, a: float, b: float) -> float: ...
def triangular(self, low: float = 0.0, high: float = 1.0, mode: float | None = None) -> float: ...
if sys.version_info >= (3, 12):
def binomialvariate(self, n: int = 1, p: float = 0.5) -> int: ...
def betavariate(self, alpha: float, beta: float) -> float: ...
if sys.version_info >= (3, 12):
def expovariate(self, lambd: float = 1.0) -> float: ...
else:
def expovariate(self, lambd: float) -> float: ...
def gammavariate(self, alpha: float, beta: float) -> float: ...
if sys.version_info >= (3, 11):
def gauss(self, mu: float = 0.0, sigma: float = 1.0) -> float: ...
def normalvariate(self, mu: float = 0.0, sigma: float = 1.0) -> float: ...
else:
def gauss(self, mu: float, sigma: float) -> float: ...
def normalvariate(self, mu: float, sigma: float) -> float: ...
def lognormvariate(self, mu: float, sigma: float) -> float: ...
def vonmisesvariate(self, mu: float, kappa: float) -> float: ...
def paretovariate(self, alpha: float) -> float: ...
def weibullvariate(self, alpha: float, beta: float) -> float: ...
# SystemRandom is not implemented for all OS's; good on Windows & Linux
class SystemRandom(Random):
def getrandbits(self, k: int) -> int: ... # k can be passed by keyword
def getstate(self, *args: Any, **kwds: Any) -> NoReturn: ...
def setstate(self, *args: Any, **kwds: Any) -> NoReturn: ...
_inst: Random
seed = _inst.seed
random = _inst.random
uniform = _inst.uniform
triangular = _inst.triangular
randint = _inst.randint
choice = _inst.choice
randrange = _inst.randrange
sample = _inst.sample
shuffle = _inst.shuffle
choices = _inst.choices
normalvariate = _inst.normalvariate
lognormvariate = _inst.lognormvariate
expovariate = _inst.expovariate
vonmisesvariate = _inst.vonmisesvariate
gammavariate = _inst.gammavariate
gauss = _inst.gauss
if sys.version_info >= (3, 12):
binomialvariate = _inst.binomialvariate
betavariate = _inst.betavariate
paretovariate = _inst.paretovariate
weibullvariate = _inst.weibullvariate
getstate = _inst.getstate
setstate = _inst.setstate
getrandbits = _inst.getrandbits
randbytes = _inst.randbytes
|