U e5d‚pã@sdZddlmZddlmZmZm Z m Z m ZddlmZmZmZmZddlmZddlmZmZddlm Z!m"Z#ddl$m$Z%dd lZ&zdd l'm(Z'Wn e)k r´dd l*m(Z'YnXd d d ddddddddddddddddddd d!d"gZ+d#ed$ƒed%ƒZ,d%e Z-ed&ƒZ.d'ed(ƒZ/d)Z0d*e0 Z1dd l2Z2Gd+d „d e2j3ƒZ3Gd,d"„d"e3ƒZ4d-d.„Z5d5d0d1„Z6e3ƒZ7e7j8Z8e7j9Z9e7j:Z:e7j;Z;e7jZ>e7j?Z?e7j@Z@e7jAZAe7jBZBe7jCZCe7jDZDe7jEZEe7jFZFe7jGZGe7jHZHe7jIZIe7jJZJe7jKZKe7jLZLe7jMZMeNe&d2ƒ�re&jOe7j8d3�ePd4k�re6ƒd S)6a°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)ÚlogÚexpÚpiÚeÚceil)ÚsqrtÚacosÚcosÚsin)Úurandom)ÚSetÚSequence)Ú accumulateÚrepeat)ÚbisectN)Úsha512Ú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à¿ç@ç@çð?ç@é5écs$eZdZdZdZd?dd„Zdd„Zd@‡fd d „ Z‡fd d „Z‡fd d„Z dd„Z dd„Z dd„Z dde fdd„Zdd„Zdd„Ze de>fdd„ZeZdd„ZdAd d!„Zd"d#„ZdBddd$œd%d&„Zd'd(„ZdCd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Zd9d:„Z d;d<„Z!d=d>„Z"‡Z#S)Draã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©r6ú/usr/lib64/python3.8/random.pyÚ__init__^s zRandom.__init__cKsJ|jD]>}d|jkrqFd|jkr.|j|_qFd|jkr|j|_qFqdS)aControl how subclasses generate random integers. The algorithm a subclass can use depends on the random() and/or getrandbits() implementation available to it and determines whether it can generate random integers from arbitrarily large ranges. Ú _randbelowr(rN)Ú__mro__Ú__dict__Ú_randbelow_with_getrandbitsr9Ú_randbelow_without_getrandbits)ÚclsÚkwargsÚcr6r6r7Ú__init_subclass__gs    zRandom.__init_subclass__r1csÞ|dkr‚t|ttfƒr‚t|tƒr*| d¡n|}|rBt|dƒd>nd}tt|ƒD]}d||Ad@}qP|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ÿÿÿÿéÿÿÿÿéþÿÿÿr1ÚbigN)Ú isinstanceÚstrÚbytesÚdecodeÚordÚmapÚlenÚ bytearrayÚencodeÚ_sha512ZdigestÚintÚ from_bytesÚsuperrr3)r4ÚaÚversionr5r@©Ú __class__r6r7r{s    z Random.seedcs|jtƒ ¡|jfS)z9Return internal state; can be passed to setstate() later.)ÚVERSIONrSr&r3©r4rVr6r7r&›szRandom.getstatec s¢|d}|dkr*|\}}|_tƒ |¡nt|dkrŒ|\}}|_ztdd„|Dƒƒ}Wn(tk r|}z t|‚W5d}~XYnXtƒ |¡ntd||jfƒ‚dS)z:Restore internal state from object returned by getstate().rr2r1css|]}|dVqdS)lNr6)Ú.0r5r6r6r7Ú ¬sz"Random.setstate..Nz?state with version %s passed to Random.setstate() of version %s)r3rSr'ÚtupleÚ ValueErrorÚ TypeErrorrX)r4ÚstaterUZ internalstaterrVr6r7r'Ÿs  þzRandom.setstatecCs| ¡S©N)r&rYr6r6r7Ú __getstate__½szRandom.__getstate__cCs| |¡dSr`)r')r4r_r6r6r7Ú __setstate__ÀszRandom.__setstate__cCs|jd| ¡fS)Nr6)rWr&rYr6r6r7Ú __reduce__ÃszRandom.__reduce__rBc 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()rBz(empty range for randrange() (%d, %d, %d)z non-integer step for randrange()zzero step for randrange())r]r9) r4ÚstartÚstopÚstepÚ_intZistartZistopÚwidthZistepÚnr6r6r7rÈs4  zRandom.randrangecCs| ||d¡S)zJReturn random integer in range [a, b], including both end points. rB)r©r4rTÚbr6r6r7rôszRandom.randintcCs,|j}| ¡}||ƒ}||kr(||ƒ}q|S)zCReturn a random int in the range [0,n). Raises ValueError if n==0.)r(Ú bit_length)r4rir(ÚkÚrr6r6r7r<ús  z"Random._randbelow_with_getrandbitscCsn|j}||kr$tdƒ||ƒ|ƒS|dkr4tdƒ‚||}|||}|ƒ}||kr^|ƒ}qN|||ƒ|S)z“Return a random int in the range [0,n). Raises ValueError if n==0. The implementation does not use getrandbits, but only random. 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)rÚ_warnr])r4rirQÚmaxsizerZremÚlimitrnr6r6r7r=s z%Random._randbelow_without_getrandbitscCs:z| t|ƒ¡}Wntk r0tdƒd‚YnX||S)z2Choose a random element from a non-empty sequence.z$Cannot choose from an empty sequenceN)r9rMr]Ú IndexError)r4ÚseqÚir6r6r7rs z Random.choicecCsš|dkrN|j}ttdt|ƒƒƒD]*}||dƒ}||||||<||<q nHt}ttdt|ƒƒƒD]0}||ƒ|dƒ}||||||<||<qddS)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. NrB)r9ÚreversedÚrangerMrQ)r4r5rÚ randbelowrtÚjrgr6r6r7r%s  zRandom.shufflec Cst|tƒrt|ƒ}t|tƒs$tdƒ‚|j}t|ƒ}d|krF|ksPntdƒ‚dg|}d}|dkr€|dtt |ddƒƒ7}||krÌt |ƒ}t |ƒD]0}|||ƒ} || ||<|||d || <q˜nHt ƒ} | j } t |ƒD]2}||ƒ} | | krþ||ƒ} qì| | ƒ|| ||<qà|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+r2rB)rGÚ_Setr\Ú _Sequencer^r9rMr]Ú_ceilÚ_logÚlistrvÚsetÚadd) r4Ú populationrmrwriÚresultZsetsizeZpoolrtrxZselectedZ selected_addr6r6r7r;s6)        z Random.sample)Ú cum_weightsrmcs´|j‰tˆƒ‰ˆdkrV|dkrHt‰ˆd7‰‡‡‡‡fdd„td|ƒDƒStt|ƒƒ‰n|dk rftdƒ‚tˆƒˆkrztdƒ‚t‰ˆdd‰ˆd‰‡‡‡‡‡‡fd d„td|ƒ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. Nçcsg|]}ˆˆˆƒˆƒ‘qSr6r6©rZrt)rgrir‚rr6r7Ú �sz"Random.choices..z2Cannot specify both weights and cumulative weightsz3The number of weights does not match the populationrDrBcs$g|]}ˆˆˆˆƒˆdˆƒ‘qS)rr6r†)rr„Úhir‚rÚtotalr6r7r‡˜sÿ) rrMrQÚ_repeatrÚ _accumulater^r]Ú_bisect)r4r‚Zweightsr„rmr6)rgrr„rˆrir‚rr‰r7r)‚s$  ÿzRandom.choicescCs|||| ¡S)zHGet a random number in the range [a, b) or [a, b] depending on rounding.©rrjr6r6r7rŸszRandom.uniformr…r.cCs|| ¡}z |dkrdn||||}Wntk r@|YSX||krdd|}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 Nçà?r.)rÚZeroDivisionErrorÚ_sqrt)r4ZlowZhighÚmodeÚur@r6r6r7r!¥s    zRandom.triangularcCsP|j}|ƒ}d|ƒ}t|d|}||d}|t|ƒ krqDq|||S)z\Normal distribution. mu is the mean, and sigma is the standard deviation. r.rŽr-)rÚ NV_MAGICCONSTr~)r4ÚmuÚsigmarÚu1Úu2ÚzZzzr6r6r7r»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)r4r”r•r6r6r7rÔ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. r.)r~r)r4Zlambdr6r6r7ràszRandom.expovariatecCsÐ|j}|dkrt|ƒSd|}|td||ƒ}|ƒ}tt|ƒ}|||}|ƒ} | d||ks€| d|t|ƒkr4q€q4d|} | |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�íµ ÷ư>rŽr.)rÚTWOPIr�Ú_cosÚ_pir™Ú_acos)r4r”ZkapparÚsrnr–r˜Údr—ÚqÚfZu3Zthetar6r6r7rós$   $zRandom.vonmisesvariatecCs~|dks|dkrtdƒ‚|j}|dkrÖtd|dƒ}|t}||}|ƒ}d|kr`dksdqFqFd|ƒ}t|d|ƒ|} |t| ƒ} |||} ||| | } | td| dksÊ| t| ƒkrF| |SqFn¤|dkròtd|ƒƒ |S|ƒ} t|t}|| }|dk�r$|d|} nt|||ƒ } |ƒ}|dk�r^|| |dk�rp�qrqò|t| ƒkrò�qrqò| |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 r…z*gammavariate: alpha and beta must be > 0.0r.r,gH¯¼šò×z>gËPÊÿÿï?r/N)r]rr�ÚLOG4r~r™Ú SG_MAGICCONSTÚ_e)r4ÚalphaÚbetarZainvZbbbZcccr–r—Úvr5r˜rnr’rkÚpr6r6r7r #s@        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Àr.)rr3ršr�r~r›Ú_sin)r4r”r•rr˜Zx2piZg2radr6r6r7r"hs  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. r.rr…N)r )r4r¥r¦Úyr6r6r7r#�s zRandom.betavariatecCsd| ¡}d|d|S)z3Pareto distribution. alpha is the shape parameter.r.r�)r4r¥r’r6r6r7r$¯s zRandom.paretovariatecCs"d| ¡}|t|ƒ d|S)zfWeibull distribution. alpha is the scale parameter and beta is the shape parameter. r.)rr~)r4r¥r¦r’r6r6r7r%¸s zRandom.weibullvariate)N)Nr1)N)N)r…r.N)$Ú__name__Ú __module__Ú __qualname__Ú__doc__rXr8rArr&r'rarbrcrQrrr<ÚBPFr=r9rrrr)rr!rrrrr r"r#r$r%Ú __classcell__r6r6rVr7rNs<    ,  G  0E5 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).rCrFr2)rQrRÚ_urandomÚ RECIP_BPFrYr6r6r7rÍszSystemRandom.randomcCs<|dkrtdƒ‚|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 zerorCérF)r]rQrRr±)r4rmZnumbytesr5r6r6r7r(Ñs  zSystemRandom.getrandbitscOsdS)zs’'   û  {