Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / module-random.html
CommitLineData
86530b38
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-whrandom.html" />
13<link rel="prev" href="module-cmath.html" />
14<link rel="parent" href="misc.html" />
15<link rel="next" href="module-whrandom.html" />
16<meta name='aesop' content='information' />
17<title>5.9 random -- Generate pseudo-random numbers</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="5.8 cmath "
25 href="module-cmath.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
28 href="misc.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="5.10 whrandom "
31 href="module-whrandom.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="module-cmath.html">5.8 cmath </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-whrandom.html">5.10 whrandom </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION007900000000000000000">
565.9 <tt class="module">random</tt> --
57 Generate pseudo-random numbers</A>
58</H1>
59
60<P>
61<A NAME="module-random"></A>
62
63<P>
64This module implements pseudo-random number generators for various
65distributions.
66
67<P>
68For integers, uniform selection from a range.
69For sequences, uniform selection of a random element, a function to
70generate a random permutation of a list in-place, and a function for
71random sampling without replacement.
72
73<P>
74On the real line, there are functions to compute uniform, normal (Gaussian),
75lognormal, negative exponential, gamma, and beta distributions.
76For generating distributions of angles, the von Mises distribution
77is available.
78
79<P>
80Almost all module functions depend on the basic function
81<tt class="function">random()</tt>, which generates a random float uniformly in
82the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as
83the core generator. It produces 53-bit precision floats and has a
84period of 2**19937-1. The underlying implementation in C
85is both fast and threadsafe. The Mersenne Twister is one of the most
86extensively tested random number generators in existence. However, being
87completely deterministic, it is not suitable for all purposes, and is
88completely unsuitable for cryptographic purposes.
89
90<P>
91The functions supplied by this module are actually bound methods of a
92hidden instance of the <tt class="class">random.Random</tt> class. You can
93instantiate your own instances of <tt class="class">Random</tt> to get generators
94that don't share state. This is especially useful for multi-threaded
95programs, creating a different instance of <tt class="class">Random</tt> for each
96thread, and using the <tt class="method">jumpahead()</tt> method to ensure that the
97generated sequences seen by each thread don't overlap.
98
99<P>
100Class <tt class="class">Random</tt> can also be subclassed if you want to use a
101different basic generator of your own devising: in that case, override
102the <tt class="method">random()</tt>, <tt class="method">seed()</tt>, <tt class="method">getstate()</tt>,
103<tt class="method">setstate()</tt> and <tt class="method">jumpahead()</tt> methods.
104Optionally, a new generator can supply a <tt class="method">getrandombits()</tt>
105method -- this allows <tt class="method">randrange()</tt> to produce selections
106over an arbitrarily large range.
107
108<span class="versionnote">New in version 2.4:
109the <tt class="method">getrandombits()</tt> method.</span>
110
111<P>
112As an example of subclassing, the <tt class="module">random</tt> module provides
113the <tt class="class">WichmannHill</tt> class which implements an alternative generator
114in pure Python. The class provides a backward compatible way to
115reproduce results from earlier versions of Python which used the
116Wichmann-Hill algorithm as the core generator.
117
118<span class="versionnote">Changed in version 2.3:
119Substituted MersenneTwister for Wichmann-Hill.</span>
120
121<P>
122Bookkeeping functions:
123
124<P>
125<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
126 <td><nobr><b><tt id='l2h-1298' xml:id='l2h-1298' class="function">seed</tt></b>(</nobr></td>
127 <td><var></var><big>[</big><var>x</var><big>]</big><var></var>)</td></tr></table></dt>
128<dd>
129 Initialize the basic random number generator.
130 Optional argument <var>x</var> can be any hashable object.
131 If <var>x</var> is omitted or <code>None</code>, current system time is used;
132 current system time is also used to initialize the generator when the
133 module is first imported. If randomness sources are provided by the
134 operating system, they are used instead of the system time (see the
135 <tt class="function">os.urandom()</tt>
136 function for details on availability).
137<span class="versionnote">Changed in version 2.4:
138formerly,
139 operating system resources were not used.</span>
140
141 If <var>x</var> is not <code>None</code> or an int or long,
142 <code>hash(<var>x</var>)</code> is used instead.
143 If <var>x</var> is an int or long, <var>x</var> is used directly.
144</dl>
145
146<P>
147<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
148 <td><nobr><b><tt id='l2h-1299' xml:id='l2h-1299' class="function">getstate</tt></b>(</nobr></td>
149 <td><var></var>)</td></tr></table></dt>
150<dd>
151 Return an object capturing the current internal state of the
152 generator. This object can be passed to <tt class="function">setstate()</tt> to
153 restore the state.
154
155<span class="versionnote">New in version 2.1.</span>
156
157</dl>
158
159<P>
160<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
161 <td><nobr><b><tt id='l2h-1300' xml:id='l2h-1300' class="function">setstate</tt></b>(</nobr></td>
162 <td><var>state</var>)</td></tr></table></dt>
163<dd>
164 <var>state</var> should have been obtained from a previous call to
165 <tt class="function">getstate()</tt>, and <tt class="function">setstate()</tt> restores the
166 internal state of the generator to what it was at the time
167 <tt class="function">setstate()</tt> was called.
168
169<span class="versionnote">New in version 2.1.</span>
170
171</dl>
172
173<P>
174<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
175 <td><nobr><b><tt id='l2h-1301' xml:id='l2h-1301' class="function">jumpahead</tt></b>(</nobr></td>
176 <td><var>n</var>)</td></tr></table></dt>
177<dd>
178 Change the internal state to one different from and likely far away from
179 the current state. <var>n</var> is a non-negative integer which is used to
180 scramble the current state vector. This is most useful in multi-threaded
181 programs, in conjuction with multiple instances of the <tt class="class">Random</tt>
182 class: <tt class="method">setstate()</tt> or <tt class="method">seed()</tt> can be used to force all
183 instances into the same internal state, and then <tt class="method">jumpahead()</tt>
184 can be used to force the instances' states far apart.
185
186<span class="versionnote">New in version 2.1.</span>
187
188<span class="versionnote">Changed in version 2.3:
189Instead of jumping to a specific state, <var>n</var> steps
190 ahead, <tt class="method">jumpahead(<var>n</var>)</tt> jumps to another state likely to be
191 separated by many steps..</span>
192
193 </dl>
194
195<P>
196<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
197 <td><nobr><b><tt id='l2h-1302' xml:id='l2h-1302' class="function">getrandbits</tt></b>(</nobr></td>
198 <td><var>k</var>)</td></tr></table></dt>
199<dd>
200 Returns a python <tt class="class">long</tt> int with <var>k</var> random bits.
201 This method is supplied with the MersenneTwister generator and some
202 other generators may also provide it as an optional part of the API.
203 When available, <tt class="method">getrandbits()</tt> enables <tt class="method">randrange()</tt>
204 to handle arbitrarily large ranges.
205
206<span class="versionnote">New in version 2.4.</span>
207
208</dl>
209
210<P>
211Functions for integers:
212
213<P>
214<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
215 <td><nobr><b><tt id='l2h-1303' xml:id='l2h-1303' class="function">randrange</tt></b>(</nobr></td>
216 <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>
217<dd>
218 Return a randomly selected element from <code>range(<var>start</var>,
219 <var>stop</var>, <var>step</var>)</code>. This is equivalent to
220 <code>choice(range(<var>start</var>, <var>stop</var>, <var>step</var>))</code>,
221 but doesn't actually build a range object.
222
223<span class="versionnote">New in version 1.5.2.</span>
224
225</dl>
226
227<P>
228<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
229 <td><nobr><b><tt id='l2h-1304' xml:id='l2h-1304' class="function">randint</tt></b>(</nobr></td>
230 <td><var>a, b</var>)</td></tr></table></dt>
231<dd>
232 Return a random integer <var>N</var> such that
233 <code><var>a</var> &lt;= <var>N</var> &lt;= <var>b</var></code>.
234</dl>
235
236<P>
237Functions for sequences:
238
239<P>
240<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
241 <td><nobr><b><tt id='l2h-1305' xml:id='l2h-1305' class="function">choice</tt></b>(</nobr></td>
242 <td><var>seq</var>)</td></tr></table></dt>
243<dd>
244 Return a random element from the non-empty sequence <var>seq</var>.
245 If <var>seq</var> is empty, raises <tt class="exception">IndexError</tt>.
246</dl>
247
248<P>
249<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
250 <td><nobr><b><tt id='l2h-1306' xml:id='l2h-1306' class="function">shuffle</tt></b>(</nobr></td>
251 <td><var>x</var><big>[</big><var>, random</var><big>]</big><var></var>)</td></tr></table></dt>
252<dd>
253 Shuffle the sequence <var>x</var> in place.
254 The optional argument <var>random</var> is a 0-argument function
255 returning a random float in [0.0, 1.0); by default, this is the
256 function <tt class="function">random()</tt>.
257
258<P>
259Note that for even rather small <code>len(<var>x</var>)</code>, the total
260 number of permutations of <var>x</var> is larger than the period of most
261 random number generators; this implies that most permutations of a
262 long sequence can never be generated.
263</dl>
264
265<P>
266<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
267 <td><nobr><b><tt id='l2h-1307' xml:id='l2h-1307' class="function">sample</tt></b>(</nobr></td>
268 <td><var>population, k</var>)</td></tr></table></dt>
269<dd>
270 Return a <var>k</var> length list of unique elements chosen from the
271 population sequence. Used for random sampling without replacement.
272
273<span class="versionnote">New in version 2.3.</span>
274
275<P>
276Returns a new list containing elements from the population while
277 leaving the original population unchanged. The resulting list is
278 in selection order so that all sub-slices will also be valid random
279 samples. This allows raffle winners (the sample) to be partitioned
280 into grand prize and second place winners (the subslices).
281
282<P>
283Members of the population need not be hashable or unique. If the
284 population contains repeats, then each occurrence is a possible
285 selection in the sample.
286
287<P>
288To choose a sample from a range of integers, use <tt class="function">xrange</tt>
289 as an argument. This is especially fast and space efficient for
290 sampling from a large population: <code>sample(xrange(10000000), 60)</code>.
291</dl>
292
293<P>
294The following functions generate specific real-valued distributions.
295Function parameters are named after the corresponding variables in the
296distribution's equation, as used in common mathematical practice; most of
297these equations can be found in any statistics text.
298
299<P>
300<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
301 <td><nobr><b><tt id='l2h-1308' xml:id='l2h-1308' class="function">random</tt></b>(</nobr></td>
302 <td><var></var>)</td></tr></table></dt>
303<dd>
304 Return the next random floating point number in the range [0.0, 1.0).
305</dl>
306
307<P>
308<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
309 <td><nobr><b><tt id='l2h-1309' xml:id='l2h-1309' class="function">uniform</tt></b>(</nobr></td>
310 <td><var>a, b</var>)</td></tr></table></dt>
311<dd>
312 Return a random real number <var>N</var> such that
313 <code><var>a</var> &lt;= <var>N</var> &lt; <var>b</var></code>.
314</dl>
315
316<P>
317<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
318 <td><nobr><b><tt id='l2h-1310' xml:id='l2h-1310' class="function">betavariate</tt></b>(</nobr></td>
319 <td><var>alpha, beta</var>)</td></tr></table></dt>
320<dd>
321 Beta distribution. Conditions on the parameters are
322 <code><var>alpha</var> &gt; -1</code> and <code><var>beta</var> &gt; -1</code>.
323 Returned values range between 0 and 1.
324</dl>
325
326<P>
327<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
328 <td><nobr><b><tt id='l2h-1311' xml:id='l2h-1311' class="function">expovariate</tt></b>(</nobr></td>
329 <td><var>lambd</var>)</td></tr></table></dt>
330<dd>
331 Exponential distribution. <var>lambd</var> is 1.0 divided by the desired
332 mean. (The parameter would be called ``lambda'', but that is a
333 reserved word in Python.) Returned values range from 0 to
334 positive infinity.
335</dl>
336
337<P>
338<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
339 <td><nobr><b><tt id='l2h-1312' xml:id='l2h-1312' class="function">gammavariate</tt></b>(</nobr></td>
340 <td><var>alpha, beta</var>)</td></tr></table></dt>
341<dd>
342 Gamma distribution. (<em>Not</em> the gamma function!) Conditions on
343 the parameters are <code><var>alpha</var> &gt; 0</code> and <code><var>beta</var> &gt; 0</code>.
344</dl>
345
346<P>
347<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
348 <td><nobr><b><tt id='l2h-1313' xml:id='l2h-1313' class="function">gauss</tt></b>(</nobr></td>
349 <td><var>mu, sigma</var>)</td></tr></table></dt>
350<dd>
351 Gaussian distribution. <var>mu</var> is the mean, and <var>sigma</var> is the
352 standard deviation. This is slightly faster than the
353 <tt class="function">normalvariate()</tt> function defined below.
354</dl>
355
356<P>
357<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
358 <td><nobr><b><tt id='l2h-1314' xml:id='l2h-1314' class="function">lognormvariate</tt></b>(</nobr></td>
359 <td><var>mu, sigma</var>)</td></tr></table></dt>
360<dd>
361 Log normal distribution. If you take the natural logarithm of this
362 distribution, you'll get a normal distribution with mean <var>mu</var>
363 and standard deviation <var>sigma</var>. <var>mu</var> can have any value,
364 and <var>sigma</var> must be greater than zero.
365</dl>
366
367<P>
368<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
369 <td><nobr><b><tt id='l2h-1315' xml:id='l2h-1315' class="function">normalvariate</tt></b>(</nobr></td>
370 <td><var>mu, sigma</var>)</td></tr></table></dt>
371<dd>
372 Normal distribution. <var>mu</var> is the mean, and <var>sigma</var> is the
373 standard deviation.
374</dl>
375
376<P>
377<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
378 <td><nobr><b><tt id='l2h-1316' xml:id='l2h-1316' class="function">vonmisesvariate</tt></b>(</nobr></td>
379 <td><var>mu, kappa</var>)</td></tr></table></dt>
380<dd>
381 <var>mu</var> is the mean angle, expressed in radians between 0 and
382 2*<em>pi</em>, and <var>kappa</var> is the concentration parameter, which
383 must be greater than or equal to zero. If <var>kappa</var> is equal to
384 zero, this distribution reduces to a uniform random angle over the
385 range 0 to 2*<em>pi</em>.
386</dl>
387
388<P>
389<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
390 <td><nobr><b><tt id='l2h-1317' xml:id='l2h-1317' class="function">paretovariate</tt></b>(</nobr></td>
391 <td><var>alpha</var>)</td></tr></table></dt>
392<dd>
393 Pareto distribution. <var>alpha</var> is the shape parameter.
394</dl>
395
396<P>
397<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
398 <td><nobr><b><tt id='l2h-1318' xml:id='l2h-1318' class="function">weibullvariate</tt></b>(</nobr></td>
399 <td><var>alpha, beta</var>)</td></tr></table></dt>
400<dd>
401 Weibull distribution. <var>alpha</var> is the scale parameter and
402 <var>beta</var> is the shape parameter.
403</dl>
404
405<P>
406Alternative Generators
407
408<P>
409<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
410 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1319' xml:id='l2h-1319' class="class">WichmannHill</tt></b>(</nobr></td>
411 <td><var></var><big>[</big><var>seed</var><big>]</big><var></var>)</td></tr></table></dt>
412<dd>
413Class that implements the Wichmann-Hill algorithm as the core generator.
414Has all of the same methods as <tt class="class">Random</tt> plus the <tt class="method">whseed</tt>
415method described below. Because this class is implemented in pure
416Python, it is not threadsafe and may require locks between calls. The
417period of the generator is 6,953,607,871,644 which is small enough to
418require care that two independent random sequences do not overlap.
419</dl>
420
421<P>
422<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
423 <td><nobr><b><tt id='l2h-1320' xml:id='l2h-1320' class="function">whseed</tt></b>(</nobr></td>
424 <td><var></var><big>[</big><var>x</var><big>]</big><var></var>)</td></tr></table></dt>
425<dd>
426 This is obsolete, supplied for bit-level compatibility with versions
427 of Python prior to 2.1.
428 See <tt class="function">seed</tt> for details. <tt class="function">whseed</tt> does not guarantee
429 that distinct integer arguments yield distinct internal states, and can
430 yield no more than about 2**24 distinct internal states in all.
431</dl>
432
433<P>
434<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
435 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1321' xml:id='l2h-1321' class="class">SystemRandom</tt></b>(</nobr></td>
436 <td><var></var><big>[</big><var>seed</var><big>]</big><var></var>)</td></tr></table></dt>
437<dd>
438Class that uses the <tt class="function">os.urandom()</tt> function for generating
439random numbers from sources provided by the operating system.
440Not available on all systems.
441Does not rely on software state and sequences are not reproducible.
442Accordingly, the <tt class="method">seed()</tt> and <tt class="method">jumpahead()</tt> methods
443have no effect and are ignored. The <tt class="method">getstate()</tt> and
444<tt class="method">setstate()</tt> methods raise <tt class="exception">NotImplementedError</tt> if
445called.
446
447<span class="versionnote">New in version 2.4.</span>
448
449</dl>
450
451<P>
452<div class="seealso">
453 <p class="heading">See Also:</p>
454
455 <div class="seetext"><p>M. Matsumoto and T. Nishimura, ``Mersenne Twister: A
456 623-dimensionally equidistributed uniform pseudorandom
457 number generator'',
458 <em class="citetitle"
459 >ACM Transactions on Modeling and Computer Simulation</em>
460 Vol. 8, No. 1, January pp.3-30 1998.</p></div>
461
462<P>
463<div class="seetext"><p>Wichmann, B. A. &amp; Hill, I. D., ``Algorithm AS 183:
464 An efficient and portable pseudo-random number generator'',
465 <em class="citetitle"
466 >Applied Statistics</em> 31 (1982) 188-190.</p></div>
467</div>
468
469<DIV CLASS="navigation">
470<div class='online-navigation'>
471<p></p><hr />
472<table align="center" width="100%" cellpadding="0" cellspacing="2">
473<tr>
474<td class='online-navigation'><a rel="prev" title="5.8 cmath "
475 href="module-cmath.html"><img src='../icons/previous.png'
476 border='0' height='32' alt='Previous Page' width='32' /></A></td>
477<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
478 href="misc.html"><img src='../icons/up.png'
479 border='0' height='32' alt='Up One Level' width='32' /></A></td>
480<td class='online-navigation'><a rel="next" title="5.10 whrandom "
481 href="module-whrandom.html"><img src='../icons/next.png'
482 border='0' height='32' alt='Next Page' width='32' /></A></td>
483<td align="center" width="100%">Python Library Reference</td>
484<td class='online-navigation'><a rel="contents" title="Table of Contents"
485 href="contents.html"><img src='../icons/contents.png'
486 border='0' height='32' alt='Contents' width='32' /></A></td>
487<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
488 border='0' height='32' alt='Module Index' width='32' /></a></td>
489<td class='online-navigation'><a rel="index" title="Index"
490 href="genindex.html"><img src='../icons/index.png'
491 border='0' height='32' alt='Index' width='32' /></A></td>
492</tr></table>
493<div class='online-navigation'>
494<b class="navlabel">Previous:</b>
495<a class="sectref" rel="prev" href="module-cmath.html">5.8 cmath </A>
496<b class="navlabel">Up:</b>
497<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
498<b class="navlabel">Next:</b>
499<a class="sectref" rel="next" href="module-whrandom.html">5.10 whrandom </A>
500</div>
501</div>
502<hr />
503<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
504</DIV>
505<!--End of Navigation Panel-->
506<ADDRESS>
507See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
508</ADDRESS>
509</BODY>
510</HTML>