blob: 8ae242c02921628eab9ca1761cb30b2259bbde78 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`random` --- Generate pseudo-random numbers
2================================================
3
4.. module:: random
5 :synopsis: Generate pseudo-random numbers with various common distributions.
6
Raymond Hettinger10480942011-01-10 03:26:08 +00007**Source code:** :source:`Lib/random.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
Raymond Hettinger4f707fd2011-01-10 19:54:11 +00009--------------
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module implements pseudo-random number generators for various
12distributions.
13
Raymond Hettingerb21dac12010-09-07 05:32:49 +000014For integers, there is uniform selection from a range. For sequences, there is
15uniform selection of a random element, a function to generate a random
16permutation of a list in-place, and a function for random sampling without
17replacement.
Georg Brandl116aa622007-08-15 14:28:22 +000018
19On the real line, there are functions to compute uniform, normal (Gaussian),
20lognormal, negative exponential, gamma, and beta distributions. For generating
21distributions of angles, the von Mises distribution is available.
22
Georg Brandl92849d12016-02-19 08:57:38 +010023Almost all module functions depend on the basic function :func:`.random`, which
Georg Brandl116aa622007-08-15 14:28:22 +000024generates a random float uniformly in the semi-open range [0.0, 1.0). Python
25uses the Mersenne Twister as the core generator. It produces 53-bit precision
26floats and has a period of 2\*\*19937-1. The underlying implementation in C is
27both fast and threadsafe. The Mersenne Twister is one of the most extensively
28tested random number generators in existence. However, being completely
29deterministic, it is not suitable for all purposes, and is completely unsuitable
30for cryptographic purposes.
31
32The functions supplied by this module are actually bound methods of a hidden
33instance of the :class:`random.Random` class. You can instantiate your own
Raymond Hettinger28de64f2008-01-13 23:40:30 +000034instances of :class:`Random` to get generators that don't share state.
Georg Brandl116aa622007-08-15 14:28:22 +000035
36Class :class:`Random` can also be subclassed if you want to use a different
Georg Brandl92849d12016-02-19 08:57:38 +010037basic generator of your own devising: in that case, override the :meth:`~Random.random`,
38:meth:`~Random.seed`, :meth:`~Random.getstate`, and :meth:`~Random.setstate` methods.
39Optionally, a new generator can supply a :meth:`~Random.getrandbits` method --- this
Georg Brandl116aa622007-08-15 14:28:22 +000040allows :meth:`randrange` to produce selections over an arbitrarily large range.
41
Benjamin Peterson21896a32010-03-21 22:03:03 +000042The :mod:`random` module also provides the :class:`SystemRandom` class which
43uses the system function :func:`os.urandom` to generate random numbers
44from sources provided by the operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000045
Raymond Hettingerc89a4512014-05-11 02:26:23 -070046.. warning::
47
48 The pseudo-random generators of this module should not be used for
49 security purposes.
50
Georg Brandl116aa622007-08-15 14:28:22 +000051
Raymond Hettinger10480942011-01-10 03:26:08 +000052Bookkeeping functions:
Georg Brandl116aa622007-08-15 14:28:22 +000053
Ezio Melottie0add762012-09-14 06:32:35 +030054.. function:: seed(a=None, version=2)
Georg Brandl116aa622007-08-15 14:28:22 +000055
Raymond Hettingerf763a722010-09-07 00:38:15 +000056 Initialize the random number generator.
Georg Brandl116aa622007-08-15 14:28:22 +000057
Ezio Melottie0add762012-09-14 06:32:35 +030058 If *a* is omitted or ``None``, the current system time is used. If
Raymond Hettingerf763a722010-09-07 00:38:15 +000059 randomness sources are provided by the operating system, they are used
60 instead of the system time (see the :func:`os.urandom` function for details
61 on availability).
Georg Brandl116aa622007-08-15 14:28:22 +000062
Ezio Melottie0add762012-09-14 06:32:35 +030063 If *a* is an int, it is used directly.
Raymond Hettingerf763a722010-09-07 00:38:15 +000064
65 With version 2 (the default), a :class:`str`, :class:`bytes`, or :class:`bytearray`
Raymond Hettinger16eb8272016-09-04 11:17:28 -070066 object gets converted to an :class:`int` and all of its bits are used.
67
68 With version 1 (provided for reproducing random sequences from older versions
69 of Python), the algorithm for :class:`str` and :class:`bytes` generates a
70 narrower range of seeds.
Raymond Hettingerf763a722010-09-07 00:38:15 +000071
72 .. versionchanged:: 3.2
73 Moved to the version 2 scheme which uses all of the bits in a string seed.
Georg Brandl116aa622007-08-15 14:28:22 +000074
75.. function:: getstate()
76
77 Return an object capturing the current internal state of the generator. This
78 object can be passed to :func:`setstate` to restore the state.
79
Georg Brandl116aa622007-08-15 14:28:22 +000080
81.. function:: setstate(state)
82
83 *state* should have been obtained from a previous call to :func:`getstate`, and
84 :func:`setstate` restores the internal state of the generator to what it was at
Sandro Tosi985104a2012-08-12 15:12:15 +020085 the time :func:`getstate` was called.
Georg Brandl116aa622007-08-15 14:28:22 +000086
Georg Brandl116aa622007-08-15 14:28:22 +000087
Georg Brandl116aa622007-08-15 14:28:22 +000088.. function:: getrandbits(k)
89
Ezio Melotti0639d5a2009-12-19 23:26:38 +000090 Returns a Python integer with *k* random bits. This method is supplied with
Georg Brandl5c106642007-11-29 17:41:05 +000091 the MersenneTwister generator and some other generators may also provide it
Georg Brandl116aa622007-08-15 14:28:22 +000092 as an optional part of the API. When available, :meth:`getrandbits` enables
93 :meth:`randrange` to handle arbitrarily large ranges.
94
Georg Brandl116aa622007-08-15 14:28:22 +000095
96Functions for integers:
97
Ezio Melottie0add762012-09-14 06:32:35 +030098.. function:: randrange(stop)
99 randrange(start, stop[, step])
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101 Return a randomly selected element from ``range(start, stop, step)``. This is
102 equivalent to ``choice(range(start, stop, step))``, but doesn't actually build a
103 range object.
104
Raymond Hettinger05156612010-09-07 04:44:52 +0000105 The positional argument pattern matches that of :func:`range`. Keyword arguments
106 should not be used because the function may use them in unexpected ways.
107
108 .. versionchanged:: 3.2
109 :meth:`randrange` is more sophisticated about producing equally distributed
110 values. Formerly it used a style like ``int(random()*n)`` which could produce
111 slightly uneven distributions.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113.. function:: randint(a, b)
114
Raymond Hettingerafd30452009-02-24 10:57:02 +0000115 Return a random integer *N* such that ``a <= N <= b``. Alias for
116 ``randrange(a, b+1)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Georg Brandl116aa622007-08-15 14:28:22 +0000118
Georg Brandl55ac8f02007-09-01 13:51:09 +0000119Functions for sequences:
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121.. function:: choice(seq)
122
123 Return a random element from the non-empty sequence *seq*. If *seq* is empty,
124 raises :exc:`IndexError`.
125
126
127.. function:: shuffle(x[, random])
128
129 Shuffle the sequence *x* in place. The optional argument *random* is a
130 0-argument function returning a random float in [0.0, 1.0); by default, this is
Georg Brandl92849d12016-02-19 08:57:38 +0100131 the function :func:`.random`.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133 Note that for even rather small ``len(x)``, the total number of permutations of
134 *x* is larger than the period of most random number generators; this implies
135 that most permutations of a long sequence can never be generated.
136
137
138.. function:: sample(population, k)
139
Raymond Hettinger1acde192008-01-14 01:00:53 +0000140 Return a *k* length list of unique elements chosen from the population sequence
141 or set. Used for random sampling without replacement.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Georg Brandl116aa622007-08-15 14:28:22 +0000143 Returns a new list containing elements from the population while leaving the
144 original population unchanged. The resulting list is in selection order so that
145 all sub-slices will also be valid random samples. This allows raffle winners
146 (the sample) to be partitioned into grand prize and second place winners (the
147 subslices).
148
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000149 Members of the population need not be :term:`hashable` or unique. If the population
Georg Brandl116aa622007-08-15 14:28:22 +0000150 contains repeats, then each occurrence is a possible selection in the sample.
151
152 To choose a sample from a range of integers, use an :func:`range` object as an
153 argument. This is especially fast and space efficient for sampling from a large
154 population: ``sample(range(10000000), 60)``.
155
Raymond Hettingerf07d9492012-07-09 12:43:57 -0700156 If the sample size is larger than the population size, a :exc:`ValueError`
Raymond Hettinger86a20f82012-07-08 16:01:53 -0700157 is raised.
158
Georg Brandl116aa622007-08-15 14:28:22 +0000159The following functions generate specific real-valued distributions. Function
160parameters are named after the corresponding variables in the distribution's
161equation, as used in common mathematical practice; most of these equations can
162be found in any statistics text.
163
164
165.. function:: random()
166
167 Return the next random floating point number in the range [0.0, 1.0).
168
169
170.. function:: uniform(a, b)
171
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000172 Return a random floating point number *N* such that ``a <= N <= b`` for
173 ``a <= b`` and ``b <= N <= a`` for ``b < a``.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Raymond Hettingerbe40db02009-06-11 23:12:14 +0000175 The end-point value ``b`` may or may not be included in the range
176 depending on floating-point rounding in the equation ``a + (b-a) * random()``.
Benjamin Peterson35e8c462008-04-24 02:34:53 +0000177
Georg Brandl73dd7c72011-09-17 20:36:28 +0200178
Christian Heimesfe337bf2008-03-23 21:54:12 +0000179.. function:: triangular(low, high, mode)
180
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000181 Return a random floating point number *N* such that ``low <= N <= high`` and
Christian Heimescc47b052008-03-25 14:56:36 +0000182 with the specified *mode* between those bounds. The *low* and *high* bounds
183 default to zero and one. The *mode* argument defaults to the midpoint
184 between the bounds, giving a symmetric distribution.
Christian Heimesfe337bf2008-03-23 21:54:12 +0000185
Georg Brandl116aa622007-08-15 14:28:22 +0000186
187.. function:: betavariate(alpha, beta)
188
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000189 Beta distribution. Conditions on the parameters are ``alpha > 0`` and
190 ``beta > 0``. Returned values range between 0 and 1.
Georg Brandl116aa622007-08-15 14:28:22 +0000191
192
193.. function:: expovariate(lambd)
194
Mark Dickinson2f947362009-01-07 17:54:07 +0000195 Exponential distribution. *lambd* is 1.0 divided by the desired
196 mean. It should be nonzero. (The parameter would be called
197 "lambda", but that is a reserved word in Python.) Returned values
198 range from 0 to positive infinity if *lambd* is positive, and from
199 negative infinity to 0 if *lambd* is negative.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201
202.. function:: gammavariate(alpha, beta)
203
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000204 Gamma distribution. (*Not* the gamma function!) Conditions on the
205 parameters are ``alpha > 0`` and ``beta > 0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Georg Brandl73dd7c72011-09-17 20:36:28 +0200207 The probability distribution function is::
208
209 x ** (alpha - 1) * math.exp(-x / beta)
210 pdf(x) = --------------------------------------
211 math.gamma(alpha) * beta ** alpha
212
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214.. function:: gauss(mu, sigma)
215
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000216 Gaussian distribution. *mu* is the mean, and *sigma* is the standard
217 deviation. This is slightly faster than the :func:`normalvariate` function
218 defined below.
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220
221.. function:: lognormvariate(mu, sigma)
222
223 Log normal distribution. If you take the natural logarithm of this
224 distribution, you'll get a normal distribution with mean *mu* and standard
225 deviation *sigma*. *mu* can have any value, and *sigma* must be greater than
226 zero.
227
228
229.. function:: normalvariate(mu, sigma)
230
231 Normal distribution. *mu* is the mean, and *sigma* is the standard deviation.
232
233
234.. function:: vonmisesvariate(mu, kappa)
235
236 *mu* is the mean angle, expressed in radians between 0 and 2\*\ *pi*, and *kappa*
237 is the concentration parameter, which must be greater than or equal to zero. If
238 *kappa* is equal to zero, this distribution reduces to a uniform random angle
239 over the range 0 to 2\*\ *pi*.
240
241
242.. function:: paretovariate(alpha)
243
244 Pareto distribution. *alpha* is the shape parameter.
245
246
247.. function:: weibullvariate(alpha, beta)
248
249 Weibull distribution. *alpha* is the scale parameter and *beta* is the shape
250 parameter.
251
252
Raymond Hettinger3cdf8712010-12-02 05:35:35 +0000253Alternative Generator:
Georg Brandl116aa622007-08-15 14:28:22 +0000254
Georg Brandl116aa622007-08-15 14:28:22 +0000255.. class:: SystemRandom([seed])
256
257 Class that uses the :func:`os.urandom` function for generating random numbers
258 from sources provided by the operating system. Not available on all systems.
Raymond Hettinger3cdf8712010-12-02 05:35:35 +0000259 Does not rely on software state, and sequences are not reproducible. Accordingly,
Raymond Hettingerafd30452009-02-24 10:57:02 +0000260 the :meth:`seed` method has no effect and is ignored.
Georg Brandl116aa622007-08-15 14:28:22 +0000261 The :meth:`getstate` and :meth:`setstate` methods raise
262 :exc:`NotImplementedError` if called.
263
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Georg Brandl116aa622007-08-15 14:28:22 +0000265.. seealso::
266
267 M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-dimensionally
268 equidistributed uniform pseudorandom number generator", ACM Transactions on
Serhiy Storchakac7b1a0b2016-11-26 13:43:28 +0200269 Modeling and Computer Simulation Vol. 8, No. 1, January pp.3--30 1998.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Raymond Hettinger1fd32a62009-04-01 20:52:13 +0000272 `Complementary-Multiply-with-Carry recipe
Georg Brandl5d941342016-02-26 19:37:12 +0100273 <https://code.activestate.com/recipes/576707/>`_ for a compatible alternative
Raymond Hettinger9743fd02009-04-03 05:47:33 +0000274 random number generator with a long period and comparatively simple update
Raymond Hettinger1fd32a62009-04-01 20:52:13 +0000275 operations.
Raymond Hettinger435cb0f2010-09-06 23:36:31 +0000276
Raymond Hettinger3cdf8712010-12-02 05:35:35 +0000277
Raymond Hettinger435cb0f2010-09-06 23:36:31 +0000278Notes on Reproducibility
Antoine Pitroue72b5862010-12-12 20:13:31 +0000279------------------------
Raymond Hettinger435cb0f2010-09-06 23:36:31 +0000280
281Sometimes it is useful to be able to reproduce the sequences given by a pseudo
282random number generator. By re-using a seed value, the same sequence should be
283reproducible from run to run as long as multiple threads are not running.
284
285Most of the random module's algorithms and seeding functions are subject to
286change across Python versions, but two aspects are guaranteed not to change:
287
288* If a new seeding method is added, then a backward compatible seeder will be
289 offered.
290
Georg Brandl92849d12016-02-19 08:57:38 +0100291* The generator's :meth:`~Random.random` method will continue to produce the same
Raymond Hettinger435cb0f2010-09-06 23:36:31 +0000292 sequence when the compatible seeder is given the same seed.
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000293
Raymond Hettinger6e353942010-12-04 23:42:12 +0000294.. _random-examples:
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000295
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000296Examples and Recipes
Antoine Pitroue72b5862010-12-12 20:13:31 +0000297--------------------
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000298
Raymond Hettinger3cdf8712010-12-02 05:35:35 +0000299Basic usage::
300
301 >>> random.random() # Random float x, 0.0 <= x < 1.0
302 0.37444887175646646
303
304 >>> random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0
305 1.1800146073117523
306
307 >>> random.randrange(10) # Integer from 0 to 9
308 7
309
310 >>> random.randrange(0, 101, 2) # Even integer from 0 to 100
311 26
312
313 >>> random.choice('abcdefghij') # Single random element
314 'c'
315
316 >>> items = [1, 2, 3, 4, 5, 6, 7]
317 >>> random.shuffle(items)
318 >>> items
319 [7, 3, 2, 5, 6, 4, 1]
320
321 >>> random.sample([1, 2, 3, 4, 5], 3) # Three samples without replacement
322 [4, 1, 5]
323
Sandro Tosi1ee17192012-04-14 16:01:17 +0200324A common task is to make a :func:`random.choice` with weighted probabilities.
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000325
326If the weights are small integer ratios, a simple technique is to build a sample
327population with repeats::
328
329 >>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
330 >>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
Raymond Hettingerf5b7c7b2016-09-05 13:15:02 -0700331 >>> population
332 ['Red', 'Red', 'Red', 'Blue', 'Blue', 'Yellow', 'Green', 'Green', 'Green', 'Green']
333
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000334 >>> random.choice(population)
335 'Green'
336
Raymond Hettinger3cdf8712010-12-02 05:35:35 +0000337A more general approach is to arrange the weights in a cumulative distribution
338with :func:`itertools.accumulate`, and then locate the random value with
339:func:`bisect.bisect`::
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000340
341 >>> choices, weights = zip(*weighted_choices)
342 >>> cumdist = list(itertools.accumulate(weights))
Raymond Hettingerf5b7c7b2016-09-05 13:15:02 -0700343 >>> cumdist # [3, 3+2, 3+2+1, 3+2+1+4]
344 [3, 5, 6, 10]
345
Raymond Hettinger2fdc7b12010-12-02 02:41:33 +0000346 >>> x = random.random() * cumdist[-1]
347 >>> choices[bisect.bisect(cumdist, x)]
348 'Blue'