| 1 | n/a | """Random variable generators. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | integers |
|---|
| 4 | n/a | -------- |
|---|
| 5 | n/a | uniform within range |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | sequences |
|---|
| 8 | n/a | --------- |
|---|
| 9 | n/a | pick random element |
|---|
| 10 | n/a | pick random sample |
|---|
| 11 | n/a | pick weighted random sample |
|---|
| 12 | n/a | generate random permutation |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | distributions on the real line: |
|---|
| 15 | n/a | ------------------------------ |
|---|
| 16 | n/a | uniform |
|---|
| 17 | n/a | triangular |
|---|
| 18 | n/a | normal (Gaussian) |
|---|
| 19 | n/a | lognormal |
|---|
| 20 | n/a | negative exponential |
|---|
| 21 | n/a | gamma |
|---|
| 22 | n/a | beta |
|---|
| 23 | n/a | pareto |
|---|
| 24 | n/a | Weibull |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | distributions on the circle (angles 0 to 2pi) |
|---|
| 27 | n/a | --------------------------------------------- |
|---|
| 28 | n/a | circular uniform |
|---|
| 29 | n/a | von Mises |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | General notes on the underlying Mersenne Twister core generator: |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | * The period is 2**19937-1. |
|---|
| 34 | n/a | * It is one of the most extensively tested generators in existence. |
|---|
| 35 | n/a | * The random() method is implemented in C, executes in a single Python step, |
|---|
| 36 | n/a | and is, therefore, threadsafe. |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | """ |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | from warnings import warn as _warn |
|---|
| 41 | n/a | from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType |
|---|
| 42 | n/a | from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil |
|---|
| 43 | n/a | from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin |
|---|
| 44 | n/a | from os import urandom as _urandom |
|---|
| 45 | n/a | from _collections_abc import Set as _Set, Sequence as _Sequence |
|---|
| 46 | n/a | from hashlib import sha512 as _sha512 |
|---|
| 47 | n/a | import itertools as _itertools |
|---|
| 48 | n/a | import bisect as _bisect |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | __all__ = ["Random","seed","random","uniform","randint","choice","sample", |
|---|
| 51 | n/a | "randrange","shuffle","normalvariate","lognormvariate", |
|---|
| 52 | n/a | "expovariate","vonmisesvariate","gammavariate","triangular", |
|---|
| 53 | n/a | "gauss","betavariate","paretovariate","weibullvariate", |
|---|
| 54 | n/a | "getstate","setstate", "getrandbits", "choices", |
|---|
| 55 | n/a | "SystemRandom"] |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0) |
|---|
| 58 | n/a | TWOPI = 2.0*_pi |
|---|
| 59 | n/a | LOG4 = _log(4.0) |
|---|
| 60 | n/a | SG_MAGICCONST = 1.0 + _log(4.5) |
|---|
| 61 | n/a | BPF = 53 # Number of bits in a float |
|---|
| 62 | n/a | RECIP_BPF = 2**-BPF |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | # Translated by Guido van Rossum from C source provided by |
|---|
| 66 | n/a | # Adrian Baddeley. Adapted by Raymond Hettinger for use with |
|---|
| 67 | n/a | # the Mersenne Twister and os.urandom() core generators. |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | import _random |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | class Random(_random.Random): |
|---|
| 72 | n/a | """Random number generator base class used by bound module functions. |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | Used to instantiate instances of Random to get generators that don't |
|---|
| 75 | n/a | share state. |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | Class Random can also be subclassed if you want to use a different basic |
|---|
| 78 | n/a | generator of your own devising: in that case, override the following |
|---|
| 79 | n/a | methods: random(), seed(), getstate(), and setstate(). |
|---|
| 80 | n/a | Optionally, implement a getrandbits() method so that randrange() |
|---|
| 81 | n/a | can cover arbitrarily large ranges. |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | """ |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | VERSION = 3 # used by getstate/setstate |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | def __init__(self, x=None): |
|---|
| 88 | n/a | """Initialize an instance. |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | Optional argument x controls seeding, as for Random.seed(). |
|---|
| 91 | n/a | """ |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | self.seed(x) |
|---|
| 94 | n/a | self.gauss_next = None |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | def seed(self, a=None, version=2): |
|---|
| 97 | n/a | """Initialize internal state from hashable object. |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | None or no argument seeds from current time or from an operating |
|---|
| 100 | n/a | system specific randomness source if available. |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | If *a* is an int, all bits are used. |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | For version 2 (the default), all of the bits are used if *a* is a str, |
|---|
| 105 | n/a | bytes, or bytearray. For version 1 (provided for reproducing random |
|---|
| 106 | n/a | sequences from older versions of Python), the algorithm for str and |
|---|
| 107 | n/a | bytes generates a narrower range of seeds. |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | """ |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | if version == 1 and isinstance(a, (str, bytes)): |
|---|
| 112 | n/a | x = ord(a[0]) << 7 if a else 0 |
|---|
| 113 | n/a | for c in a: |
|---|
| 114 | n/a | x = ((1000003 * x) ^ ord(c)) & 0xFFFFFFFFFFFFFFFF |
|---|
| 115 | n/a | x ^= len(a) |
|---|
| 116 | n/a | a = -2 if x == -1 else x |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | if version == 2 and isinstance(a, (str, bytes, bytearray)): |
|---|
| 119 | n/a | if isinstance(a, str): |
|---|
| 120 | n/a | a = a.encode() |
|---|
| 121 | n/a | a += _sha512(a).digest() |
|---|
| 122 | n/a | a = int.from_bytes(a, 'big') |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | super().seed(a) |
|---|
| 125 | n/a | self.gauss_next = None |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | def getstate(self): |
|---|
| 128 | n/a | """Return internal state; can be passed to setstate() later.""" |
|---|
| 129 | n/a | return self.VERSION, super().getstate(), self.gauss_next |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def setstate(self, state): |
|---|
| 132 | n/a | """Restore internal state from object returned by getstate().""" |
|---|
| 133 | n/a | version = state[0] |
|---|
| 134 | n/a | if version == 3: |
|---|
| 135 | n/a | version, internalstate, self.gauss_next = state |
|---|
| 136 | n/a | super().setstate(internalstate) |
|---|
| 137 | n/a | elif version == 2: |
|---|
| 138 | n/a | version, internalstate, self.gauss_next = state |
|---|
| 139 | n/a | # In version 2, the state was saved as signed ints, which causes |
|---|
| 140 | n/a | # inconsistencies between 32/64-bit systems. The state is |
|---|
| 141 | n/a | # really unsigned 32-bit ints, so we convert negative ints from |
|---|
| 142 | n/a | # version 2 to positive longs for version 3. |
|---|
| 143 | n/a | try: |
|---|
| 144 | n/a | internalstate = tuple(x % (2**32) for x in internalstate) |
|---|
| 145 | n/a | except ValueError as e: |
|---|
| 146 | n/a | raise TypeError from e |
|---|
| 147 | n/a | super().setstate(internalstate) |
|---|
| 148 | n/a | else: |
|---|
| 149 | n/a | raise ValueError("state with version %s passed to " |
|---|
| 150 | n/a | "Random.setstate() of version %s" % |
|---|
| 151 | n/a | (version, self.VERSION)) |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | ## ---- Methods below this point do not need to be overridden when |
|---|
| 154 | n/a | ## ---- subclassing for the purpose of using a different core generator. |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | ## -------------------- pickle support ------------------- |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | # Issue 17489: Since __reduce__ was defined to fix #759889 this is no |
|---|
| 159 | n/a | # longer called; we leave it here because it has been here since random was |
|---|
| 160 | n/a | # rewritten back in 2001 and why risk breaking something. |
|---|
| 161 | n/a | def __getstate__(self): # for pickle |
|---|
| 162 | n/a | return self.getstate() |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | def __setstate__(self, state): # for pickle |
|---|
| 165 | n/a | self.setstate(state) |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | def __reduce__(self): |
|---|
| 168 | n/a | return self.__class__, (), self.getstate() |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | ## -------------------- integer methods ------------------- |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def randrange(self, start, stop=None, step=1, _int=int): |
|---|
| 173 | n/a | """Choose a random item from range(start, stop[, step]). |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | This fixes the problem with randint() which includes the |
|---|
| 176 | n/a | endpoint; in Python this is usually not what you want. |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | """ |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | # This code is a bit messy to make it fast for the |
|---|
| 181 | n/a | # common case while still doing adequate error checking. |
|---|
| 182 | n/a | istart = _int(start) |
|---|
| 183 | n/a | if istart != start: |
|---|
| 184 | n/a | raise ValueError("non-integer arg 1 for randrange()") |
|---|
| 185 | n/a | if stop is None: |
|---|
| 186 | n/a | if istart > 0: |
|---|
| 187 | n/a | return self._randbelow(istart) |
|---|
| 188 | n/a | raise ValueError("empty range for randrange()") |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | # stop argument supplied. |
|---|
| 191 | n/a | istop = _int(stop) |
|---|
| 192 | n/a | if istop != stop: |
|---|
| 193 | n/a | raise ValueError("non-integer stop for randrange()") |
|---|
| 194 | n/a | width = istop - istart |
|---|
| 195 | n/a | if step == 1 and width > 0: |
|---|
| 196 | n/a | return istart + self._randbelow(width) |
|---|
| 197 | n/a | if step == 1: |
|---|
| 198 | n/a | raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | # Non-unit step argument supplied. |
|---|
| 201 | n/a | istep = _int(step) |
|---|
| 202 | n/a | if istep != step: |
|---|
| 203 | n/a | raise ValueError("non-integer step for randrange()") |
|---|
| 204 | n/a | if istep > 0: |
|---|
| 205 | n/a | n = (width + istep - 1) // istep |
|---|
| 206 | n/a | elif istep < 0: |
|---|
| 207 | n/a | n = (width + istep + 1) // istep |
|---|
| 208 | n/a | else: |
|---|
| 209 | n/a | raise ValueError("zero step for randrange()") |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | if n <= 0: |
|---|
| 212 | n/a | raise ValueError("empty range for randrange()") |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | return istart + istep*self._randbelow(n) |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | def randint(self, a, b): |
|---|
| 217 | n/a | """Return random integer in range [a, b], including both end points. |
|---|
| 218 | n/a | """ |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | return self.randrange(a, b+1) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type, |
|---|
| 223 | n/a | Method=_MethodType, BuiltinMethod=_BuiltinMethodType): |
|---|
| 224 | n/a | "Return a random int in the range [0,n). Raises ValueError if n==0." |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | random = self.random |
|---|
| 227 | n/a | getrandbits = self.getrandbits |
|---|
| 228 | n/a | # Only call self.getrandbits if the original random() builtin method |
|---|
| 229 | n/a | # has not been overridden or if a new getrandbits() was supplied. |
|---|
| 230 | n/a | if type(random) is BuiltinMethod or type(getrandbits) is Method: |
|---|
| 231 | n/a | k = n.bit_length() # don't use (n-1) here because n can be 1 |
|---|
| 232 | n/a | r = getrandbits(k) # 0 <= r < 2**k |
|---|
| 233 | n/a | while r >= n: |
|---|
| 234 | n/a | r = getrandbits(k) |
|---|
| 235 | n/a | return r |
|---|
| 236 | n/a | # There's an overridden random() method but no new getrandbits() method, |
|---|
| 237 | n/a | # so we can only use random() from here. |
|---|
| 238 | n/a | if n >= maxsize: |
|---|
| 239 | n/a | _warn("Underlying random() generator does not supply \n" |
|---|
| 240 | n/a | "enough bits to choose from a population range this large.\n" |
|---|
| 241 | n/a | "To remove the range limitation, add a getrandbits() method.") |
|---|
| 242 | n/a | return int(random() * n) |
|---|
| 243 | n/a | rem = maxsize % n |
|---|
| 244 | n/a | limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0 |
|---|
| 245 | n/a | r = random() |
|---|
| 246 | n/a | while r >= limit: |
|---|
| 247 | n/a | r = random() |
|---|
| 248 | n/a | return int(r*maxsize) % n |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | ## -------------------- sequence methods ------------------- |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | def choice(self, seq): |
|---|
| 253 | n/a | """Choose a random element from a non-empty sequence.""" |
|---|
| 254 | n/a | try: |
|---|
| 255 | n/a | i = self._randbelow(len(seq)) |
|---|
| 256 | n/a | except ValueError: |
|---|
| 257 | n/a | raise IndexError('Cannot choose from an empty sequence') from None |
|---|
| 258 | n/a | return seq[i] |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | def shuffle(self, x, random=None): |
|---|
| 261 | n/a | """Shuffle list x in place, and return None. |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | Optional argument random is a 0-argument function returning a |
|---|
| 264 | n/a | random float in [0.0, 1.0); if it is the default None, the |
|---|
| 265 | n/a | standard random.random will be used. |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | """ |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | if random is None: |
|---|
| 270 | n/a | randbelow = self._randbelow |
|---|
| 271 | n/a | for i in reversed(range(1, len(x))): |
|---|
| 272 | n/a | # pick an element in x[:i+1] with which to exchange x[i] |
|---|
| 273 | n/a | j = randbelow(i+1) |
|---|
| 274 | n/a | x[i], x[j] = x[j], x[i] |
|---|
| 275 | n/a | else: |
|---|
| 276 | n/a | _int = int |
|---|
| 277 | n/a | for i in reversed(range(1, len(x))): |
|---|
| 278 | n/a | # pick an element in x[:i+1] with which to exchange x[i] |
|---|
| 279 | n/a | j = _int(random() * (i+1)) |
|---|
| 280 | n/a | x[i], x[j] = x[j], x[i] |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | def sample(self, population, k): |
|---|
| 283 | n/a | """Chooses k unique random elements from a population sequence or set. |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | Returns a new list containing elements from the population while |
|---|
| 286 | n/a | leaving the original population unchanged. The resulting list is |
|---|
| 287 | n/a | in selection order so that all sub-slices will also be valid random |
|---|
| 288 | n/a | samples. This allows raffle winners (the sample) to be partitioned |
|---|
| 289 | n/a | into grand prize and second place winners (the subslices). |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | Members of the population need not be hashable or unique. If the |
|---|
| 292 | n/a | population contains repeats, then each occurrence is a possible |
|---|
| 293 | n/a | selection in the sample. |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | To choose a sample in a range of integers, use range as an argument. |
|---|
| 296 | n/a | This is especially fast and space efficient for sampling from a |
|---|
| 297 | n/a | large population: sample(range(10000000), 60) |
|---|
| 298 | n/a | """ |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | # Sampling without replacement entails tracking either potential |
|---|
| 301 | n/a | # selections (the pool) in a list or previous selections in a set. |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | # When the number of selections is small compared to the |
|---|
| 304 | n/a | # population, then tracking selections is efficient, requiring |
|---|
| 305 | n/a | # only a small set and an occasional reselection. For |
|---|
| 306 | n/a | # a larger number of selections, the pool tracking method is |
|---|
| 307 | n/a | # preferred since the list takes less space than the |
|---|
| 308 | n/a | # set and it doesn't suffer from frequent reselections. |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | if isinstance(population, _Set): |
|---|
| 311 | n/a | population = tuple(population) |
|---|
| 312 | n/a | if not isinstance(population, _Sequence): |
|---|
| 313 | n/a | raise TypeError("Population must be a sequence or set. For dicts, use list(d).") |
|---|
| 314 | n/a | randbelow = self._randbelow |
|---|
| 315 | n/a | n = len(population) |
|---|
| 316 | n/a | if not 0 <= k <= n: |
|---|
| 317 | n/a | raise ValueError("Sample larger than population or is negative") |
|---|
| 318 | n/a | result = [None] * k |
|---|
| 319 | n/a | setsize = 21 # size of a small set minus size of an empty list |
|---|
| 320 | n/a | if k > 5: |
|---|
| 321 | n/a | setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets |
|---|
| 322 | n/a | if n <= setsize: |
|---|
| 323 | n/a | # An n-length list is smaller than a k-length set |
|---|
| 324 | n/a | pool = list(population) |
|---|
| 325 | n/a | for i in range(k): # invariant: non-selected at [0,n-i) |
|---|
| 326 | n/a | j = randbelow(n-i) |
|---|
| 327 | n/a | result[i] = pool[j] |
|---|
| 328 | n/a | pool[j] = pool[n-i-1] # move non-selected item into vacancy |
|---|
| 329 | n/a | else: |
|---|
| 330 | n/a | selected = set() |
|---|
| 331 | n/a | selected_add = selected.add |
|---|
| 332 | n/a | for i in range(k): |
|---|
| 333 | n/a | j = randbelow(n) |
|---|
| 334 | n/a | while j in selected: |
|---|
| 335 | n/a | j = randbelow(n) |
|---|
| 336 | n/a | selected_add(j) |
|---|
| 337 | n/a | result[i] = population[j] |
|---|
| 338 | n/a | return result |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | def choices(self, population, weights=None, *, cum_weights=None, k=1): |
|---|
| 341 | n/a | """Return a k sized list of population elements chosen with replacement. |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | If the relative weights or cumulative weights are not specified, |
|---|
| 344 | n/a | the selections are made with equal probability. |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | """ |
|---|
| 347 | n/a | random = self.random |
|---|
| 348 | n/a | if cum_weights is None: |
|---|
| 349 | n/a | if weights is None: |
|---|
| 350 | n/a | _int = int |
|---|
| 351 | n/a | total = len(population) |
|---|
| 352 | n/a | return [population[_int(random() * total)] for i in range(k)] |
|---|
| 353 | n/a | cum_weights = list(_itertools.accumulate(weights)) |
|---|
| 354 | n/a | elif weights is not None: |
|---|
| 355 | n/a | raise TypeError('Cannot specify both weights and cumulative weights') |
|---|
| 356 | n/a | if len(cum_weights) != len(population): |
|---|
| 357 | n/a | raise ValueError('The number of weights does not match the population') |
|---|
| 358 | n/a | bisect = _bisect.bisect |
|---|
| 359 | n/a | total = cum_weights[-1] |
|---|
| 360 | n/a | return [population[bisect(cum_weights, random() * total)] for i in range(k)] |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | ## -------------------- real-valued distributions ------------------- |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | ## -------------------- uniform distribution ------------------- |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | def uniform(self, a, b): |
|---|
| 367 | n/a | "Get a random number in the range [a, b) or [a, b] depending on rounding." |
|---|
| 368 | n/a | return a + (b-a) * self.random() |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | ## -------------------- triangular -------------------- |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | def triangular(self, low=0.0, high=1.0, mode=None): |
|---|
| 373 | n/a | """Triangular distribution. |
|---|
| 374 | n/a | |
|---|
| 375 | n/a | Continuous distribution bounded by given lower and upper limits, |
|---|
| 376 | n/a | and having a given mode value in-between. |
|---|
| 377 | n/a | |
|---|
| 378 | n/a | http://en.wikipedia.org/wiki/Triangular_distribution |
|---|
| 379 | n/a | |
|---|
| 380 | n/a | """ |
|---|
| 381 | n/a | u = self.random() |
|---|
| 382 | n/a | try: |
|---|
| 383 | n/a | c = 0.5 if mode is None else (mode - low) / (high - low) |
|---|
| 384 | n/a | except ZeroDivisionError: |
|---|
| 385 | n/a | return low |
|---|
| 386 | n/a | if u > c: |
|---|
| 387 | n/a | u = 1.0 - u |
|---|
| 388 | n/a | c = 1.0 - c |
|---|
| 389 | n/a | low, high = high, low |
|---|
| 390 | n/a | return low + (high - low) * (u * c) ** 0.5 |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | ## -------------------- normal distribution -------------------- |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | def normalvariate(self, mu, sigma): |
|---|
| 395 | n/a | """Normal distribution. |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | mu is the mean, and sigma is the standard deviation. |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | """ |
|---|
| 400 | n/a | # mu = mean, sigma = standard deviation |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | # Uses Kinderman and Monahan method. Reference: Kinderman, |
|---|
| 403 | n/a | # A.J. and Monahan, J.F., "Computer generation of random |
|---|
| 404 | n/a | # variables using the ratio of uniform deviates", ACM Trans |
|---|
| 405 | n/a | # Math Software, 3, (1977), pp257-260. |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | random = self.random |
|---|
| 408 | n/a | while 1: |
|---|
| 409 | n/a | u1 = random() |
|---|
| 410 | n/a | u2 = 1.0 - random() |
|---|
| 411 | n/a | z = NV_MAGICCONST*(u1-0.5)/u2 |
|---|
| 412 | n/a | zz = z*z/4.0 |
|---|
| 413 | n/a | if zz <= -_log(u2): |
|---|
| 414 | n/a | break |
|---|
| 415 | n/a | return mu + z*sigma |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | ## -------------------- lognormal distribution -------------------- |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | def lognormvariate(self, mu, sigma): |
|---|
| 420 | n/a | """Log normal distribution. |
|---|
| 421 | n/a | |
|---|
| 422 | n/a | If you take the natural logarithm of this distribution, you'll get a |
|---|
| 423 | n/a | normal distribution with mean mu and standard deviation sigma. |
|---|
| 424 | n/a | mu can have any value, and sigma must be greater than zero. |
|---|
| 425 | n/a | |
|---|
| 426 | n/a | """ |
|---|
| 427 | n/a | return _exp(self.normalvariate(mu, sigma)) |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | ## -------------------- exponential distribution -------------------- |
|---|
| 430 | n/a | |
|---|
| 431 | n/a | def expovariate(self, lambd): |
|---|
| 432 | n/a | """Exponential distribution. |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | lambd is 1.0 divided by the desired mean. It should be |
|---|
| 435 | n/a | nonzero. (The parameter would be called "lambda", but that is |
|---|
| 436 | n/a | a reserved word in Python.) Returned values range from 0 to |
|---|
| 437 | n/a | positive infinity if lambd is positive, and from negative |
|---|
| 438 | n/a | infinity to 0 if lambd is negative. |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | """ |
|---|
| 441 | n/a | # lambd: rate lambd = 1/mean |
|---|
| 442 | n/a | # ('lambda' is a Python reserved word) |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | # we use 1-random() instead of random() to preclude the |
|---|
| 445 | n/a | # possibility of taking the log of zero. |
|---|
| 446 | n/a | return -_log(1.0 - self.random())/lambd |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | ## -------------------- von Mises distribution -------------------- |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | def vonmisesvariate(self, mu, kappa): |
|---|
| 451 | n/a | """Circular data distribution. |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | mu is the mean angle, expressed in radians between 0 and 2*pi, and |
|---|
| 454 | n/a | kappa is the concentration parameter, which must be greater than or |
|---|
| 455 | n/a | equal to zero. If kappa is equal to zero, this distribution reduces |
|---|
| 456 | n/a | to a uniform random angle over the range 0 to 2*pi. |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | """ |
|---|
| 459 | n/a | # mu: mean angle (in radians between 0 and 2*pi) |
|---|
| 460 | n/a | # kappa: concentration parameter kappa (>= 0) |
|---|
| 461 | n/a | # if kappa = 0 generate uniform random angle |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | # Based upon an algorithm published in: Fisher, N.I., |
|---|
| 464 | n/a | # "Statistical Analysis of Circular Data", Cambridge |
|---|
| 465 | n/a | # University Press, 1993. |
|---|
| 466 | n/a | |
|---|
| 467 | n/a | # Thanks to Magnus Kessler for a correction to the |
|---|
| 468 | n/a | # implementation of step 4. |
|---|
| 469 | n/a | |
|---|
| 470 | n/a | random = self.random |
|---|
| 471 | n/a | if kappa <= 1e-6: |
|---|
| 472 | n/a | return TWOPI * random() |
|---|
| 473 | n/a | |
|---|
| 474 | n/a | s = 0.5 / kappa |
|---|
| 475 | n/a | r = s + _sqrt(1.0 + s * s) |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | while 1: |
|---|
| 478 | n/a | u1 = random() |
|---|
| 479 | n/a | z = _cos(_pi * u1) |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | d = z / (r + z) |
|---|
| 482 | n/a | u2 = random() |
|---|
| 483 | n/a | if u2 < 1.0 - d * d or u2 <= (1.0 - d) * _exp(d): |
|---|
| 484 | n/a | break |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | q = 1.0 / r |
|---|
| 487 | n/a | f = (q + z) / (1.0 + q * z) |
|---|
| 488 | n/a | u3 = random() |
|---|
| 489 | n/a | if u3 > 0.5: |
|---|
| 490 | n/a | theta = (mu + _acos(f)) % TWOPI |
|---|
| 491 | n/a | else: |
|---|
| 492 | n/a | theta = (mu - _acos(f)) % TWOPI |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | return theta |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | ## -------------------- gamma distribution -------------------- |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | def gammavariate(self, alpha, beta): |
|---|
| 499 | n/a | """Gamma distribution. Not the gamma function! |
|---|
| 500 | n/a | |
|---|
| 501 | n/a | Conditions on the parameters are alpha > 0 and beta > 0. |
|---|
| 502 | n/a | |
|---|
| 503 | n/a | The probability distribution function is: |
|---|
| 504 | n/a | |
|---|
| 505 | n/a | x ** (alpha - 1) * math.exp(-x / beta) |
|---|
| 506 | n/a | pdf(x) = -------------------------------------- |
|---|
| 507 | n/a | math.gamma(alpha) * beta ** alpha |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | """ |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | # Warning: a few older sources define the gamma distribution in terms |
|---|
| 514 | n/a | # of alpha > -1.0 |
|---|
| 515 | n/a | if alpha <= 0.0 or beta <= 0.0: |
|---|
| 516 | n/a | raise ValueError('gammavariate: alpha and beta must be > 0.0') |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | random = self.random |
|---|
| 519 | n/a | if alpha > 1.0: |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | # Uses R.C.H. Cheng, "The generation of Gamma |
|---|
| 522 | n/a | # variables with non-integral shape parameters", |
|---|
| 523 | n/a | # Applied Statistics, (1977), 26, No. 1, p71-74 |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | ainv = _sqrt(2.0 * alpha - 1.0) |
|---|
| 526 | n/a | bbb = alpha - LOG4 |
|---|
| 527 | n/a | ccc = alpha + ainv |
|---|
| 528 | n/a | |
|---|
| 529 | n/a | while 1: |
|---|
| 530 | n/a | u1 = random() |
|---|
| 531 | n/a | if not 1e-7 < u1 < .9999999: |
|---|
| 532 | n/a | continue |
|---|
| 533 | n/a | u2 = 1.0 - random() |
|---|
| 534 | n/a | v = _log(u1/(1.0-u1))/ainv |
|---|
| 535 | n/a | x = alpha*_exp(v) |
|---|
| 536 | n/a | z = u1*u1*u2 |
|---|
| 537 | n/a | r = bbb+ccc*v-x |
|---|
| 538 | n/a | if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z): |
|---|
| 539 | n/a | return x * beta |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | elif alpha == 1.0: |
|---|
| 542 | n/a | # expovariate(1) |
|---|
| 543 | n/a | u = random() |
|---|
| 544 | n/a | while u <= 1e-7: |
|---|
| 545 | n/a | u = random() |
|---|
| 546 | n/a | return -_log(u) * beta |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | else: # alpha is between 0 and 1 (exclusive) |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | while 1: |
|---|
| 553 | n/a | u = random() |
|---|
| 554 | n/a | b = (_e + alpha)/_e |
|---|
| 555 | n/a | p = b*u |
|---|
| 556 | n/a | if p <= 1.0: |
|---|
| 557 | n/a | x = p ** (1.0/alpha) |
|---|
| 558 | n/a | else: |
|---|
| 559 | n/a | x = -_log((b-p)/alpha) |
|---|
| 560 | n/a | u1 = random() |
|---|
| 561 | n/a | if p > 1.0: |
|---|
| 562 | n/a | if u1 <= x ** (alpha - 1.0): |
|---|
| 563 | n/a | break |
|---|
| 564 | n/a | elif u1 <= _exp(-x): |
|---|
| 565 | n/a | break |
|---|
| 566 | n/a | return x * beta |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | ## -------------------- Gauss (faster alternative) -------------------- |
|---|
| 569 | n/a | |
|---|
| 570 | n/a | def gauss(self, mu, sigma): |
|---|
| 571 | n/a | """Gaussian distribution. |
|---|
| 572 | n/a | |
|---|
| 573 | n/a | mu is the mean, and sigma is the standard deviation. This is |
|---|
| 574 | n/a | slightly faster than the normalvariate() function. |
|---|
| 575 | n/a | |
|---|
| 576 | n/a | Not thread-safe without a lock around calls. |
|---|
| 577 | n/a | |
|---|
| 578 | n/a | """ |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | # When x and y are two variables from [0, 1), uniformly |
|---|
| 581 | n/a | # distributed, then |
|---|
| 582 | n/a | # |
|---|
| 583 | n/a | # cos(2*pi*x)*sqrt(-2*log(1-y)) |
|---|
| 584 | n/a | # sin(2*pi*x)*sqrt(-2*log(1-y)) |
|---|
| 585 | n/a | # |
|---|
| 586 | n/a | # are two *independent* variables with normal distribution |
|---|
| 587 | n/a | # (mu = 0, sigma = 1). |
|---|
| 588 | n/a | # (Lambert Meertens) |
|---|
| 589 | n/a | # (corrected version; bug discovered by Mike Miller, fixed by LM) |
|---|
| 590 | n/a | |
|---|
| 591 | n/a | # Multithreading note: When two threads call this function |
|---|
| 592 | n/a | # simultaneously, it is possible that they will receive the |
|---|
| 593 | n/a | # same return value. The window is very small though. To |
|---|
| 594 | n/a | # avoid this, you have to use a lock around all calls. (I |
|---|
| 595 | n/a | # didn't want to slow this down in the serial case by using a |
|---|
| 596 | n/a | # lock here.) |
|---|
| 597 | n/a | |
|---|
| 598 | n/a | random = self.random |
|---|
| 599 | n/a | z = self.gauss_next |
|---|
| 600 | n/a | self.gauss_next = None |
|---|
| 601 | n/a | if z is None: |
|---|
| 602 | n/a | x2pi = random() * TWOPI |
|---|
| 603 | n/a | g2rad = _sqrt(-2.0 * _log(1.0 - random())) |
|---|
| 604 | n/a | z = _cos(x2pi) * g2rad |
|---|
| 605 | n/a | self.gauss_next = _sin(x2pi) * g2rad |
|---|
| 606 | n/a | |
|---|
| 607 | n/a | return mu + z*sigma |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | ## -------------------- beta -------------------- |
|---|
| 610 | n/a | ## See |
|---|
| 611 | n/a | ## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html |
|---|
| 612 | n/a | ## for Ivan Frohne's insightful analysis of why the original implementation: |
|---|
| 613 | n/a | ## |
|---|
| 614 | n/a | ## def betavariate(self, alpha, beta): |
|---|
| 615 | n/a | ## # Discrete Event Simulation in C, pp 87-88. |
|---|
| 616 | n/a | ## |
|---|
| 617 | n/a | ## y = self.expovariate(alpha) |
|---|
| 618 | n/a | ## z = self.expovariate(1.0/beta) |
|---|
| 619 | n/a | ## return z/(y+z) |
|---|
| 620 | n/a | ## |
|---|
| 621 | n/a | ## was dead wrong, and how it probably got that way. |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | def betavariate(self, alpha, beta): |
|---|
| 624 | n/a | """Beta distribution. |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | Conditions on the parameters are alpha > 0 and beta > 0. |
|---|
| 627 | n/a | Returned values range between 0 and 1. |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | """ |
|---|
| 630 | n/a | |
|---|
| 631 | n/a | # This version due to Janne Sinkkonen, and matches all the std |
|---|
| 632 | n/a | # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution"). |
|---|
| 633 | n/a | y = self.gammavariate(alpha, 1.0) |
|---|
| 634 | n/a | if y == 0: |
|---|
| 635 | n/a | return 0.0 |
|---|
| 636 | n/a | else: |
|---|
| 637 | n/a | return y / (y + self.gammavariate(beta, 1.0)) |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | ## -------------------- Pareto -------------------- |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | def paretovariate(self, alpha): |
|---|
| 642 | n/a | """Pareto distribution. alpha is the shape parameter.""" |
|---|
| 643 | n/a | # Jain, pg. 495 |
|---|
| 644 | n/a | |
|---|
| 645 | n/a | u = 1.0 - self.random() |
|---|
| 646 | n/a | return 1.0 / u ** (1.0/alpha) |
|---|
| 647 | n/a | |
|---|
| 648 | n/a | ## -------------------- Weibull -------------------- |
|---|
| 649 | n/a | |
|---|
| 650 | n/a | def weibullvariate(self, alpha, beta): |
|---|
| 651 | n/a | """Weibull distribution. |
|---|
| 652 | n/a | |
|---|
| 653 | n/a | alpha is the scale parameter and beta is the shape parameter. |
|---|
| 654 | n/a | |
|---|
| 655 | n/a | """ |
|---|
| 656 | n/a | # Jain, pg. 499; bug fix courtesy Bill Arms |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | u = 1.0 - self.random() |
|---|
| 659 | n/a | return alpha * (-_log(u)) ** (1.0/beta) |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | ## --------------- Operating System Random Source ------------------ |
|---|
| 662 | n/a | |
|---|
| 663 | n/a | class SystemRandom(Random): |
|---|
| 664 | n/a | """Alternate random number generator using sources provided |
|---|
| 665 | n/a | by the operating system (such as /dev/urandom on Unix or |
|---|
| 666 | n/a | CryptGenRandom on Windows). |
|---|
| 667 | n/a | |
|---|
| 668 | n/a | Not available on all systems (see os.urandom() for details). |
|---|
| 669 | n/a | """ |
|---|
| 670 | n/a | |
|---|
| 671 | n/a | def random(self): |
|---|
| 672 | n/a | """Get the next random number in the range [0.0, 1.0).""" |
|---|
| 673 | n/a | return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF |
|---|
| 674 | n/a | |
|---|
| 675 | n/a | def getrandbits(self, k): |
|---|
| 676 | n/a | """getrandbits(k) -> x. Generates an int with k random bits.""" |
|---|
| 677 | n/a | if k <= 0: |
|---|
| 678 | n/a | raise ValueError('number of bits must be greater than zero') |
|---|
| 679 | n/a | if k != int(k): |
|---|
| 680 | n/a | raise TypeError('number of bits should be an integer') |
|---|
| 681 | n/a | numbytes = (k + 7) // 8 # bits / 8 and rounded up |
|---|
| 682 | n/a | x = int.from_bytes(_urandom(numbytes), 'big') |
|---|
| 683 | n/a | return x >> (numbytes * 8 - k) # trim excess bits |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | def seed(self, *args, **kwds): |
|---|
| 686 | n/a | "Stub method. Not used for a system random number generator." |
|---|
| 687 | n/a | return None |
|---|
| 688 | n/a | |
|---|
| 689 | n/a | def _notimplemented(self, *args, **kwds): |
|---|
| 690 | n/a | "Method should not be called for a system random number generator." |
|---|
| 691 | n/a | raise NotImplementedError('System entropy source does not have state.') |
|---|
| 692 | n/a | getstate = setstate = _notimplemented |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | ## -------------------- test program -------------------- |
|---|
| 695 | n/a | |
|---|
| 696 | n/a | def _test_generator(n, func, args): |
|---|
| 697 | n/a | import time |
|---|
| 698 | n/a | print(n, 'times', func.__name__) |
|---|
| 699 | n/a | total = 0.0 |
|---|
| 700 | n/a | sqsum = 0.0 |
|---|
| 701 | n/a | smallest = 1e10 |
|---|
| 702 | n/a | largest = -1e10 |
|---|
| 703 | n/a | t0 = time.time() |
|---|
| 704 | n/a | for i in range(n): |
|---|
| 705 | n/a | x = func(*args) |
|---|
| 706 | n/a | total += x |
|---|
| 707 | n/a | sqsum = sqsum + x*x |
|---|
| 708 | n/a | smallest = min(x, smallest) |
|---|
| 709 | n/a | largest = max(x, largest) |
|---|
| 710 | n/a | t1 = time.time() |
|---|
| 711 | n/a | print(round(t1-t0, 3), 'sec,', end=' ') |
|---|
| 712 | n/a | avg = total/n |
|---|
| 713 | n/a | stddev = _sqrt(sqsum/n - avg*avg) |
|---|
| 714 | n/a | print('avg %g, stddev %g, min %g, max %g\n' % \ |
|---|
| 715 | n/a | (avg, stddev, smallest, largest)) |
|---|
| 716 | n/a | |
|---|
| 717 | n/a | |
|---|
| 718 | n/a | def _test(N=2000): |
|---|
| 719 | n/a | _test_generator(N, random, ()) |
|---|
| 720 | n/a | _test_generator(N, normalvariate, (0.0, 1.0)) |
|---|
| 721 | n/a | _test_generator(N, lognormvariate, (0.0, 1.0)) |
|---|
| 722 | n/a | _test_generator(N, vonmisesvariate, (0.0, 1.0)) |
|---|
| 723 | n/a | _test_generator(N, gammavariate, (0.01, 1.0)) |
|---|
| 724 | n/a | _test_generator(N, gammavariate, (0.1, 1.0)) |
|---|
| 725 | n/a | _test_generator(N, gammavariate, (0.1, 2.0)) |
|---|
| 726 | n/a | _test_generator(N, gammavariate, (0.5, 1.0)) |
|---|
| 727 | n/a | _test_generator(N, gammavariate, (0.9, 1.0)) |
|---|
| 728 | n/a | _test_generator(N, gammavariate, (1.0, 1.0)) |
|---|
| 729 | n/a | _test_generator(N, gammavariate, (2.0, 1.0)) |
|---|
| 730 | n/a | _test_generator(N, gammavariate, (20.0, 1.0)) |
|---|
| 731 | n/a | _test_generator(N, gammavariate, (200.0, 1.0)) |
|---|
| 732 | n/a | _test_generator(N, gauss, (0.0, 1.0)) |
|---|
| 733 | n/a | _test_generator(N, betavariate, (3.0, 3.0)) |
|---|
| 734 | n/a | _test_generator(N, triangular, (0.0, 1.0, 1.0/3.0)) |
|---|
| 735 | n/a | |
|---|
| 736 | n/a | # Create one instance, seeded from current time, and export its methods |
|---|
| 737 | n/a | # as module-level functions. The functions share state across all uses |
|---|
| 738 | n/a | #(both in the user's code and in the Python libraries), but that's fine |
|---|
| 739 | n/a | # for most programs and is easier for the casual user than making them |
|---|
| 740 | n/a | # instantiate their own Random() instance. |
|---|
| 741 | n/a | |
|---|
| 742 | n/a | _inst = Random() |
|---|
| 743 | n/a | seed = _inst.seed |
|---|
| 744 | n/a | random = _inst.random |
|---|
| 745 | n/a | uniform = _inst.uniform |
|---|
| 746 | n/a | triangular = _inst.triangular |
|---|
| 747 | n/a | randint = _inst.randint |
|---|
| 748 | n/a | choice = _inst.choice |
|---|
| 749 | n/a | randrange = _inst.randrange |
|---|
| 750 | n/a | sample = _inst.sample |
|---|
| 751 | n/a | shuffle = _inst.shuffle |
|---|
| 752 | n/a | choices = _inst.choices |
|---|
| 753 | n/a | normalvariate = _inst.normalvariate |
|---|
| 754 | n/a | lognormvariate = _inst.lognormvariate |
|---|
| 755 | n/a | expovariate = _inst.expovariate |
|---|
| 756 | n/a | vonmisesvariate = _inst.vonmisesvariate |
|---|
| 757 | n/a | gammavariate = _inst.gammavariate |
|---|
| 758 | n/a | gauss = _inst.gauss |
|---|
| 759 | n/a | betavariate = _inst.betavariate |
|---|
| 760 | n/a | paretovariate = _inst.paretovariate |
|---|
| 761 | n/a | weibullvariate = _inst.weibullvariate |
|---|
| 762 | n/a | getstate = _inst.getstate |
|---|
| 763 | n/a | setstate = _inst.setstate |
|---|
| 764 | n/a | getrandbits = _inst.getrandbits |
|---|
| 765 | n/a | |
|---|
| 766 | n/a | if __name__ == '__main__': |
|---|
| 767 | n/a | _test() |
|---|