grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026B ül6mm £Kã@súdZddlmZddlmZmZddl m Z m Z mZmZmZddl mZmZmZmZddlmZddlm Z!m"Z#ddl$m%Z&dd l'Z(dd l)Z*dd lZ+d d d d ddddddddddddddddddd d!gZ,d"e d#ƒed$ƒZ-d$eZ.e d%ƒZ/d&e d'ƒZ0d(Z1d)e1 Z2dd l3Z3Gd*d „d e3j4ƒZ4Gd+d!„d!e4ƒZ5d,d-„Z6d4d/d0„Z7e4ƒZ8e8j9Z9e8j:Z:e8j;Z;e8jZ>e8j?Z?e8j@Z@e8jAZAe8jBZBe8jCZCe8jDZDe8jEZEe8jFZFe8jGZGe8jHZHe8jIZIe8jJZJe8jKZKe8jLZLe8jMZMe8jNZNeOe+d1ƒ�ræe+jPe8j9d2�eQd3k�röe7ƒd S)5a°Random variable generators. integers -------- uniform within range sequences --------- pick random element pick random sample pick weighted 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)Úsha512NÚRandomÚseedÚrandomÚuniformÚrandintÚchoiceÚsampleÚ randrangeÚshuffleÚ normalvariateÚlognormvariateÚ expovariateÚvonmisesvariateÚ gammavariateÚ triangularÚgaussÚ betavariateÚ paretovariateÚweibullvariateÚgetstateÚsetstateÚ getrandbitsÚchoicesÚ SystemRandomégà¿g@g@gð?g@é5écseZdZdZdZd;dd„Zd<‡fdd„ Z‡fd d „Z‡fd d „Zd d„Z dd„Z dd„Z dde fdd„Z dd„Ze de>eeefdd„Zdd„Zd=dd„Zdd„Zd>ddd œd!d"„Zd#d$„Zd?d'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Z d7d8„Z!d9d:„Z"‡Z#S)@raã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| |¡d|_dS)zeInitialize an instance. Optional argument x controls seeding, as for Random.seed(). N)rÚ gauss_next)ÚselfÚx©r1ú+/opt/alt/python37/lib64/python3.7/random.pyÚ__init__Xs zRandom.__init__r,csâ|dkr†t|ttfƒr†t|tƒr*| d¡n|}|rBt|dƒd>nd}x"tt|ƒD]}d||Ad@}qRW|t|ƒN}|dkr‚dn|}|d krÌt|tttfƒrÌt|tƒr°| ¡}|t |ƒ  ¡7}t   |d ¡}t ƒ |¡d |_d S) aInitialize 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 an int, all bits are used. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds. ézlatin-1réiCBlÿÿÿÿéÿÿÿÿéþÿÿÿr,ÚbigN)Ú isinstanceÚstrÚbytesÚdecodeÚordÚmapÚlenÚ bytearrayÚencodeÚ_sha512ZdigestÚintÚ from_bytesÚsuperrr.)r/ÚaÚversionr0Úc)Ú __class__r1r2ras    z Random.seedcs|jtƒ ¡|jfS)z9Return internal state; can be passed to setstate() later.)ÚVERSIONrEr%r.)r/)rIr1r2r%�szRandom.getstatec s¢|d}|dkr*|\}}|_tƒ |¡nt|dkrŒ|\}}|_ytdd„|Dƒƒ}Wn(tk r|}z t|‚Wdd}~XYnXtƒ |¡ntd||jfƒ‚dS)z:Restore internal state from object returned by getstate().rr-r,css|]}|dVqdS)lNr1)Ú.0r0r1r1r2ú ’sz"Random.setstate..Nz?state with version %s passed to Random.setstate() of version %s)r.rEr&ÚtupleÚ ValueErrorÚ TypeErrorrJ)r/ÚstaterGZ internalstater)rIr1r2r&…s  zRandom.setstatecCs| ¡S)N)r%)r/r1r1r2Ú __getstate__£szRandom.__getstate__cCs| |¡dS)N)r&)r/rPr1r1r2Ú __setstate__¦szRandom.__setstate__cCs|jd| ¡fS)Nr1)rIr%)r/r1r1r2Ú __reduce__©szRandom.__reduce__r4c Cs||ƒ}||krtdƒ‚|dkr:|dkr2| |¡Stdƒ‚||ƒ}||krRtdƒ‚||}|dkrx|dkrx|| |¡S|dkr’td|||fƒ‚||ƒ}||krªtdƒ‚|dkrÄ||d|} n"|dkrÞ||d|} ntd ƒ‚| dkrötdƒ‚||| | ¡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()r4z'empty range for randrange() (%d,%d, %d)z non-integer step for randrange()zzero step for randrange())rNÚ _randbelow) r/ÚstartÚstopÚstepÚ_intZistartZistopÚwidthZistepÚnr1r1r2r®s4  zRandom.randrangecCs| ||d¡S)zJReturn random integer in range [a, b], including both end points. r4)r)r/rFÚbr1r1r2rÚszRandom.randintc Csº|j}|j}||ƒ|ks$||ƒ|krN| ¡} || ƒ} x| |krH|| ƒ} q6W| S||krltdƒ||ƒ|ƒS|dkr|tdƒ‚||} || |} |ƒ} 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.rzBoundary cannot be zero)rr'Ú bit_lengthÚ_warnrN) r/rZrCÚmaxsizeÚtypeZMethodZ BuiltinMethodrr'ÚkÚrZremÚlimitr1r1r2rTàs&     zRandom._randbelowcCs:y| t|ƒ¡}Wntk r0tdƒd‚YnX||S)z2Choose a random element from a non-empty sequence.z$Cannot choose from an empty sequenceN)rTr?rNÚ IndexError)r/ÚseqÚir1r1r2rs z Random.choicecCs¢|dkrR|j}xŽttdt|ƒƒƒD]*}||dƒ}||||||<||<q"WnLt}xFttdt|ƒƒƒD]0}||ƒ|dƒ}||||||<||<qjWdS)zêShuffle list x in place, and return None. Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used. Nr4)rTÚreversedÚranger?rC)r/r0rÚ randbelowreÚjrXr1r1r2rs   zRandom.shufflec Cs&t|tƒrt|ƒ}t|tƒs$tdƒ‚|j}t|ƒ}d|krF|ksPntdƒ‚dg|}d}|dkr€|dtt |ddƒƒ7}||krÐt |ƒ}x�t |ƒD]0}|||ƒ} || ||<|||d || <qšWnRt ƒ} | j } xDt |ƒD]8}||ƒ} x| | k�r||ƒ} qôW| | ƒ|| ||<qæW|S) a=Chooses k unique random elements from a population sequence or set. 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 range as an argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), 60) z>Population must be a sequence or set. For dicts, use list(d).rz,Sample larger than population or is negativeNéér*r-r4)r9Ú_SetrMÚ _SequencerOrTr?rNÚ_ceilÚ_logÚlistrgÚsetÚadd) r/Ú populationr`rhrZÚresultZsetsizeZpoolreriZselectedZ selected_addr1r1r2rs6       z Random.sample)Ú cum_weightsr`cs°|j‰ˆdkrN|dkr>t‰tˆƒ‰‡‡‡‡fdd„t|ƒDƒStt |¡ƒ‰n|dk r^tdƒ‚tˆƒtˆƒkrvtdƒ‚t j ‰ˆd‰tˆƒd‰‡‡‡‡‡‡fdd„t|ƒDƒS) zÑReturn a k sized list of population elements chosen with replacement. If the relative weights or cumulative weights are not specified, the selections are made with equal probability. Ncsg|]}ˆˆˆƒˆƒ‘qSr1r1)rKre)rXrsrÚtotalr1r2ú dsz"Random.choices..z2Cannot specify both weights and cumulative weightsz3The number of weights does not match the populationr6r4cs$g|]}ˆˆˆˆƒˆdˆƒ‘qS)rr1)rKre)ÚbisectruÚhirsrrvr1r2rwms) rrCr?rgrpÚ _itertoolsÚ accumulaterOrNÚ_bisectrx)r/rsZweightsrur`r1)rXrxruryrsrrvr2r(Xs  zRandom.choicescCs|||| ¡S)zHGet a random number in the range [a, b) or [a, b] depending on rounding.)r)r/rFr[r1r1r2rtszRandom.uniformççð?cCsx| ¡}y |dkrdn||||}Wntk r<|SX||kr`d|}d|}||}}|||t||ƒS)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Ú_sqrt)r/ZlowZhighÚmodeÚurHr1r1r2r zs   zRandom.triangularcCsT|j}x@|ƒ}d|ƒ}t|d|}||d}|t|ƒ krPqW|||S)z\Normal distribution. mu is the mean, and sigma is the standard deviation. gð?gà?g@)rÚ NV_MAGICCONSTro)r/ÚmuÚsigmarÚu1Úu2ÚzZzzr1r1r2r�s   zRandom.normalvariatecCst| ||¡ƒ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/r„r…r1r1r2r©szRandom.lognormvariatecCstd| ¡ƒ |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ð?)ror)r/Zlambdr1r1r2rµszRandom.expovariatecCsÔ|j}|dkrt|ƒSd|}|td||ƒ}xN|ƒ}tt|ƒ}|||}|ƒ} | d||ks~| d|t|ƒkr6Pq6Wd|} | |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ÚTWOPIr€Ú_cosÚ_pir‰Ú_acos)r/r„ZkapparÚsrar†rˆÚdr‡ÚqÚfZu3Zthetar1r1r2rÈs&   $zRandom.vonmisesvariatecCsœ|dks|dkrtdƒ‚|j}|dkrÚtd|dƒ}|t}||}x�|ƒ}d|krbdksfqHqHd|ƒ}t|d|ƒ|} |t| ƒ} |||} ||| | } | td| dksÌ| t| ƒkrH| |SqHWn¾|dk�r |ƒ} x| dkrü|ƒ} qìWt| ƒ |Sx‚|ƒ} t|t}|| }|dk�r@|d|} nt|||ƒ } |ƒ}|dk�rx|| |dk�rŠPn|t| ƒk�rP�qW| |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)rNrr€ÚLOG4ror‰Ú SG_MAGICCONSTÚ_e)r/ÚalphaÚbetarZainvZbbbZcccr†r‡Úvr0rˆrar‚r[Úpr1r1r2røsJ          zRandom.gammavariatecCs`|j}|j}d|_|dkrT|ƒt}tdtd|ƒƒƒ}t|ƒ|}t|ƒ||_|||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ð?)rr.rŠr€ror‹Ú_sin)r/r„r…rrˆZx2piZg2radr1r1r2r!@s  z Random.gausscCs0| |d¡}|dkrdS||| |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–Úyr1r1r2r"us zRandom.betavariatecCsd| ¡}d|d|S)z3Pareto distribution. alpha is the shape parameter.gð?)r)r/r•r‚r1r1r2r#‡s zRandom.paretovariatecCs"d| ¡}|t|ƒ d|S)zfWeibull distribution. alpha is the scale parameter and beta is the shape parameter. gð?)rro)r/r•r–r‚r1r1r2r$�s zRandom.weibullvariate)N)Nr,)N)N)r}r~N)$Ú__name__Ú __module__Ú __qualname__Ú__doc__rJr3rr%r&rQrRrSrCrrÚBPFr_Ú _MethodTypeÚ_BuiltinMethodTyperTrrrr(rr rrrrrr!r"r#r$Ú __classcell__r1r1)rIr2rHs8    ,  :  0H5 c@s8eZdZdZdd„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). cCst tdƒd¡d?tS)z3Get the next random number in the range [0.0, 1.0).r5r8r-)rCrDÚ_urandomÚ RECIP_BPF)r/r1r1r2r¥szSystemRandom.randomcCsP|dkrtdƒ‚|t|ƒkr$tdƒ‚|dd}t t|ƒ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 integerr5ér8)rNrCrOrDr£)r/r`Znumbytesr0r1r1r2r'©s  zSystemRandom.getrandbitscOsdS)z&st     Y!