• Home
  • Raw
  • Download

Lines Matching +full:is +full:- +full:generator +full:- +full:function

1 :mod:`random` --- Generate pseudo-random numbers
5 :synopsis: Generate pseudo-random numbers with various common distributions.
9 --------------
11 This module implements pseudo-random number generators for various
15 of a random element, a function to generate a random permutation of a list
16 in-place, and a function for random sampling without replacement.
20 distributions of angles, the von Mises distribution is available.
22 Almost all module functions depend on the basic function :func:`.random`, which
23 generates a random float uniformly in the semi-open range [0.0, 1.0). Python
24 uses the Mersenne Twister as the core generator. It produces 53-bit precision
25 floats and has a period of 2\*\*19937-1. The underlying implementation in C is
26 both fast and threadsafe. The Mersenne Twister is one of the most extensively
28 deterministic, it is not suitable for all purposes, and is completely unsuitable
33 instances of :class:`Random` to get generators that don't share state. This is
34 especially useful for multi-threaded programs, creating a different instance of
39 basic generator of your own devising: in that case, override the :meth:`~Random.random`,
41 :meth:`~Random.jumpahead` methods. Optionally, a new generator can supply a
42 :meth:`~Random.getrandbits` method --- this
49 :class:`WichmannHill` class that implements an alternative generator in pure
51 earlier versions of Python, which used the Wichmann-Hill algorithm as the core
52 generator. Note that this Wichmann-Hill generator can no longer be recommended:
53 its period is too short by contemporary standards, and the sequence generated is
58 MersenneTwister replaced Wichmann-Hill as the default generator.
61 uses the system function :func:`os.urandom` to generate random numbers
66 The pseudo-random generators of this module should not be used for
68 you require a cryptographically secure pseudo-random number generator.
74 .. function:: seed(a=None)
76 Initialize internal state of the random number generator.
80 function for details on availability).
82 If *a* is not ``None`` or an :class:`int` or a :class:`long`, then
83 ``hash(a)`` is used instead. Note that the hash values for some types
84 are nondeterministic when :envvar:`PYTHONHASHSEED` is enabled.
89 .. function:: getstate()
91 Return an object capturing the current internal state of the generator. This
100 .. function:: setstate(state)
103 :func:`setstate` restores the internal state of the generator to what it was at
109 .. function:: jumpahead(n)
112 current state. *n* is a non-negative integer which is used to scramble the
113 current state vector. This is most useful in multi-threaded programs, in
126 .. function:: getrandbits(k)
128 Returns a python :class:`long` int with *k* random bits. This method is supplied
129 with the MersenneTwister generator and some other generators may also provide it
138 .. function:: randrange(stop)
141 Return a randomly selected element from ``range(start, stop, step)``. This is
148 .. function:: randint(a, b)
155 .. function:: choice(seq)
157 Return a random element from the non-empty sequence *seq*. If *seq* is empty,
161 .. function:: shuffle(x[, random])
163 Shuffle the sequence *x* in place. The optional argument *random* is a
164 0-argument function returning a random float in [0.0, 1.0); by default, this is
165 the function :func:`.random`.
168 *x* is larger than the period of most random number generators; this implies
172 .. function:: sample(population, k)
180 original population unchanged. The resulting list is in selection order so that
181 all sub-slices will also be valid random samples. This allows raffle winners
186 contains repeats, then each occurrence is a possible selection in the sample.
189 argument. This is especially fast and space efficient for sampling from a large
192 The following functions generate specific real-valued distributions. Function
198 .. function:: random()
203 .. function:: uniform(a, b)
208 The end-point value ``b`` may or may not be included in the range
209 depending on floating-point rounding in the equation ``a + (b-a) * random()``.
212 .. function:: triangular(low, high, mode)
222 .. function:: betavariate(alpha, beta)
228 .. function:: expovariate(lambd)
230 Exponential distribution. *lambd* is 1.0 divided by the desired
232 "lambda", but that is a reserved word in Python.) Returned values
233 range from 0 to positive infinity if *lambd* is positive, and from
234 negative infinity to 0 if *lambd* is negative.
237 .. function:: gammavariate(alpha, beta)
239 Gamma distribution. (*Not* the gamma function!) Conditions on the
242 The probability distribution function is::
244 x ** (alpha - 1) * math.exp(-x / beta)
245 pdf(x) = --------------------------------------
249 .. function:: gauss(mu, sigma)
251 Gaussian distribution. *mu* is the mean, and *sigma* is the standard
252 deviation. This is slightly faster than the :func:`normalvariate` function
256 .. function:: lognormvariate(mu, sigma)
264 .. function:: normalvariate(mu, sigma)
266 Normal distribution. *mu* is the mean, and *sigma* is the standard deviation.
269 .. function:: vonmisesvariate(mu, kappa)
271 *mu* is the mean angle, expressed in radians between 0 and 2\*\ *pi*, and *kappa*
272 is the concentration parameter, which must be greater than or equal to zero. If
273 *kappa* is equal to zero, this distribution reduces to a uniform random angle
277 .. function:: paretovariate(alpha)
279 Pareto distribution. *alpha* is the shape parameter.
282 .. function:: weibullvariate(alpha, beta)
284 Weibull distribution. *alpha* is the scale parameter and *beta* is the shape
292 Class that implements the Wichmann-Hill algorithm as the core generator. Has all
294 below. Because this class is implemented in pure Python, it is not threadsafe
295 and may require locks between calls. The period of the generator is
296 6,953,607,871,644 which is small enough to require care that two independent
300 .. function:: whseed([x])
302 This is obsolete, supplied for bit-level compatibility with versions of Python
310 Class that uses the :func:`os.urandom` function for generating random numbers
344 M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-dimensionally
345 equidistributed uniform pseudorandom number generator", ACM Transactions on
346 Modeling and Computer Simulation Vol. 8, No. 1, January pp.3--30 1998.
349 pseudo-random number generator", Applied Statistics 31 (1982) 188-190.
351 `Complementary-Multiply-with-Carry recipe
353 random number generator with a long period and comparatively simple update