Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / module-random.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-whrandom.html" />
<link rel="prev" href="module-cmath.html" />
<link rel="parent" href="misc.html" />
<link rel="next" href="module-whrandom.html" />
<meta name='aesop' content='information' />
<title>5.9 random -- Generate pseudo-random numbers</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.8 cmath "
href="module-cmath.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.10 whrandom "
href="module-whrandom.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-cmath.html">5.8 cmath </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-whrandom.html">5.10 whrandom </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION007900000000000000000">
5.9 <tt class="module">random</tt> --
Generate pseudo-random numbers</A>
</H1>
<P>
<A NAME="module-random"></A>
<P>
This module implements pseudo-random number generators for various
distributions.
<P>
For integers, uniform selection from a range.
For sequences, uniform selection of a random element, a function to
generate a random permutation of a list in-place, and a function for
random sampling without replacement.
<P>
On the real line, there are functions to compute uniform, normal (Gaussian),
lognormal, negative exponential, gamma, and beta distributions.
For generating distributions of angles, the von Mises distribution
is available.
<P>
Almost all module functions depend on the basic function
<tt class="function">random()</tt>, which generates a random float uniformly in
the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as
the core generator. It produces 53-bit precision floats and has a
period of 2**19937-1. The underlying implementation in C
is both fast and threadsafe. The Mersenne Twister is one of the most
extensively tested random number generators in existence. However, being
completely deterministic, it is not suitable for all purposes, and is
completely unsuitable for cryptographic purposes.
<P>
The functions supplied by this module are actually bound methods of a
hidden instance of the <tt class="class">random.Random</tt> class. You can
instantiate your own instances of <tt class="class">Random</tt> to get generators
that don't share state. This is especially useful for multi-threaded
programs, creating a different instance of <tt class="class">Random</tt> for each
thread, and using the <tt class="method">jumpahead()</tt> method to ensure that the
generated sequences seen by each thread don't overlap.
<P>
Class <tt class="class">Random</tt> can also be subclassed if you want to use a
different basic generator of your own devising: in that case, override
the <tt class="method">random()</tt>, <tt class="method">seed()</tt>, <tt class="method">getstate()</tt>,
<tt class="method">setstate()</tt> and <tt class="method">jumpahead()</tt> methods.
Optionally, a new generator can supply a <tt class="method">getrandombits()</tt>
method -- this allows <tt class="method">randrange()</tt> to produce selections
over an arbitrarily large range.
<span class="versionnote">New in version 2.4:
the <tt class="method">getrandombits()</tt> method.</span>
<P>
As an example of subclassing, the <tt class="module">random</tt> module provides
the <tt class="class">WichmannHill</tt> class which implements an alternative generator
in pure Python. The class provides a backward compatible way to
reproduce results from earlier versions of Python which used the
Wichmann-Hill algorithm as the core generator.
<span class="versionnote">Changed in version 2.3:
Substituted MersenneTwister for Wichmann-Hill.</span>
<P>
Bookkeeping functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1298' xml:id='l2h-1298' class="function">seed</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>x</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Initialize the basic random number generator.
Optional argument <var>x</var> can be any hashable object.
If <var>x</var> is omitted or <code>None</code>, current system time is used;
current system time is also used to initialize the generator when the
module is first imported. If randomness sources are provided by the
operating system, they are used instead of the system time (see the
<tt class="function">os.urandom()</tt>
function for details on availability).
<span class="versionnote">Changed in version 2.4:
formerly,
operating system resources were not used.</span>
If <var>x</var> is not <code>None</code> or an int or long,
<code>hash(<var>x</var>)</code> is used instead.
If <var>x</var> is an int or long, <var>x</var> is used directly.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1299' xml:id='l2h-1299' class="function">getstate</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return an object capturing the current internal state of the
generator. This object can be passed to <tt class="function">setstate()</tt> to
restore the state.
<span class="versionnote">New in version 2.1.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1300' xml:id='l2h-1300' class="function">setstate</tt></b>(</nobr></td>
<td><var>state</var>)</td></tr></table></dt>
<dd>
<var>state</var> should have been obtained from a previous call to
<tt class="function">getstate()</tt>, and <tt class="function">setstate()</tt> restores the
internal state of the generator to what it was at the time
<tt class="function">setstate()</tt> was called.
<span class="versionnote">New in version 2.1.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1301' xml:id='l2h-1301' class="function">jumpahead</tt></b>(</nobr></td>
<td><var>n</var>)</td></tr></table></dt>
<dd>
Change the internal state to one different from and likely far away from
the current state. <var>n</var> is a non-negative integer which is used to
scramble the current state vector. This is most useful in multi-threaded
programs, in conjuction with multiple instances of the <tt class="class">Random</tt>
class: <tt class="method">setstate()</tt> or <tt class="method">seed()</tt> can be used to force all
instances into the same internal state, and then <tt class="method">jumpahead()</tt>
can be used to force the instances' states far apart.
<span class="versionnote">New in version 2.1.</span>
<span class="versionnote">Changed in version 2.3:
Instead of jumping to a specific state, <var>n</var> steps
ahead, <tt class="method">jumpahead(<var>n</var>)</tt> jumps to another state likely to be
separated by many steps..</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1302' xml:id='l2h-1302' class="function">getrandbits</tt></b>(</nobr></td>
<td><var>k</var>)</td></tr></table></dt>
<dd>
Returns a python <tt class="class">long</tt> int with <var>k</var> random bits.
This method is supplied with the MersenneTwister generator and some
other generators may also provide it as an optional part of the API.
When available, <tt class="method">getrandbits()</tt> enables <tt class="method">randrange()</tt>
to handle arbitrarily large ranges.
<span class="versionnote">New in version 2.4.</span>
</dl>
<P>
Functions for integers:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1303' xml:id='l2h-1303' class="function">randrange</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>start,</var><big>]</big><var> stop</var><big>[</big><var>, step</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a randomly selected element from <code>range(<var>start</var>,
<var>stop</var>, <var>step</var>)</code>. This is equivalent to
<code>choice(range(<var>start</var>, <var>stop</var>, <var>step</var>))</code>,
but doesn't actually build a range object.
<span class="versionnote">New in version 1.5.2.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1304' xml:id='l2h-1304' class="function">randint</tt></b>(</nobr></td>
<td><var>a, b</var>)</td></tr></table></dt>
<dd>
Return a random integer <var>N</var> such that
<code><var>a</var> &lt;= <var>N</var> &lt;= <var>b</var></code>.
</dl>
<P>
Functions for sequences:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1305' xml:id='l2h-1305' class="function">choice</tt></b>(</nobr></td>
<td><var>seq</var>)</td></tr></table></dt>
<dd>
Return a random element from the non-empty sequence <var>seq</var>.
If <var>seq</var> is empty, raises <tt class="exception">IndexError</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1306' xml:id='l2h-1306' class="function">shuffle</tt></b>(</nobr></td>
<td><var>x</var><big>[</big><var>, random</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Shuffle the sequence <var>x</var> in place.
The optional argument <var>random</var> is a 0-argument function
returning a random float in [0.0, 1.0); by default, this is the
function <tt class="function">random()</tt>.
<P>
Note that for even rather small <code>len(<var>x</var>)</code>, the total
number of permutations of <var>x</var> is larger than the period of most
random number generators; this implies that most permutations of a
long sequence can never be generated.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1307' xml:id='l2h-1307' class="function">sample</tt></b>(</nobr></td>
<td><var>population, k</var>)</td></tr></table></dt>
<dd>
Return a <var>k</var> length list of unique elements chosen from the
population sequence. Used for random sampling without replacement.
<span class="versionnote">New in version 2.3.</span>
<P>
Returns a new list containing elements from the population while
leaving the original population unchanged. The resulting list is
in selection order so that all sub-slices will also be valid random
samples. This allows raffle winners (the sample) to be partitioned
into grand prize and second place winners (the subslices).
<P>
Members of the population need not be hashable or unique. If the
population contains repeats, then each occurrence is a possible
selection in the sample.
<P>
To choose a sample from a range of integers, use <tt class="function">xrange</tt>
as an argument. This is especially fast and space efficient for
sampling from a large population: <code>sample(xrange(10000000), 60)</code>.
</dl>
<P>
The following functions generate specific real-valued distributions.
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.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1308' xml:id='l2h-1308' class="function">random</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the next random floating point number in the range [0.0, 1.0).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1309' xml:id='l2h-1309' class="function">uniform</tt></b>(</nobr></td>
<td><var>a, b</var>)</td></tr></table></dt>
<dd>
Return a random real number <var>N</var> such that
<code><var>a</var> &lt;= <var>N</var> &lt; <var>b</var></code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1310' xml:id='l2h-1310' class="function">betavariate</tt></b>(</nobr></td>
<td><var>alpha, beta</var>)</td></tr></table></dt>
<dd>
Beta distribution. Conditions on the parameters are
<code><var>alpha</var> &gt; -1</code> and <code><var>beta</var> &gt; -1</code>.
Returned values range between 0 and 1.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1311' xml:id='l2h-1311' class="function">expovariate</tt></b>(</nobr></td>
<td><var>lambd</var>)</td></tr></table></dt>
<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 range from 0 to
positive infinity.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1312' xml:id='l2h-1312' class="function">gammavariate</tt></b>(</nobr></td>
<td><var>alpha, beta</var>)</td></tr></table></dt>
<dd>
Gamma distribution. (<em>Not</em> the gamma function!) Conditions on
the parameters are <code><var>alpha</var> &gt; 0</code> and <code><var>beta</var> &gt; 0</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1313' xml:id='l2h-1313' class="function">gauss</tt></b>(</nobr></td>
<td><var>mu, sigma</var>)</td></tr></table></dt>
<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1314' xml:id='l2h-1314' class="function">lognormvariate</tt></b>(</nobr></td>
<td><var>mu, sigma</var>)</td></tr></table></dt>
<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><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1315' xml:id='l2h-1315' class="function">normalvariate</tt></b>(</nobr></td>
<td><var>mu, sigma</var>)</td></tr></table></dt>
<dd>
Normal distribution. <var>mu</var> is the mean, and <var>sigma</var> is the
standard deviation.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1316' xml:id='l2h-1316' class="function">vonmisesvariate</tt></b>(</nobr></td>
<td><var>mu, kappa</var>)</td></tr></table></dt>
<dd>
<var>mu</var> is the mean angle, expressed in radians between 0 and
2*<em>pi</em>, 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*<em>pi</em>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1317' xml:id='l2h-1317' class="function">paretovariate</tt></b>(</nobr></td>
<td><var>alpha</var>)</td></tr></table></dt>
<dd>
Pareto distribution. <var>alpha</var> is the shape parameter.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1318' xml:id='l2h-1318' class="function">weibullvariate</tt></b>(</nobr></td>
<td><var>alpha, beta</var>)</td></tr></table></dt>
<dd>
Weibull distribution. <var>alpha</var> is the scale parameter and
<var>beta</var> is the shape parameter.
</dl>
<P>
Alternative Generators
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1319' xml:id='l2h-1319' class="class">WichmannHill</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>seed</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Class that implements the Wichmann-Hill algorithm as the core generator.
Has all of the same methods as <tt class="class">Random</tt> plus the <tt class="method">whseed</tt>
method described below. Because this class is implemented in pure
Python, it is not threadsafe and may require locks between calls. The
period of the generator is 6,953,607,871,644 which is small enough to
require care that two independent random sequences do not overlap.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1320' xml:id='l2h-1320' class="function">whseed</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>x</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This is obsolete, supplied for bit-level compatibility with versions
of Python prior to 2.1.
See <tt class="function">seed</tt> for details. <tt class="function">whseed</tt> does not guarantee
that distinct integer arguments yield distinct internal states, and can
yield no more than about 2**24 distinct internal states in all.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1321' xml:id='l2h-1321' class="class">SystemRandom</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>seed</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Class that uses the <tt class="function">os.urandom()</tt> function for generating
random numbers from sources provided by the operating system.
Not available on all systems.
Does not rely on software state and sequences are not reproducible.
Accordingly, the <tt class="method">seed()</tt> and <tt class="method">jumpahead()</tt> methods
have no effect and are ignored. The <tt class="method">getstate()</tt> and
<tt class="method">setstate()</tt> methods raise <tt class="exception">NotImplementedError</tt> if
called.
<span class="versionnote">New in version 2.4.</span>
</dl>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<div class="seetext"><p>M. Matsumoto and T. Nishimura, ``Mersenne Twister: A
623-dimensionally equidistributed uniform pseudorandom
number generator'',
<em class="citetitle"
>ACM Transactions on Modeling and Computer Simulation</em>
Vol. 8, No. 1, January pp.3-30 1998.</p></div>
<P>
<div class="seetext"><p>Wichmann, B. A. &amp; Hill, I. D., ``Algorithm AS 183:
An efficient and portable pseudo-random number generator'',
<em class="citetitle"
>Applied Statistics</em> 31 (1982) 188-190.</p></div>
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.8 cmath "
href="module-cmath.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.10 whrandom "
href="module-whrandom.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-cmath.html">5.8 cmath </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-whrandom.html">5.10 whrandom </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>