�
��c@s}dZddklZddklZlZddk l
ZlZ
lZlZlZddk lZlZlZlZddklZddkl Z!dd d
ddd
ddddddddddddddddddgZ"d e
d!�ed"�Z#d"eZ$ed#�Z%d$ed%�Z&d&Z'd'e'Z(dd(k)Z)de)i*fd)��YZ*de*fd*��YZ+de*fd+��YZ,d,�Z-d-d.�Z.e*�Z/e/i0Z0e/i1Z1e/i2Z2e/i3Z3e/i4Z4e/i5Z5e/i6Z6e/i7Z7e/i8Z8e/i9Z9e/i:Z:e/i;Z;e/i<Z<e/i=Z=e/i>Z>e/i?Z?e/i@Z@e/iAZAe/iBZBe/iCZCe/iDZDeEd/joe.�nd(S(0s:Random variable generators.
integers
--------
uniform within range
sequences
---------
pick random element
pick random sample
generate random permutation
distributions on the real line:
------------------------------
uniform
normal (Gaussian)
lognormal
negative exponential
gamma
beta
pareto
Weibull
distributions on the circle (angles 0 to 2pi)
---------------------------------------------
circular uniform
von Mises
General notes on the underlying Mersenne Twister core generator:
* The period is 2**19937-1.
* It is one of the most extensively tested generators in existence.
* Without a direct way to compute N steps forward, the semantics of
jumpahead(n) are weakened to simply jump to another distant state and rely
on the large period to avoid overlapping sequences.
* The random() method is implemented in C, executes in a single Python step,
and is, therefore, threadsafe.
i�(twarn(t
MethodTypetBuiltinMethodType(tlogtexptpitetceil(tsqrttacostcostsin(turandom(thexlifytRandomtseedtrandomtuniformtrandinttchoicetsamplet randrangetshufflet
normalvariatetlognormvariatetexpovariatetvonmisesvariatetgammavariatetgausstbetavariatet
paretovariatetweibullvariatetgetstatetsetstatet jumpaheadtWichmannHilltgetrandbitstSystemRandomig�@g@g�@i5iNcBseZdZdZdd�Zdd�Zd�Zd�Zd�Z d�Z
d�Zdd edd
e
>d�Zd�Zeed
e
>eed
�Zd�Zded�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z RS(s�Random number generator base class used by bound module functions.
Used to instantiate instances of Random to get generators that don't
share state. Especially useful for multi-threaded programs, creating
a different instance of Random for each thread, and using the jumpahead()
method to ensure that the generated sequences seen by each thread don't
overlap.
Class Random can also be subclassed if you want to use a different basic
generator of your own devising: in that case, override the following
methods: random(), seed(), getstate(), setstate() and jumpahead().
Optionally, implement a getrandombits() method so that randrange()
can cover arbitrarily large ranges.
icCs|i|�d|_dS(seInitialize an instance.
Optional argument x controls seeding, as for Random.seed().
N(RtNonet
gauss_next(tselftx((s/sys/lib/python/random.pyt__init__Xs
cCs�|djo[ytttd��d�}Wqhtj
o)ddk}t|i�d�}qhXntt|�i|�d|_ dS(sInitialize internal state from hashable object.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
If a is not None or an int or long, hash(a) is used instead.
ii�Ni(
R&tlongt_hexlifyt_urandomtNotImplementedErrorttimetsuperRRR'(R(taR/((s/sys/lib/python/random.pyRas
cCs"|itt|�i�|ifS(s9Return internal state; can be passed to setstate() later.(tVERSIONR0RR R'(R(((s/sys/lib/python/random.pyR tscCs`|d}|djo,|\}}|_tt|�i|�ntd||if��dS(s:Restore internal state from object returned by getstate().iis?state with version %s passed to Random.setstate() of version %sN(R'R0RR!t
ValueErrorR2(R(tstatetversiont
internalstate((s/sys/lib/python/random.pyR!xs
cCs
|i�S(N(R (R(((s/sys/lib/python/random.pyt__getstate__�scCs|i|�dS(N(R!(R(R4((s/sys/lib/python/random.pyt__setstate__�scCs|id|i�fS(N((t __class__R (R(((s/sys/lib/python/random.pyt
__reduce__�silcCs||�}||jo
td�n||joP|djo6||jo|i|�Sn||i�|�Sntd�n||�}||jo
td�n||} |djoW| djoJ| |jo|||i| ��Sn||||i�| ��Sn|djotd||| f�n||�}
|
|jo
td�n|
djo| |
d|
}n-|
djo| |
d|
}n
td�|djo
td�n||jo||
|i|�Sn||
||i�|�S( sChoose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
Do not supply the 'int', 'default', and 'maxwidth' arguments.
s!non-integer arg 1 for randrange()isempty range for randrange()s non-integer stop for randrange()is'empty range for randrange() (%d,%d, %d)s non-integer step for randrange()szero step for randrange()(R3t
_randbelowR(R(tstarttstoptsteptinttdefaulttmaxwidthtistarttistoptwidthtisteptn((s/sys/lib/python/random.pyR�s@
"
cCs|i||d�S(sJReturn random integer in range [a, b], including both end points.
i(R(R(R1tb((s/sys/lib/python/random.pyR�sc
Cs�y
|i}Wntj
on}Xt|i�|jpt|�|joR|d||dd��}||�} x| |jo||�} qxW| Sn||jotd�n||i�|�S(s�Return a random int in the range [0,n)
Handles the case where n has more bits than returned
by a single call to the underlying generator.
gr�|
�g@sgUnderlying random() generator does not supply
enough bits to choose from a population range this large(R$tAttributeErrorttypeRt_warn(
R(RFt_logR?t _maxwidtht_Methodt_BuiltinMethodR$tktr((s/sys/lib/python/random.pyR;�s
)
cCs|t|i�t|��S(s2Choose a random element from a non-empty sequence.(R?Rtlen(R(tseq((s/sys/lib/python/random.pyR�scCsx|djo
|i}nxWttdt|���D]:}||�|d�}||||||<||<q6WdS(s�x, random=random.random -> shuffle list x in place; return None.
Optional arg random is a 0-argument function returning a random
float in [0.0, 1.0); by default, the standard random.random.
iN(R&RtreversedtxrangeRQ(R(R)RR?titj((s/sys/lib/python/random.pyR�s
c
Cs�t|�}d|jo
|jnp
td�n|i}t}d g|}d}|djo%|dtt|dd��7}n||jpt|d�obt|�}xt |�D]A} ||�|| �}
||
|| <||| d||
<q�Wn�y�t
�}|i}xgt |�D]Y} ||�|�}
x%|
|jo||�|�}
qHW||
�||
|| <q,WWnFtt
fj
o4t|t�o�n|it|�|�SnX|S(
s8Chooses k unique random elements from a population sequence.
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).
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.
To choose a sample in a range of integers, use xrange as an argument.
This is especially fast and space efficient for sampling from a
large population: sample(xrange(10000000), 60)
issample larger than populationiiiitkeysiN(RQR3RR?R&t_ceilRKthasattrtlistRTtsettaddt TypeErrortKeyErrort
isinstanceRttuple(
R(t
populationRORFRt_inttresulttsetsizetpoolRURVtselectedtselected_add((s/sys/lib/python/random.pyRs>%
%
cCs||||i�S(s(Get a random number in the range [a, b).(R(R(R1RG((s/sys/lib/python/random.pyRRscCsj|i}xR|�}d|�}t|d|}||d}|t|�joPqq|||S(s\Normal distribution.
mu is the mean, and sigma is the standard deviation.
g��@(Rt
NV_MAGICCONSTRK(R(tmutsigmaRtu1tu2tztzz((s/sys/lib/python/random.pyRXs
cCst|i||��S(s�Log normal distribution.
If you take the natural logarithm of this distribution, you'll get a
normal distribution with mean mu and standard deviation sigma.
mu can have any value, and sigma must be greater than zero.
(t_expR(R(RiRj((s/sys/lib/python/random.pyRqscCs?|i}|�}x|djo
|�}qWt|�|S(s�Exponential distribution.
lambd 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.
gH���>(RRK(R(tlambdRtu((s/sys/lib/python/random.pyR}s
cCs:|i}|djot|�Sndtdd||�}|td|�d|}d||d|}x~|�}tt|�}d||||} ||| }
|�}||
d|
jp||
td|
�joPqvqv|�}|djo|tt| �}
n|tt| �}
|
S(sFCircular data distribution.
mu is the mean angle, expressed in radians between 0 and 2*pi, and
kappa is the concentration parameter, which must be greater than or
equal to zero. If kappa is equal to zero, this distribution reduces
to a uniform random angle over the range 0 to 2*pi.
g���g�@g@g�RtTWOPIt_sqrtt_cost_piRot_acos(R(RitkappaRR1RGRPRkRmtftcRltu3ttheta((s/sys/lib/python/random.pyR�s&
0
cCs%|djp
|djo
td�n|i}|djo�td|d�}|t}||}x�|�}d|jo
djnpqhnd|�}t|d|�|} |t| �}
|||}||| |
}|td|djp|t|�jo|
|Sqhqhn�djo:|�}
x|
djo
|�}
qCWt|
�|Sn�x�|�}
t|t}||
}|djo|d|}
nt|||�}
|�}|djo||
|djoPqqt|t|
�joPqtqt|
|SdS( sxGamma distribution. Not the gamma function!
Conditions on the parameters are alpha > 0 and beta > 0.
gs*gammavariate: alpha and beta must be > 0.0g�@gH���>g�P��@N(R3RRstLOG4RKRot
SG_MAGICCONSTt_e(R(talphatbetaRtainvtbbbtcccRkRltvR)RmRPRqRGtp((s/sys/lib/python/random.pyR�sJ
,
cCs�|i}|i}d|_|djoQ|�t}tdtd|���}t|�|}t|�||_n|||S(s�Gaussian distribution.
mu is the mean, and sigma is the standard deviation. This is
slightly faster than the normalvariate() function.
Not thread-safe without a lock around calls.
g��RR'R&RrRsRKRtt_sin(R(RiRjRRmtx2pitg2rad((s/sys/lib/python/random.pyRs
cCsC|i|d�}|djodSn|||i|d�SdS(s�Beta distribution.
Conditions on the parameters are alpha > 0 and beta > 0.
Returned values range between 0 and 1.
g�gN(R(R(RR�ty((s/sys/lib/python/random.pyR9s
cCs%d|i�}dt|d|�S(s3Pareto distribution. alpha is the shape parameter.g�Rtpow(R(RRq((s/sys/lib/python/random.pyRKscCs,d|i�}|tt|�d|�S(sfWeibull distribution.
alpha is the scale parameter and beta is the shape parameter.
g�RR�RK(R(RR�Rq((s/sys/lib/python/random.pyRTsN(!t__name__t
__module__t__doc__R2R&R*RR R!R7R8R:R?tBPFRRRKt_MethodTypet_BuiltinMethodTypeR;RRRRRRRRRRRRR(((s/sys/lib/python/random.pyREs6 ?
J 2 B 5 cBs\eZdZd d�Zd�Zd�Zd�Zd�Zdddd�Z d d�Z
RS(
icCs
|djo[ytttd��d�}Wqhtj
o)ddk}t|i�d�}qhXnt|ttf�pt|�}nt |d�\}}t |d�\}}t |d�\}}t|�dt|�dt|�df|_
d|_dS( s�Initialize internal state from hashable object.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
If a is not None or an int or long, hash(a) is used instead.
If a is an int or long, a is used directly. Distinct values between
0 and 27814431486575L inclusive are guaranteed to yield distinct
internal states (this guarantee is specific to the default
Wichmann-Hill generator).
ii�Nii<vibvirvi(R&R+R,R-R.R/R_R?thashtdivmodt_seedR'(R(R1R/R)R�Rm((s/sys/lib/python/random.pyRes
0cCsj|i\}}}d|d}d|d}d|d}|||f|_|d|d|d d
S(s3Get the next random number in the range [0.0, 1.0).i�i=vi�icvi�isvg@�@g��g��g�R�(R(R)R�Rm((s/sys/lib/python/random.pyR�scCs|i|i|ifS(s9Return internal state; can be passed to setstate() later.(R2R�R'(R(((s/sys/lib/python/random.pyR �scCsM|d}|djo|\}|_|_ntd||if��dS(s:Restore internal state from object returned by getstate().iis?state with version %s passed to Random.setstate() of version %sN(R�R'R3R2(R(R4R5((s/sys/lib/python/random.pyR!�s
cCs�|djptd��n|i\}}}t|td|d��d}t|td|d��d}t|td|d��d}|||f|_d S(
s�Act as if n calls to random() were made, but quickly.
n is an int, greater than or equal to 0.
Example use: If you have 2 threads and know that each will
consume no more than a million random numbers, create two Random
objects r1 and r2, then do
r2.setstate(r1.getstate())
r2.jumpahead(1000000)
Then r1 and r2 will use guaranteed-disjoint segments of the full
period.
isn must be >= 0i�i=vi�icvi�isvN(R3R�R?R�(R(RFR)R�Rm((s/sys/lib/python/random.pyR"�s
icCs�t|�t|�jot|�jo
tjnptd��nd|jo
djno9d|jo
djnod|jo
djnptd��nd|jo|jo
|jno}ddk}t|i�d�}t|d@|d?A�}t|d�\}}t|d�\}}t|d�\}}n|pd |pd |pd f|_d|_ dS(
sjSet the Wichmann-Hill seed from (x, y, z).
These must be integers in the range [0, 256).
sseeds must be integersiisseeds must be in range(0, 256)i�Ni�ii(
RIR?R]R3R/R+R�R�R&R'(R(R)R�RmR/tt((s/sys/lib/python/random.pyt__whseed�s<Z*'cCs�|djo|i�dSnt|�}t|d�\}}t|d�\}}t|d�\}}||dpd}||dpd}||dpd}|i|||�dS(sbSeed from hashable object's hash code.
None or no argument seeds from current time. It is not guaranteed
that objects with distinct hash codes lead to distinct internal
states.
This is obsolete, provided for compatibility with the seed routine
used prior to Python 2.1. Use the .seed() method instead.
Nii(R&t_WichmannHill__whseedR�R�(R(R1R)R�Rm((s/sys/lib/python/random.pytwhseed�s
N(R�R�R2R&RRR R!R"R�R�(((s/sys/lib/python/random.pyR#as
cBsFeZdZd�Zd�Zd�ZeZZd�ZeZ Z
RS(s�Alternate random number generator using sources provided
by the operating system (such as /dev/urandom on Unix or
CryptGenRandom on Windows).
Not available on all systems (see os.urandom() for details).
cCs!tttd��d�d?tS(s3Get the next random number in the range [0.0, 1.0).iii(R+R,R-t RECIP_BPF(R(((s/sys/lib/python/random.pyR�scCsy|djotd��n|t|�jotd��n|dd}ttt|��d�}||d|?S(s>getrandbits(k) -> x. Generates a long int with k random bits.is(number of bits must be greater than zeros#number of bits should be an integeriii(R3R?R]R+R,R-(R(ROtbytesR)((s/sys/lib/python/random.pyR$s
cOsdS(s<Stub method. Not used for a system random number generator.N(R&(R(targstkwds((s/sys/lib/python/random.pyt_stub
scOstd��dS(sAMethod should not be called for a system random number generator.s*System entropy source does not have state.N(R.(R(R�R�((s/sys/lib/python/random.pyt_notimplementeds(R�R�R�RR$R�RR"R�R R!(((s/sys/lib/python/random.pyR%�s
cCs�ddk}|GdG|iGHd}d}d}d}|i�}xVt|�D]H} ||�}
||
7}||
|
}t|
|�}t|
|�}qMW|i�}t||d�GdG||}t||||�}
d||
||fGHdS( Ni�ttimesgg _�Bg _��ssec,s!avg %g, stddev %g, min %g, max %g(R/R�trangetmintmaxtroundRs(RFtfuncR�R/ttotaltsqsumtsmallesttlargesttt0RUR)tt1tavgtstddev((s/sys/lib/python/random.pyt_test_generators(
i�cCs�t|td�t|td�t|td
�t|td�t|td�t|td�t|td�t|td�t|td�t|td�t|td�t|td�t|td�t|td�t|td�dS(Ngg���?g����g@g�����4@gi@g@((gg�gg�gg�g{��?g�g����g�g����g@(g��g�����g��g@g�g4@g�gi@g�gg�g@g@(R�RRRRRRR(tN((s/sys/lib/python/random.pyt_test/st__main__(FR�twarningsRRJttypesRR�RR�tmathRRKRRoRRuRR~RRXRRsR RvR
RtRR�tosRR-tbinasciiR
R,t__all__RhRrR|R}R�R�t_randomRR#R%R�R�t_instRRRRRRRRRRRRRRRRRR R!R"R$R�(((s/sys/lib/python/random.pys<module>'sd("
��"
|