grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î   fäeã@sdZddlmZddlmZmZddl m Z m Z mZmZmZddl mZmZmZmZddlmZddlm Z!m"Z#ddl$m%Z&d 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(l.Z.Gd)d „d e.j/ƒZ/Gd*d„de/ƒZ0d+d,„Z1d-d.d/„Z2e/ƒZ3e3j4Z4e3j5Z5e3j6Z6e3j7Z7e3j8Z8e3j9Z9e3j:Z:e3j;Z;e3j<Z<e3j=Z=e3j>Z>e3j?Z?e3j@Z@e3jAZAe3jBZBe3jCZCe3jDZDe3jEZEe3jFZFe3jGZGe3jHZHeId0kr{e2ƒnd(S)1a‰Random variable generators. integers -------- uniform within range sequences --------- pick random element pick random sample generate random permutation distributions on the real line: ------------------------------ uniform triangular 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. * The random() method is implemented in C, executes in a single Python step, and is, therefore, threadsafe. é)Úwarn)Ú MethodTypeÚBuiltinMethodType)ÚlogÚexpÚpiÚeÚceil)ÚsqrtÚacosÚcosÚsin)Úurandom)ÚSetÚSequence)Úsha512ÚRandomÚseedÚrandomÚuniformÚrandintÚchoiceÚsampleÚ randrangeÚshuffleÚ normalvariateÚlognormvariateÚ expovariateÚvonmisesvariateÚ gammavariateÚ triangularÚgaussÚ betavariateÚ paretovariateÚweibullvariateÚgetstateÚsetstateÚ getrandbitsÚ SystemRandomégà?g@g@gð?g@é5éNcseZdZdZdZddd„Zdd‡fdd†Z‡fd d †Z‡fd d †Zd d„Z dd„Z dd„Z dde dd„Z dd„Ze de>eeedd„Zdd„Zddd„Zdd„Zd d!„Zd"d#dd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Z d6d7„Z!‡S)8raãRandom number generator base class used by bound module functions. Used to instantiate instances of Random to get generators that don't share state. 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(), and setstate(). Optionally, implement a getrandbits() method so that randrange() can cover arbitrarily large ranges. éNcCs|j|ƒd|_dS)zeInitialize an instance. Optional argument x controls seeding, as for Random.seed(). N)rÚ gauss_next)ÚselfÚx©r0ú+/opt/alt/python34/lib64/python3.4/random.pyÚ__init__Ts zRandom.__init__r+c sï|dkrbytjtdƒdƒ}Wqbtk r^ddl}t|jƒdƒ}YqbXn|dkrÒt|tttfƒrÒt|tƒr¤|j ƒ}n|t |ƒj ƒ7}tj|dƒ}qÒnt ƒj |ƒd|_dS)a‰Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1, the hash() of *a* is used instead. If *a* is an int, all bits are used. NiÄ Úbigrér+)ÚintÚ from_bytesÚ_urandomÚNotImplementedErrorÚtimeÚ isinstanceÚstrÚbytesÚ bytearrayÚencodeÚ_sha512ZdigestÚsuperrr-)r.ÚaÚversionr9)Ú __class__r0r1r]s    z Random.seedcs|jtƒjƒ|jfS)z9Return internal state; can be passed to setstate() later.)ÚVERSIONr@r%r-)r.)rCr0r1r%}szRandom.getstatecs×|d}|dkr;|\}}|_tƒj|ƒn˜|dkrº|\}}|_ytdd„|Dƒƒ}Wn.tk r¦}zt|‚WYdd}~XnXtƒj|ƒntd||jfƒ‚dS)z:Restore internal state from object returned by getstate().rr,r+css|]}|dVqdS)r+é Nlr0)Ú.0r/r0r0r1ú Žsz"Random.setstate..Nz?state with version %s passed to Random.setstate() of version %s)r-r@r&ÚtupleÚ ValueErrorÚ TypeErrorrD)r.ÚstaterBZ internalstater)rCr0r1r&�s   zRandom.setstatecCs |jƒS)N)r%)r.r0r0r1Ú __getstate__ŸszRandom.__getstate__cCs|j|ƒdS)N)r&)r.rKr0r0r1Ú __setstate__¢szRandom.__setstate__cCs|jf|jƒfS)N)rCr%)r.r0r0r1Ú __reduce__¥szRandom.__reduce__éc Cs‚||ƒ}||kr'tdƒ‚n|dkr[|dkrL|j|ƒStdƒ‚n||ƒ}||kr‚tdƒ‚n||}|dkrµ|dkrµ||j|ƒS|dkrÝtd|||fƒ‚n||ƒ}||krtdƒ‚n|dkr%||d|} n-|dkrF||d|} n td ƒ‚| dkrmtdƒ‚n|||j| ƒS) zÀChoose 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. z!non-integer arg 1 for randrange()Nrzempty range for randrange()z non-integer stop for randrange()rOz'empty range for randrange() (%d,%d, %d)z non-integer step for randrange()zzero step for randrange())rIÚ _randbelow) r.ÚstartÚstopÚstepÚ_intZistartZistopÚwidthZistepÚnr0r0r1rªs4               zRandom.randrangecCs|j||dƒS)zJReturn random integer in range [a, b], including both end points. rO)r)r.rAÚbr0r0r1rÖszRandom.randintc Csç|j}|j}||ƒ|ks6||ƒ|krq|jƒ} || ƒ} x| |krl|| ƒ} qQW| S||kr˜tdƒ||ƒ|ƒS||} || |} |ƒ} x| | krÔ|ƒ} q¼W|| |ƒ|S)zCReturn a random int in the range [0,n). Raises ValueError if n==0.z¤Underlying random() generator does not supply enough bits to choose from a population range this large. To remove the range limitation, add a getrandbits() method.)rr'Ú bit_lengthÚ_warn) r.rVr5ÚmaxsizeÚtypeZMethodZ BuiltinMethodrr'ÚkÚrZremÚlimitr0r0r1rPÜs"  $       zRandom._randbelowc CsBy|jt|ƒƒ}Wntk r9tdƒ‚YnX||S)z2Choose a random element from a non-empty sequence.z$Cannot choose from an empty sequence)rPÚlenrIÚ IndexError)r.ÚseqÚir0r0r1rús  z Random.choicecCsÏ|dkrk|j}x³ttdt|ƒƒƒD]3}||dƒ}||||||<||Population must be a sequence or set. For dicts, use list(d).rzSample larger than populationNéér)r,rO)r:Ú_SetrHÚ _SequencerJrPr_rIÚ_ceilÚ_logÚlistrdÚsetÚadd) r.Z populationr\rerVÚresultZsetsizeZpoolrbrfZselectedZ selected_addr0r0r1rs6    $      z Random.samplecCs||||jƒS)zHGet a random number in the range [a, b) or [a, b] depending on rounding.)r)r.rArWr0r0r1rVszRandom.uniformggð?c Cs•|jƒ}y(|dkr!dn||||}Wntk rL|SYnX||kr}d|}d|}||}}n|||||dS)zÜTriangular distribution. Continuous distribution bounded by given lower and upper limits, and having a given mode value in-between. http://en.wikipedia.org/wiki/Triangular_distribution Ngà?gð?)rÚZeroDivisionError)r.ZlowZhighÚmodeÚuÚcr0r0r1r \s (     zRandom.triangularcCsi|j}xQ|ƒ}d|ƒ}t|d|}||d}|t|ƒ kr Pq q W|||S)z\Normal distribution. mu is the mean, and sigma is the standard deviation. gð?gà?g@)rÚ NV_MAGICCONSTrl)r.ÚmuÚsigmarÚu1Úu2ÚzZzzr0r0r1rrs   zRandom.normalvariatecCst|j||ƒƒS)zû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. )Ú_expr)r.rvrwr0r0r1r‹szRandom.lognormvariatecCstd|jƒƒ |S)a^Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called "lambda", but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative. gð?)rlr)r.Zlambdr0r0r1r—szRandom.expovariatecCs|j}|dkr t|ƒSd|}|td||ƒ}xf|ƒ}tt|ƒ}|||}|ƒ} | d||ks£| d|t|ƒkrEPqEqEWd|} | |d| |} |ƒ} | dkr÷|t| ƒt} n|t| ƒt} | S)aFCircular 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ð?)rÚTWOPIÚ_sqrtÚ_cosÚ_pir{Ú_acos)r.rvZkapparÚsr]rxrzÚdryÚqÚfZu3Zthetar0r0r1rªs&      .   zRandom.vonmisesvariatecCs |dks|dkr'tdƒ‚n|j}|dkr td|dƒ}|t}||}x¢|ƒ}d|ko‡dkns’qgnd|ƒ}t|d|ƒ|} |t| ƒ} |||} ||| | } | td| dks| t| ƒkrg| |SqgWné|dkr`|ƒ} x| dkrP|ƒ} q8Wt| ƒ |Sxž|ƒ} t|t}|| }|dkr¡|d|} nt|||ƒ } |ƒ}|dkræ|| |dkrýPqýqc|t| ƒkrcPqcqcW| |SdS) aZGamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0. The probability distribution function is: x ** (alpha - 1) * math.exp(-x / beta) pdf(x) = -------------------------------------- math.gamma(alpha) * beta ** alpha gz*gammavariate: alpha and beta must be > 0.0gð?g@gH¯¼šò×z>gËPÊÿÿï?g@N)rIrr}ÚLOG4rlr{Ú SG_MAGICCONSTÚ_e)r.ÚalphaÚbetarZainvZbbbZcccrxryÚvr/rzr]rsrWÚpr0r0r1rÚsJ      *        zRandom.gammavariatecCsƒ|j}|j}d|_|dkrw|ƒt}tdtd|ƒƒƒ}t|ƒ|}t|ƒ||_n|||S)zØ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. Ng@gð?gÀ)rr-r|r}rlr~Ú_sin)r.rvrwrrzZx2piZg2radr0r0r1r!"s     z Random.gausscCs>|j|dƒ}|dkr"dS|||j|dƒSdS)z�Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. gð?rgN)r)r.rˆr‰Úyr0r0r1r"Ws  zRandom.betavariatecCs d|jƒ}d|d|S)z3Pareto distribution. alpha is the shape parameter.gð?)r)r.rˆrsr0r0r1r#iszRandom.paretovariatecCs'd|jƒ}|t|ƒ d|S)zfWeibull distribution. alpha is the scale parameter and beta is the shape parameter. gð?)rrl)r.rˆr‰rsr0r0r1r$rszRandom.weibullvariate)"Ú__name__Ú __module__Ú __qualname__Ú__doc__rDr2rr%r&rLrMrNr5rrÚBPFr[Ú _MethodTypeÚ_BuiltinMethodTyperPrrrrr rrrrrr!r"r#r$r0r0)rCr1rDs6      ,    >    0 H 5  c@sPeZdZdZdd„Zdd„Zdd„Zdd „ZeZZ d S) r(zÝ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). cCstjtdƒdƒd?tS)z3Get the next random number in the range [0.0, 1.0).ér3r,)r5r6r7Ú RECIP_BPF)r.r0r0r1r‡szSystemRandom.randomcCsr|dkrtdƒ‚n|t|ƒkr<tdƒ‚n|dd}tjt|ƒdƒ}||d|?S)z:getrandbits(k) -> x. Generates an int with k random bits.rz(number of bits must be greater than zeroz#number of bits should be an integerr•ér3)rIr5rJr6r7)r.r\Znumbytesr/r0r0r1r'‹s zSystemRandom.getrandbitscOsdS)z%sd("         ÿÿ=!