mò %U²Ic@scdZdklZdklZlZdk l Z l Z lZlZdk lZlZlZlZdklZdklZddd d d d d dddddddddddddddddgZ de d ƒed!ƒZ!d!eZ"e d"ƒZ#d#e d$ƒZ$d%Z%d&e% Z&d'k'Z'de'i(fd(„ƒYZ(de(fd)„ƒYZ)de(fd*„ƒYZ*d+„Z+d,d-„Z,e(ƒZ-e-i.Z.e-i/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-iBZBeCd.jo e,ƒnd'S(/s: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. (swarn(s MethodTypesBuiltinMethodType(slogsexpspite(ssqrtsacosscosssin(surandom(shexlifytRandomtseedtrandomtuniformtrandinttchoicetsamplet randrangetshufflet normalvariatetlognormvariatet expovariatetvonmisesvariatet gammavariatetgausst betavariatet paretovariatetweibullvariatetgetstatetsetstatet jumpaheadt WichmannHillt getrandbitst SystemRandomif0.5f2.0f4.0f1.0f4.5i5iNcBstZdZdZed„Zed„Zd„Zd„Zd„Z d„Z d„Z ed e ed e >d „Zd „Zee d e >eed „Zd„Zee 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(tselfRtxtNonet gauss_next(RR((t#/data/zmath/lib/python2.4/random.pyt__init__Xs cCsˆ|djoXytttdƒƒdƒ}Wqetj o&dk}t|iƒdƒ}qeXntt|ƒ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. iNi( taRtlongt_hexlifyt_urandomtNotImplementedErrorttimetsuperRRRR(RRR$((RRas  cCs"|itt|ƒiƒ|ifS(s9Return internal state; can be passed to setstate() later.N(RtVERSIONR%RRR(R((RRtscCs`|d}|djo,|\}}|_tt|ƒi|ƒntd||i fƒ‚dS(s:Restore internal state from object returned by getstate().iis?state with version %s passed to Random.setstate() of version %sN( tstatetversiont internalstateRRR%RRt ValueErrorR&(RR'R(R)((RRxs   cCs |iƒS(N(RR(R((Rt __getstate__ˆscCs|i|ƒdS(N(RRR'(RR'((Rt __setstate__‹scCs|if|iƒfS(N(Rt __class__R(R((Rt __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()N(tinttstarttistartR*tstoptdefaulttmaxwidthRt _randbelowRtistoptwidthtsteptisteptn( RR0R2R8R/R3R4R1R7R:R6R9((RR“sB            "          cCs|i||dƒS(sJReturn random integer in range [a, b], including both end points. iN(RRRtb(RRR;((RRÓsc CsÍy |i} Wntj on}Xt|iƒ|jpt| ƒ|joR|d||ddƒƒ}| |ƒ}x||jo| |ƒ}qxW|Sn||jot dƒ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. f1.0000100000000001if2.0sgUnderlying random() generator does not supply enough bits to choose from a population range this largeN(RRtAttributeErrorttypeRt_BuiltinMethodt_MethodR/t_logR:tktrt _maxwidtht_warn( RR:R@R/RCR?R>RBRAR((RR5Ùs )   cCs|t|iƒt|ƒƒS(s2Choose a random element from a non-empty sequence.N(tseqR/RRtlen(RRE((RRö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( RRRtreversedtxrangeRFRtiR/tj(RRRR/RJRI((RR ús  c Cs•t|ƒ} d|jo | jnp td‚n|i}t}dg|}| d|jpt |dƒobt |ƒ}xt|ƒD]A}||ƒ| |ƒ} || ||<|| |d|| 0 and beta > 0. f0.0s*gammavariate: alpha and beta must be > 0.0f1.0f2.0if9.9999999999999995e-08f0.99999990000000005f4.5N(talphatbetaR*RRRctainvtLOG4tbbbtcccRWRXR@tvR^RRZRBt SG_MAGICCONSTR_t_eR;tp(RRkRlRRmRWRoRXR;RtRBR_RqRRZRp((RR¾sR       ,          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. f-2.0f1.0N(RRRRZRRbtx2piRcR@tg2radRdt_sinR\R](RR\R]RRuRZRv((RRs     cCsC|i|dƒ}|djodSn|||i|dƒSdS(sBeta distribution. Conditions on the parameters are alpha > -1 and beta} > -1. Returned values range between 0 and 1. f1.0if0.0N(RRRktyRl(RRkRlRx((RR5s  cCs%d|iƒ}dt|d|ƒS(s3Pareto distribution. alpha is the shape parameter.f1.0N(RRR_tpowRk(RRkR_((RRGscCs,d|iƒ}|tt|ƒ d|ƒS(sfWeibull distribution. alpha is the scale parameter and beta is the shape parameter. f1.0N(RRR_RkRyR@Rl(RRkRlR_((RRPs(!t__name__t __module__t__doc__R&RRRRRR+R,R.R/tBPFRRR@t _MethodTypet_BuiltinMethodTypeR5RR RRR R R R RRRRR(((RREs2       @   F    2 B 5  cBs\tZdZdd„Zd„Zd„Zd„Zd„Zdddd„Z dd „Z RS( NicCs|djoXytttdƒƒdƒ}Wqetj o&dk}t|iƒdƒ}qeXnt|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). iNii= 0i«i=vi¬icviªisvN( R:R*RR‚RRxRZR/Ry(RR:RxRRZ((RR­s     icCsŽt|ƒt|ƒjot|ƒjo tjnptdƒ‚nd|jo djno9d|jo djnod|jo djnptdƒ‚nd|jo|jo |jnozdk}t|iƒdƒ}t|d@|d?Aƒ}t |dƒ\}}t |dƒ\}}t |dƒ\}}n|pd|pd|pdf|_ 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)Niÿÿÿii(R=RRxRZR/RSR*R$R ttRRR‚RR(RRRxRZRƒR$((Rt__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( RRRt_WichmannHill__whseedR€RRRxRZ(RRRxRRZ((RtwhseedÚs    ( RzR{R&RRRRRRR…R†(((RR]s    cBsFtZdZd„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).iiiN(R R!R"t RECIP_BPF(R((RRû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 integeriiiN( RAR*R/RStbytesR R!R"R(RRARˆR((RRÿs cOsdS(s<Stub method. Not used for a system random number generator.N(R(Rtargstkwds((Rt_stub scOstdƒ‚dS(sAMethod should not be called for a system random number generator.s*System entropy source does not have state.N(R#(RR‰RŠ((Rt_notimplementeds( RzR{R|RRR‹RRRŒRR(((RRós     cCsòdk} |GdG|iGHd} d}d}d}| iƒ}xVt |ƒD]H}||Œ} | | 7} || | }t | |ƒ}t| |ƒ}qJW| iƒ} t| |dƒGdG| |}t||||ƒ} d|| ||fGHdS(Nttimesf0.0f 10000000000.0f-10000000000.0issec,s!avg %g, stddev %g, min %g, max %g(R$R:tfuncRzttotaltsqsumtsmallesttlargesttt0trangeRIR‰Rtmintmaxtt1troundtavgRctstddev(R:RŽR‰R™R’RIR“R‘RR$RRR—Rš((Rt_test_generators&       iÐcCsôt|tfƒ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(Nf0.0f1.0f0.01f0.10000000000000001f2.0f0.5f0.90000000000000002f20.0f200.0f3.0(f0.0f1.0(f0.0f1.0(f0.0f1.0(f0.01f1.0(f0.10000000000000001f1.0(f0.10000000000000001f2.0(f0.5f1.0(f0.90000000000000002f1.0(f1.0f1.0(f2.0f1.0(f20.0f1.0(f200.0f1.0(f0.0f1.0(f3.0f3.0( R›tNRR R R RRR(Rœ((Rt_test+st__main__(DR|twarningstwarnRDttypest MethodTypeR~tBuiltinMethodTypeRtmathtlogR@texpR^tpiReRRstsqrtRctacosRitcosRdtsinRwtosturandomR"tbinasciithexlifyR!t__all__RYRbRnRrR}R‡t_randomRRRR›Rt_instRRRRRRRR R R R R RRRRRRRRRRz(0RRwR^RR}RiR±RRRR RRRRR R RbR°RR ReRRRRsRRR RRdR"RRRRR@RR!R‡R²R›RrRDR~RnRcRY((Rt?'sZ   N    ÿÿ–"