³ò îFc@s}dZddklZddklZlZddk l Z l Z lZlZlZddk lZlZlZlZddklZddkl Z!dd d d d d ddddddddddddddddddgZ"d e d!ƒed"ƒZ#d"eZ$e d#ƒZ%d$e d%ƒ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/jo e.ƒ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 normalvariatetlognormvariatet expovariatetvonmisesvariatet gammavariatetgausst betavariatet paretovariatetweibullvariatetgetstatetsetstatet jumpaheadt WichmannHillt getrandbitst SystemRandomigà¿g@g@gð?g@i5iNcBseZdZdZdd„Zdd„Zd„Zd„Zd„Z d„Z d„Z dd e dd e >d „Zd „Zee d e >eed „Zd„Zde d„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 random.pyct__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 random.pycRas  cCs"|itt|ƒiƒ|ifS(s9Return internal state; can be passed to setstate() later.(tVERSIONR0RR R'(R(((s random.pycR 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 random.pycR!xs   cCs |iƒS(N(R (R(((s random.pyct __getstate__ˆscCs|i|ƒdS(N(R!(R(R4((s random.pyct __setstate__‹scCs|id|iƒfS(N((t __class__R (R(((s random.pyct __reduce__Žsilc Cs||ƒ}||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 random.pycR“s@            "          cCs|i||dƒS(sJReturn random integer in range [a, b], including both end points. i(R(R(R1tb((s random.pycRÓ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ÄZ| ð?ig@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 random.pycR;Ùs )   cCs|t|iƒt|ƒƒS(s2Choose a random element from a non-empty sequence.(R?Rtlen(R(tseq((s random.pycRöscCsx|djo |i}nxWttdt|ƒƒƒD]:}||ƒ|dƒ}||||||<|| 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 random.pycRú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|| (RRK(R(tlambdRtu((s random.pycR}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@gà?(RtTWOPIt_sqrtt_cost_piRot_acos(R(RitkappaRR1RGRPRkRmtftcRltu3ttheta((s random.pycRs&    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ð?g@gH¯¼šò×z>gËPÊÿÿï?g@N(R3RRstLOG4RKRot SG_MAGICCONSTt_e(R(talphatbetaRtainvtbbbtcccRkRltvR)RmRPRqRGtp((s random.pycRÂsL        ,         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Àgð?N(RR'R&RrRsRKRtt_sin(R(RiRjRRmtx2pitg2rad((s random.pycRs     cCsC|i|dƒ}|djodSn|||i|dƒSdS(sBeta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. gð?igN(R(R(RR€ty((s random.pycR9s  cCs%d|iƒ}dt|d|ƒS(s3Pareto distribution. alpha is the shape parameter.gð?(Rtpow(R(RRq((s random.pycRKscCs,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 random.pycRTsN(!t__name__t __module__t__doc__R2R&R*RR R!R7R8R:R?tBPFRRRKt _MethodTypet_BuiltinMethodTypeR;RRRRRRRRRRRRR(((s random.pycREs6       ?    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= 0i«i=vi¬icviªisvN(R3R“R?RŠ(R(RFR)R‰Rm((s random.pycR"±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 random.pyct__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 random.pyctwhseedÞs   N( R‹RŒR2R&RRR R!R"R–R—(((s random.pycR#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 random.pycRÿ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 random.pycR$s cOsdS(s<Stub method. Not used for a system random number generator.N(R&(R(targstkwds((s random.pyct_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 random.pyct_notimplementeds( R‹RŒRRR$RœRR"RR R!(((s random.pycR%÷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 _ Âissec,s!avg %g, stddev %g, min %g, max %g(R/R‹trangetmintmaxtroundRs(RFtfuncRšR/ttotaltsqsumtsmallesttlargesttt0RUR)tt1tavgtstddev((s random.pyct_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áz„?gš™™™™™¹?g@gà?gÍÌÌÌÌÌì?g4@gi@g@((ggð?(ggð?(ggð?(g{®Gáz„?gð?(gš™™™™™¹?gð?(gš™™™™™¹?g@(gà?gð?(gÍÌÌÌÌÌì?gð?(gð?gð?(g@gð?(g4@gð?(gi@gð?(ggð?(g@g@(R¬RRRRRRR(tN((s random.pyct_test/st__main__(FRtwarningsRRJttypesRRRRtmathRRKRRoRRuRR~RRXRRsR RvR RtR R†tosR R-tbinasciiR R,t__all__RhRrR|R}RŽR˜t_randomRR#R%R¬R®t_instRRRRRRRRRRRRRRRRRR R!R"R$R‹(((s random.pycs'sd("        ÿÿ–"