📄 module-random.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>5.3 random -- Generate pseudo-random numbers</title>
<META NAME="description" CONTENT="5.3 random -- Generate pseudo-random numbers">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-whrandom.html" tppabs="http://www.python.org/doc/current/lib/module-whrandom.html">
<LINK REL="previous" href="module-cmath.html" tppabs="http://www.python.org/doc/current/lib/module-cmath.html">
<LINK REL="up" href="misc.html" tppabs="http://www.python.org/doc/current/lib/misc.html">
<LINK REL="next" href="rng-objects.html" tppabs="http://www.python.org/doc/current/lib/rng-objects.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-cmath.html" tppabs="http://www.python.org/doc/current/lib/module-cmath.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="misc.html" tppabs="http://www.python.org/doc/current/lib/misc.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="rng-objects.html" tppabs="http://www.python.org/doc/current/lib/rng-objects.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-cmath.html" tppabs="http://www.python.org/doc/current/lib/module-cmath.html">5.2 cmath </A>
<b class="navlabel">Up:</b> <a class="sectref" href="misc.html" tppabs="http://www.python.org/doc/current/lib/misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="rng-objects.html" tppabs="http://www.python.org/doc/current/lib/rng-objects.html">5.3.1 The Random Number</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION007300000000000000000">
5.3 <tt class="module">random</tt> --
Generate pseudo-random numbers</A>
</H1>
<P>
<P>
This module implements pseudo-random number generators for various
distributions: on the real line, there are functions to compute normal
or Gaussian, lognormal, negative exponential, gamma, and beta
distributions. For generating distribution of angles, the circular
uniform and von Mises distributions are available.
<P>
The <tt class="module">random</tt> module supports the <i>Random Number
Generator</i> interface, described in section <A href="rng-objects.html#rng-objects" tppabs="http://www.python.org/doc/current/lib/rng-objects.html#rng-objects">5.3.1</A>. This
interface of the module, as well as the distribution-specific
functions described below, all use the pseudo-random generator
provided by the <tt class='module'><a href="module-whrandom.html" tppabs="http://www.python.org/doc/current/lib/module-whrandom.html">whrandom</a></tt> module.
<P>
The following functions are defined to support specific distributions,
and all return real values. Function parameters are named after the
corresponding variables in the distribution's equation, as used in
common mathematical practice; most of these equations can be found in
any statistics text. These are expected to become part of the Random
Number Generator interface in a future release.
<P>
<dl><dt><b><a name='l2h-731'><tt class='function'>betavariate</tt></a></b> (<var>alpha, beta</var>)
<dd>
Beta distribution. Conditions on the parameters are
<code><var>alpha</var> > -1</code> and <code><var>beta</var> > -1</code>.
Returned values range between 0 and 1.
</dl>
<P>
<dl><dt><b><a name='l2h-732'><tt class='function'>cunifvariate</tt></a></b> (<var>mean, arc</var>)
<dd>
Circular uniform distribution. <var>mean</var> is the mean angle, and
<var>arc</var> is the range of the distribution, centered around the mean
angle. Both values must be expressed in radians, and can range
between 0 and <i>pi</i>. Returned values will range between
<code><var>mean</var> - <var>arc</var>/2</code> and <code><var>mean</var> + <var>arc</var>/2</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-733'><tt class='function'>expovariate</tt></a></b> (<var>lambd</var>)
<dd>
Exponential distribution. <var>lambd</var> is 1.0 divided by the desired
mean. (The parameter would be called ``lambda'', but that is a
reserved word in Python.) Returned values will range from 0 to
positive infinity.
</dl>
<P>
<dl><dt><b><a name='l2h-734'><tt class='function'>gamma</tt></a></b> (<var>alpha, beta</var>)
<dd>
Gamma distribution. (<i>Not</i> the gamma function!) Conditions on
the parameters are <code><var>alpha</var> > -1</code> and <code><var>beta</var> > 0</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-735'><tt class='function'>gauss</tt></a></b> (<var>mu, sigma</var>)
<dd>
Gaussian distribution. <var>mu</var> is the mean, and <var>sigma</var> is the
standard deviation. This is slightly faster than the
<tt class="function">normalvariate()</tt> function defined below.
</dl>
<P>
<dl><dt><b><a name='l2h-736'><tt class='function'>lognormvariate</tt></a></b> (<var>mu, sigma</var>)
<dd>
Log normal distribution. If you take the natural logarithm of this
distribution, you'll get a normal distribution with mean <var>mu</var> and
standard deviation <var>sigma</var>. <var>mu</var> can have any value, and
<var>sigma</var> must be greater than zero.
</dl>
<P>
<dl><dt><b><a name='l2h-737'><tt class='function'>normalvariate</tt></a></b> (<var>mu, sigma</var>)
<dd>
Normal distribution. <var>mu</var> is the mean, and <var>sigma</var> is the
standard deviation.
</dl>
<P>
<dl><dt><b><a name='l2h-738'><tt class='function'>vonmisesvariate</tt></a></b> (<var>mu, kappa</var>)
<dd>
<var>mu</var> is the mean angle, expressed in radians between 0 and 2*<i>pi</i>,
and <var>kappa</var> is the concentration parameter, which must be greater
than or equal to zero. If <var>kappa</var> is equal to zero, this
distribution reduces to a uniform random angle over the range 0 to
2*<i>pi</i>.
</dl>
<P>
<dl><dt><b><a name='l2h-739'><tt class='function'>paretovariate</tt></a></b> (<var>alpha</var>)
<dd>
Pareto distribution. <var>alpha</var> is the shape parameter.
</dl>
<P>
<dl><dt><b><a name='l2h-740'><tt class='function'>weibullvariate</tt></a></b> (<var>alpha, beta</var>)
<dd>
Weibull distribution. <var>alpha</var> is the scale parameter and
<var>beta</var> is the shape parameter.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-whrandom.html" tppabs="http://www.python.org/doc/current/lib/module-whrandom.html">whrandom</a></tt>:</b>
<dd>The standard Python random number generator.
</dl>
</div>
<P>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html2003"
href="rng-objects.html" tppabs="http://www.python.org/doc/current/lib/rng-objects.html">5.3.1 The Random Number Generator Interface
</A>
</UL>
<!--End of Table of Child-Links-->
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-cmath.html" tppabs="http://www.python.org/doc/current/lib/module-cmath.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="misc.html" tppabs="http://www.python.org/doc/current/lib/misc.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="rng-objects.html" tppabs="http://www.python.org/doc/current/lib/rng-objects.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-cmath.html" tppabs="http://www.python.org/doc/current/lib/module-cmath.html">5.2 cmath </A>
<b class="navlabel">Up:</b> <a class="sectref" href="misc.html" tppabs="http://www.python.org/doc/current/lib/misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="rng-objects.html" tppabs="http://www.python.org/doc/current/lib/rng-objects.html">5.3.1 The Random Number</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -