class_docstr: Random number generator base class used by bound module functions.
  
  Used to instantiate instances of Random to get generators that don't
  share state.
  
  Class Random can also be subclassed if you want to use a different basic
  generator of your own devising: in that case, override the following
  methods:  random(), seed(), getstate(), and setstate().
  Optionally, implement a getrandbits() method so that randrange()
  can cover arbitrarily large ranges.
class_docstr_tok: ["Random","number","generator","base","class","used","by","bound","module","functions",".","Used","to","instantiate","instances","of","Random","to","get","generators","that","don't","share","state",".","Class","Random","can","also","be","subclassed","if","you","want","to","use","a","different","basic","generator","of","your","own","devising",":","in","that","case",",","override","the","following","methods",":","random","(",")",",","seed","(",")",",","getstate","(",")",",","and","setstate","(",")",".","Optionally",",","implement","a","getrandbits","(",")","method","so","that","randrange","(",")","can","cover","arbitrarily","large","ranges","."]
docstr: Binomial random variable.
  
  Gives the number of successes for *n* independent trials
  with the probability of success in each trial being *p*:
  
      sum(random() < p for i in range(n))
  
  Returns an integer in the range:   0 <= X <= n
  
  The mean (expected value) and variance of the random variable are:
  
      E[X] = n * p
      Var[x] = n * p * (1 - p)
docstr_tok: ["Binomial","random","variable",".","Gives","the","number","of","successes","for","*","n","*","independent","trials","with","the","probability","of","success","in","each","trial","being","*","p","*",":","sum","(","random","(",")","<","p","for","i","in","range","(","n",")",")","Returns","an","integer","in","the","range",":","0","<","=","X","<","=","n","The","mean","(","expected","value",")","and","variance","of","the","random","variable","are",":","E","[","X","]","=","n","*","p","Var","[","x","]","=","n","*","p","*","(","1","-","p",")"]
code: def binomialvariate(self, n=1, p=0.5):
      """Binomial random variable.
  
      Gives the number of successes for *n* independent trials
      with the probability of success in each trial being *p*:
  
          sum(random() < p for i in range(n))
  
      Returns an integer in the range:   0 <= X <= n
  
      The mean (expected value) and variance of the random variable are:
  
          E[X] = n * p
          Var[x] = n * p * (1 - p)
  
      """
      # Error check inputs and handle edge cases
      if n < 0:
          raise ValueError("n must be non-negative")
      if p <= 0.0 or p >= 1.0:
          if p == 0.0:
              return 0
          if p == 1.0:
              return n
          raise ValueError("p must be in the range 0.0 <= p <= 1.0")
  
      random = self.random
  
      # Fast path for a common case
      if n == 1:
          return _index(random() < p)
  
      # Exploit symmetry to establish:  p <= 0.5
      if p > 0.5:
          return n - self.binomialvariate(n, 1.0 - p)
  
      if n * p < 10.0:
          # BG: Geometric method by Devroye with running time of O(np).
          # https://dl.acm.org/doi/pdf/10.1145/42372.42381
          x = y = 0
          c = _log2(1.0 - p)
          if not c:
              return x
          while True:
              y += _floor(_log2(random()) / c) + 1
              if y > n:
                  return x
              x += 1
  
      # BTRS: Transformed rejection with squeeze method by Wolfgang Hörmann
      # https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.47.8407&rep=rep1&type=pdf
      assert n * p >= 10.0 and p <= 0.5
      setup_complete = False
  
      spq = _sqrt(
          n * p * (1.0 - p)
      )  # Standard deviation of the distribution
      b = 1.15 + 2.53 * spq
      a = -0.0873 + 0.0248 * b + 0.01 * p
      c = n * p + 0.5
      vr = 0.92 - 4.2 / b
  
      while True:
          u = random()
          u -= 0.5
          us = 0.5 - _fabs(u)
          k = _floor((2.0 * a / us + b) * u + c)
          if k < 0 or k > n:
              continue
  
          # The early-out "squeeze" test substantially reduces
          # the number of acceptance condition evaluations.
          v = random()
          if us >= 0.07 and v <= vr:
              return k
  
          # Acceptance-rejection test.
          # Note, the original paper erroneously omits the call to log(v)
          # when comparing to the log of the rescaled binomial distribution.
          if not setup_complete:
              alpha = (2.83 + 5.1 / b) * spq
              lpq = _log(p / (1.0 - p))
              m = _floor((n + 1) * p)  # Mode of the distribution
              h = _lgamma(m + 1) + _lgamma(n - m + 1)
              setup_complete = True  # Only needs to be done once
          v *= alpha / (a / (us * us) + b)
          if (
              _log(v)
              <= h - _lgamma(k + 1) - _lgamma(n - k + 1) + (k - m) * lpq
          ):
              return k
  
imports: from math import log, exp, pi, e, ceil
  from math import sqrt, acos, cos, sin
  from math import tau, floor, isfinite
  from math import lgamma, fabs, log2
  from os import urandom
  from _collections_abc import Sequence
  from operator import index
  from itertools import accumulate, repeat
  from bisect import bisect
  import os
  import _random
label_desc: Use image node_id 29 for calling the Random obj's underlying member method code with example usage: obj.binomialvariate(n, p) and returns: _index, unknown, int, n, x, k, k, x