{
    "mode": "pydoc",
    "parameter": "random",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/pydoc/random/json",
    "generated": "2026-06-02T15:49:05Z",
    "sections": {
        "NAME": {
            "content": "random - Random variable generators.\n",
            "subsections": []
        },
        "MODULE REFERENCE": {
            "content": "https://docs.python.org/3.10/library/random.html\n\nThe following documentation is automatically generated from the Python\nsource files.  It may be incomplete, incorrect or include features that\nare considered implementation detail and may vary between Python\nimplementations.  When in doubt, consult the module reference at the\nlocation listed above.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "bytes\n-----\nuniform bytes (values between 0 and 255)\n\nintegers\n--------\nuniform within range\n\nsequences\n---------\npick random element\npick random sample\npick weighted random sample\ngenerate random permutation\n\ndistributions on the real line:\n------------------------------\nuniform\ntriangular\nnormal (Gaussian)\nlognormal\nnegative exponential\ngamma\nbeta\npareto\nWeibull\n\ndistributions on the circle (angles 0 to 2pi)\n---------------------------------------------\ncircular uniform\nvon Mises\n\nGeneral notes on the underlying Mersenne Twister core generator:\n\n* The period is 219937-1.\n* It is one of the most extensively tested generators in existence.\n* The random() method is implemented in C, executes in a single Python step,\nand is, therefore, threadsafe.\n",
            "subsections": []
        },
        "CLASSES": {
            "content": "random.Random(builtins.object)\nRandom\nSystemRandom\n",
            "subsections": [
                {
                    "name": "class Random",
                    "content": "|  Random(x=None)\n|\n|  Random number generator base class used by bound module functions.\n|\n|  Used to instantiate instances of Random to get generators that don't\n|  share state.\n|\n|  Class Random can also be subclassed if you want to use a different basic\n|  generator of your own devising: in that case, override the following\n|  methods:  random(), seed(), getstate(), and setstate().\n|  Optionally, implement a getrandbits() method so that randrange()\n|  can cover arbitrarily large ranges.\n|\n|  Method resolution order:\n|      Random\n|      random.Random\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  getstate(self)\n|      # Issue 17489: Since reduce was defined to fix #759889 this is no\n|      # longer called; we leave it here because it has been here since random was\n|      # rewritten back in 2001 and why risk breaking something.\n|\n|  init(self, x=None)\n|      Initialize an instance.\n|\n|      Optional argument x controls seeding, as for Random.seed().\n|\n|  reduce(self)\n|      Helper for pickle.\n|\n|  setstate(self, state)\n|\n|  betavariate(self, alpha, beta)\n|      Beta distribution.\n|\n|      Conditions on the parameters are alpha > 0 and beta > 0.\n|      Returned values range between 0 and 1.\n|\n|  choice(self, seq)\n|      Choose a random element from a non-empty sequence.\n|\n|  choices(self, population, weights=None, *, cumweights=None, k=1)\n|      Return a k sized list of population elements chosen with replacement.\n|\n|      If the relative weights or cumulative weights are not specified,\n|      the selections are made with equal probability.\n|\n|  expovariate(self, lambd)\n|      Exponential distribution.\n|\n|      lambd is 1.0 divided by the desired mean.  It should be\n|      nonzero.  (The parameter would be called \"lambda\", but that is\n|      a reserved word in Python.)  Returned values range from 0 to\n|      positive infinity if lambd is positive, and from negative\n|      infinity to 0 if lambd is negative.\n|\n|  gammavariate(self, alpha, beta)\n|      Gamma distribution.  Not the gamma function!\n|\n|      Conditions on the parameters are alpha > 0 and beta > 0.\n|\n|      The probability distribution function is:\n|\n|                  x  (alpha - 1) * math.exp(-x / beta)\n|        pdf(x) =  --------------------------------------\n|                    math.gamma(alpha) * beta  alpha\n|\n|  gauss(self, mu, sigma)\n|      Gaussian distribution.\n|\n|      mu is the mean, and sigma is the standard deviation.  This is\n|      slightly faster than the normalvariate() function.\n|\n|      Not thread-safe without a lock around calls.\n|\n|  getstate(self)\n|      Return internal state; can be passed to setstate() later.\n|\n|  lognormvariate(self, mu, sigma)\n|      Log normal distribution.\n|\n|      If you take the natural logarithm of this distribution, you'll get a\n|      normal distribution with mean mu and standard deviation sigma.\n|      mu can have any value, and sigma must be greater than zero.\n|\n|  normalvariate(self, mu, sigma)\n|      Normal distribution.\n|\n|      mu is the mean, and sigma is the standard deviation.\n|\n|  paretovariate(self, alpha)\n|      Pareto distribution.  alpha is the shape parameter.\n|\n|  randbytes(self, n)\n|      Generate n random bytes.\n|\n|  randint(self, a, b)\n|      Return random integer in range [a, b], including both end points.\n|\n|  randrange(self, start, stop=None, step=1)\n|      Choose a random item from range(start, stop[, step]).\n|\n|      This fixes the problem with randint() which includes the\n|      endpoint; in Python this is usually not what you want.\n|\n|  sample(self, population, k, *, counts=None)\n|      Chooses k unique random elements from a population sequence or set.\n|\n|      Returns a new list containing elements from the population while\n|      leaving the original population unchanged.  The resulting list is\n|      in selection order so that all sub-slices will also be valid random\n|      samples.  This allows raffle winners (the sample) to be partitioned\n|      into grand prize and second place winners (the subslices).\n|\n|      Members of the population need not be hashable or unique.  If the\n|      population contains repeats, then each occurrence is a possible\n|      selection in the sample.\n|\n|      Repeated elements can be specified one at a time or with the optional\n|      counts parameter.  For example:\n|\n|          sample(['red', 'blue'], counts=[4, 2], k=5)\n|\n|      is equivalent to:\n|\n|          sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)\n|\n|      To choose a sample from a range of integers, use range() for the\n|      population argument.  This is especially fast and space efficient\n|      for sampling from a large population:\n|\n|          sample(range(10000000), 60)\n|\n|  seed(self, a=None, version=2)\n|      Initialize internal state from a seed.\n|\n|      The only supported seed types are None, int, float,\n|      str, bytes, and bytearray.\n|\n|      None or no argument seeds from current time or from an operating\n|      system specific randomness source if available.\n|\n|      If *a* is an int, all bits are used.\n|\n|      For version 2 (the default), all of the bits are used if *a* is a str,\n|      bytes, or bytearray.  For version 1 (provided for reproducing random\n|      sequences from older versions of Python), the algorithm for str and\n|      bytes generates a narrower range of seeds.\n|\n|  setstate(self, state)\n|      Restore internal state from object returned by getstate().\n|\n|  shuffle(self, x, random=None)\n|      Shuffle list x in place, and return None.\n|\n|      Optional argument random is a 0-argument function returning a\n|      random float in [0.0, 1.0); if it is the default None, the\n|      standard random.random will be used.\n|\n|  triangular(self, low=0.0, high=1.0, mode=None)\n|      Triangular distribution.\n|\n|      Continuous distribution bounded by given lower and upper limits,\n|      and having a given mode value in-between.\n|\n|      http://en.wikipedia.org/wiki/Triangulardistribution\n|\n|  uniform(self, a, b)\n|      Get a random number in the range [a, b) or [a, b] depending on rounding.\n|\n|  vonmisesvariate(self, mu, kappa)\n|      Circular data distribution.\n|\n|      mu is the mean angle, expressed in radians between 0 and 2*pi, and\n|      kappa is the concentration parameter, which must be greater than or\n|      equal to zero.  If kappa is equal to zero, this distribution reduces\n|      to a uniform random angle over the range 0 to 2*pi.\n|\n|  weibullvariate(self, alpha, beta)\n|      Weibull distribution.\n|\n|      alpha is the scale parameter and beta is the shape parameter.\n|\n|  ----------------------------------------------------------------------\n|  Class methods defined here:\n|\n|  initsubclass(kwargs) from builtins.type\n|      Control how subclasses generate random integers.\n|\n|      The algorithm a subclass can use depends on the random() and/or\n|      getrandbits() implementation available to it and determines\n|      whether it can generate random integers from arbitrarily large\n|      ranges.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors defined here:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  VERSION = 3\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from random.Random:\n|\n|  getrandbits(self, k, /)\n|      getrandbits(k) -> x.  Generates an int with k random bits.\n|\n|  random(self, /)\n|      random() -> x in the interval [0, 1).\n|\n|  ----------------------------------------------------------------------\n|  Static methods inherited from random.Random:\n|\n|  new(*args, kwargs) from builtins.type\n|      Create and return a new object.  See help(type) for accurate signature.\n"
                },
                {
                    "name": "class SystemRandom",
                    "content": "|  SystemRandom(x=None)\n|\n|  Alternate random number generator using sources provided\n|  by the operating system (such as /dev/urandom on Unix or\n|  CryptGenRandom on Windows).\n|\n|   Not available on all systems (see os.urandom() for details).\n|\n|  Method resolution order:\n|      SystemRandom\n|      Random\n|      random.Random\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  getrandbits(self, k)\n|      getrandbits(k) -> x.  Generates an int with k random bits.\n|\n|  getstate = notimplemented(self, *args, kwds)\n|\n|  randbytes(self, n)\n|      Generate n random bytes.\n|\n|  random(self)\n|      Get the next random number in the range [0.0, 1.0).\n|\n|  seed(self, *args, kwds)\n|      Stub method.  Not used for a system random number generator.\n|\n|  setstate = notimplemented(self, *args, kwds)\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from Random:\n|\n|  getstate(self)\n|      # Issue 17489: Since reduce was defined to fix #759889 this is no\n|      # longer called; we leave it here because it has been here since random was\n|      # rewritten back in 2001 and why risk breaking something.\n|\n|  init(self, x=None)\n|      Initialize an instance.\n|\n|      Optional argument x controls seeding, as for Random.seed().\n|\n|  reduce(self)\n|      Helper for pickle.\n|\n|  setstate(self, state)\n|\n|  betavariate(self, alpha, beta)\n|      Beta distribution.\n|\n|      Conditions on the parameters are alpha > 0 and beta > 0.\n|      Returned values range between 0 and 1.\n|\n|  choice(self, seq)\n|      Choose a random element from a non-empty sequence.\n|\n|  choices(self, population, weights=None, *, cumweights=None, k=1)\n|      Return a k sized list of population elements chosen with replacement.\n|\n|      If the relative weights or cumulative weights are not specified,\n|      the selections are made with equal probability.\n|\n|  expovariate(self, lambd)\n|      Exponential distribution.\n|\n|      lambd is 1.0 divided by the desired mean.  It should be\n|      nonzero.  (The parameter would be called \"lambda\", but that is\n|      a reserved word in Python.)  Returned values range from 0 to\n|      positive infinity if lambd is positive, and from negative\n|      infinity to 0 if lambd is negative.\n|\n|  gammavariate(self, alpha, beta)\n|      Gamma distribution.  Not the gamma function!\n|\n|      Conditions on the parameters are alpha > 0 and beta > 0.\n|\n|      The probability distribution function is:\n|\n|                  x  (alpha - 1) * math.exp(-x / beta)\n|        pdf(x) =  --------------------------------------\n|                    math.gamma(alpha) * beta  alpha\n|\n|  gauss(self, mu, sigma)\n|      Gaussian distribution.\n|\n|      mu is the mean, and sigma is the standard deviation.  This is\n|      slightly faster than the normalvariate() function.\n|\n|      Not thread-safe without a lock around calls.\n|\n|  lognormvariate(self, mu, sigma)\n|      Log normal distribution.\n|\n|      If you take the natural logarithm of this distribution, you'll get a\n|      normal distribution with mean mu and standard deviation sigma.\n|      mu can have any value, and sigma must be greater than zero.\n|\n|  normalvariate(self, mu, sigma)\n|      Normal distribution.\n|\n|      mu is the mean, and sigma is the standard deviation.\n|\n|  paretovariate(self, alpha)\n|      Pareto distribution.  alpha is the shape parameter.\n|\n|  randint(self, a, b)\n|      Return random integer in range [a, b], including both end points.\n|\n|  randrange(self, start, stop=None, step=1)\n|      Choose a random item from range(start, stop[, step]).\n|\n|      This fixes the problem with randint() which includes the\n|      endpoint; in Python this is usually not what you want.\n|\n|  sample(self, population, k, *, counts=None)\n|      Chooses k unique random elements from a population sequence or set.\n|\n|      Returns a new list containing elements from the population while\n|      leaving the original population unchanged.  The resulting list is\n|      in selection order so that all sub-slices will also be valid random\n|      samples.  This allows raffle winners (the sample) to be partitioned\n|      into grand prize and second place winners (the subslices).\n|\n|      Members of the population need not be hashable or unique.  If the\n|      population contains repeats, then each occurrence is a possible\n|      selection in the sample.\n|\n|      Repeated elements can be specified one at a time or with the optional\n|      counts parameter.  For example:\n|\n|          sample(['red', 'blue'], counts=[4, 2], k=5)\n|\n|      is equivalent to:\n|\n|          sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)\n|\n|      To choose a sample from a range of integers, use range() for the\n|      population argument.  This is especially fast and space efficient\n|      for sampling from a large population:\n|\n|          sample(range(10000000), 60)\n|\n|  shuffle(self, x, random=None)\n|      Shuffle list x in place, and return None.\n|\n|      Optional argument random is a 0-argument function returning a\n|      random float in [0.0, 1.0); if it is the default None, the\n|      standard random.random will be used.\n|\n|  triangular(self, low=0.0, high=1.0, mode=None)\n|      Triangular distribution.\n|\n|      Continuous distribution bounded by given lower and upper limits,\n|      and having a given mode value in-between.\n|\n|      http://en.wikipedia.org/wiki/Triangulardistribution\n|\n|  uniform(self, a, b)\n|      Get a random number in the range [a, b) or [a, b] depending on rounding.\n|\n|  vonmisesvariate(self, mu, kappa)\n|      Circular data distribution.\n|\n|      mu is the mean angle, expressed in radians between 0 and 2*pi, and\n|      kappa is the concentration parameter, which must be greater than or\n|      equal to zero.  If kappa is equal to zero, this distribution reduces\n|      to a uniform random angle over the range 0 to 2*pi.\n|\n|  weibullvariate(self, alpha, beta)\n|      Weibull distribution.\n|\n|      alpha is the scale parameter and beta is the shape parameter.\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from Random:\n|\n|  initsubclass(kwargs) from builtins.type\n|      Control how subclasses generate random integers.\n|\n|      The algorithm a subclass can use depends on the random() and/or\n|      getrandbits() implementation available to it and determines\n|      whether it can generate random integers from arbitrarily large\n|      ranges.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from Random:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes inherited from Random:\n|\n|  VERSION = 3\n|\n|  ----------------------------------------------------------------------\n|  Static methods inherited from random.Random:\n|\n|  new(*args, kwargs) from builtins.type\n|      Create and return a new object.  See help(type) for accurate signature.\n"
                }
            ]
        },
        "FUNCTIONS": {
            "content": "",
            "subsections": [
                {
                    "name": "betavariate",
                    "content": "Beta distribution.\n\nConditions on the parameters are alpha > 0 and beta > 0.\nReturned values range between 0 and 1.\n"
                },
                {
                    "name": "choice",
                    "content": "Choose a random element from a non-empty sequence.\n"
                },
                {
                    "name": "choices",
                    "content": "Return a k sized list of population elements chosen with replacement.\n\nIf the relative weights or cumulative weights are not specified,\nthe selections are made with equal probability.\n"
                },
                {
                    "name": "expovariate",
                    "content": "Exponential distribution.\n\nlambd is 1.0 divided by the desired mean.  It should be\nnonzero.  (The parameter would be called \"lambda\", but that is\na reserved word in Python.)  Returned values range from 0 to\npositive infinity if lambd is positive, and from negative\ninfinity to 0 if lambd is negative.\n"
                },
                {
                    "name": "gammavariate",
                    "content": "Gamma distribution.  Not the gamma function!\n\nConditions on the parameters are alpha > 0 and beta > 0.\n\nThe probability distribution function is:\n\nx  (alpha - 1) * math.exp(-x / beta)\npdf(x) =  --------------------------------------\nmath.gamma(alpha) * beta  alpha\n"
                },
                {
                    "name": "gauss",
                    "content": "Gaussian distribution.\n\nmu is the mean, and sigma is the standard deviation.  This is\nslightly faster than the normalvariate() function.\n\nNot thread-safe without a lock around calls.\n"
                },
                {
                    "name": "getrandbits",
                    "content": "getrandbits(k) -> x.  Generates an int with k random bits.\n"
                },
                {
                    "name": "getstate",
                    "content": "Return internal state; can be passed to setstate() later.\n"
                },
                {
                    "name": "lognormvariate",
                    "content": "Log normal distribution.\n\nIf you take the natural logarithm of this distribution, you'll get a\nnormal distribution with mean mu and standard deviation sigma.\nmu can have any value, and sigma must be greater than zero.\n"
                },
                {
                    "name": "normalvariate",
                    "content": "Normal distribution.\n\nmu is the mean, and sigma is the standard deviation.\n"
                },
                {
                    "name": "paretovariate",
                    "content": "Pareto distribution.  alpha is the shape parameter.\n"
                },
                {
                    "name": "randbytes",
                    "content": "Generate n random bytes.\n"
                },
                {
                    "name": "randint",
                    "content": "Return random integer in range [a, b], including both end points.\n"
                },
                {
                    "name": "random",
                    "content": "random() -> x in the interval [0, 1).\n"
                },
                {
                    "name": "randrange",
                    "content": "Choose a random item from range(start, stop[, step]).\n\nThis fixes the problem with randint() which includes the\nendpoint; in Python this is usually not what you want.\n"
                },
                {
                    "name": "sample",
                    "content": "Chooses k unique random elements from a population sequence or set.\n\nReturns a new list containing elements from the population while\nleaving the original population unchanged.  The resulting list is\nin selection order so that all sub-slices will also be valid random\nsamples.  This allows raffle winners (the sample) to be partitioned\ninto grand prize and second place winners (the subslices).\n\nMembers of the population need not be hashable or unique.  If the\npopulation contains repeats, then each occurrence is a possible\nselection in the sample.\n\nRepeated elements can be specified one at a time or with the optional\ncounts parameter.  For example:\n\nsample(['red', 'blue'], counts=[4, 2], k=5)\n\nis equivalent to:\n\nsample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)\n\nTo choose a sample from a range of integers, use range() for the\npopulation argument.  This is especially fast and space efficient\nfor sampling from a large population:\n\nsample(range(10000000), 60)\n"
                },
                {
                    "name": "seed",
                    "content": "Initialize internal state from a seed.\n\nThe only supported seed types are None, int, float,\nstr, bytes, and bytearray.\n\nNone or no argument seeds from current time or from an operating\nsystem specific randomness source if available.\n\nIf *a* is an int, all bits are used.\n\nFor version 2 (the default), all of the bits are used if *a* is a str,\nbytes, or bytearray.  For version 1 (provided for reproducing random\nsequences from older versions of Python), the algorithm for str and\nbytes generates a narrower range of seeds.\n"
                },
                {
                    "name": "setstate",
                    "content": "Restore internal state from object returned by getstate().\n"
                },
                {
                    "name": "shuffle",
                    "content": "Shuffle list x in place, and return None.\n\nOptional argument random is a 0-argument function returning a\nrandom float in [0.0, 1.0); if it is the default None, the\nstandard random.random will be used.\n"
                },
                {
                    "name": "triangular",
                    "content": "Triangular distribution.\n\nContinuous distribution bounded by given lower and upper limits,\nand having a given mode value in-between.\n\nhttp://en.wikipedia.org/wiki/Triangulardistribution\n"
                },
                {
                    "name": "uniform",
                    "content": "Get a random number in the range [a, b) or [a, b] depending on rounding.\n"
                },
                {
                    "name": "vonmisesvariate",
                    "content": "Circular data distribution.\n\nmu is the mean angle, expressed in radians between 0 and 2*pi, and\nkappa is the concentration parameter, which must be greater than or\nequal to zero.  If kappa is equal to zero, this distribution reduces\nto a uniform random angle over the range 0 to 2*pi.\n"
                },
                {
                    "name": "weibullvariate",
                    "content": "Weibull distribution.\n\nalpha is the scale parameter and beta is the shape parameter.\n"
                }
            ]
        },
        "DATA": {
            "content": "all = ['Random', 'SystemRandom', 'betavariate', 'choice', 'choices...\n",
            "subsections": []
        },
        "FILE": {
            "content": "/usr/lib/python3.10/random.py\n\n",
            "subsections": []
        }
    },
    "summary": "random - Random variable generators.",
    "flags": [],
    "examples": [],
    "see_also": []
}