a �DOg�z�@s�dZddlmZddlmZmZm Z m Z m ZddlmZmZmZmZddlmZmZddlmZddlmZ m!Z"ddl#m$Z%m&Z'dd l(m(Z)dd lZ*dd l+Z+zdd l,m-Z,Wne.y�dd l/m-Z,Yn0gd �Z0d ed�ed�Z1ed�Z2ded�Z3dZ4de4 Z5Gdd�de+j6�Z6Gdd�de6�Z7e6�Z8e8j9Z9e8j:Z:e8j;Z;e8jZ>e8j?Z?e8j@Z@e8jAZAe8jBZBe8jCZCe8jDZDe8jEZEe8jFZFe8jGZGe8jHZHe8jIZIe8jJZJe8jKZKe8jLZLe8jMZMe8jNZNe8jOZOdd�ZPd!dd�ZQeRe*d��r�e*jSe8j9d�eTd k�r�eQ�d S)"a�Random variable generators. bytes ----- uniform bytes (values between 0 and 255) 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)�tau�floor)�urandom)�Set�Sequence)� accumulate�repeat)�bisectN)�sha512)�Random� SystemRandom� betavariate�choice�choices� expovariate� gammavariate�gauss� getrandbits�getstate�lognormvariate� normalvariate� paretovariate� randbytes�randint�random� randrange�sample�seed�setstate�shuffle� triangular�uniform�vonmisesvariate�weibullvariate�g��@�@��?�@�5�cs*eZdZdZdZdBdd�ZdC�fdd� Z�fd d �Z�fd d �Zd d�Z dd�Z dd�Z dd�Z dd�Z de>fdd�Ze Zdd�ZdDdd�Zdd�Zd d!�ZdEd"d#�Zdd$�d%d&�ZdFddd'�d(d)�Zd*d+�ZdGd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Zdd?�Z!d@dA�Z"�Z#S)Hra�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�r9�/usr/lib64/python3.9/random.py�__init__us zRandom.__init__r4cs|dkr�t|ttf�r�t|t�r*|�d�n|}|rBt|d�d>nd}tt|�D]}d||Ad@}qP|t|�N}|dkr~dn|}nj|d kr�t|tttf�r�t|t�r�|��}t � |t |�� �d �}n&t|t d �t ttttf�s�td td �t��|�d |_d S) a\Initialize internal state from a seed. The only supported seed types are None, int, float, str, bytes, and bytearray. 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��������������r4�bigNz�Seeding based on hashing is deprecated since Python 3.9 and will be removed in a subsequent version. The only supported seed types are: None, int, float, str, bytes, and bytearray.)� isinstance�str�bytes�decode�ord�map�len� bytearray�encode�int� from_bytes�_sha512�digest�type�float�_warn�DeprecationWarning�superr'r6)r7�a�versionr8�c�� __class__r9r:r'~s"  � z Random.seedcs|jt���|jfS)z9Return internal state; can be passed to setstate() later.)�VERSIONrRrr6�r7rVr9r:r�szRandom.getstatec s�|d}|dkr*|\}}|_t��|�nv|dkr�|\}}|_ztdd�|D��}Wn*ty~}zt|�WYd}~n d}~00t��|�ntd||jf��dS)z:Restore internal state from object returned by getstate().rr5r4css|]}|dVqdS)lNr9)�.0r8r9r9r:� ��z"Random.setstate..Nz?state with version %s passed to Random.setstate() of version %s)r6rRr(�tuple� ValueError� TypeErrorrX)r7�staterT� internalstaterrVr9r:r(�s  �zRandom.setstatecCs|��S�N)rrYr9r9r:� __getstate__�szRandom.__getstate__cCs|�|�dSrb)r()r7r`r9r9r:� __setstate__�szRandom.__setstate__cCs|jd|��fS)Nr9)rWrrYr9r9r:� __reduce__�szRandom.__reduce__cKsJ|jD]>}d|jvrqFd|jvr.|j|_qFd|jvr|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. � _randbelowrr$N)�__mro__�__dict__�_randbelow_with_getrandbitsrf�_randbelow_without_getrandbits)�cls�kwargsrUr9r9r:�__init_subclass__�s    zRandom.__init_subclass__cCs4|sdS|j}|��}||�}||kr0||�}q|S)z;Return a random int in the range [0,n). Returns 0 if n==0.r)r� bit_length)r7�nr�k�rr9r9r:ri�s z"Random._randbelow_with_getrandbitsr<cCsj|j}||kr$td�t|�|�S|dkr0dS||}|||}|�}||krZ|�}qJt||�|S)z�Return a random int in the range [0,n). Returns 0 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.r)r$rP�_floor)r7ro�maxsizer$�rem�limitrqr9r9r:rj�s z%Random._randbelow_without_getrandbitscCs|�|d��|d�S)�Generate n random bytes.��little)r�to_bytes�r7ror9r9r:r"szRandom.randbytesc Cst|�}||krtd��|dur:|dkr2|�|�Std��t|�}||krRtd��||}|dkrx|dkrx||�|�S|dkr�td|||f��t|�}||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()r<z(empty range for randrange() (%d, %d, %d)z non-integer step for randrange()zzero step for randrange())rJr^rf) r7�start�stop�step�istart�istop�width�istepror9r9r:r%"s4  zRandom.randrangecCs|�||d�S)zJReturn random integer in range [a, b], including both end points. r<)r%�r7rS�br9r9r:r#NszRandom.randintcCs||�t|��S)z2Choose a random element from a non-empty sequence.)rfrG)r7�seqr9r9r:rWsz Random.choicecCs�|durN|j}ttdt|���D]*}||d�}||||||<||<q nTtdtd�t}ttdt|���D]0}||�|d�}||||||<||<qpdS)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. Nr<zuThe *random* parameter to shuffle() has been deprecated since Python 3.9 and will be removed in a subsequent version.r4)rf�reversed�rangerGrPrQrr)r7r8r$� randbelow�i�jr r9r9r:r)\s  �zRandom.shuffle)�countscs�t�t�rtdtd�t���t�t�s0td��t��}|dur�tt |���t��|kr`t d���� �}t|t �sztd��|dkr�t d��|j t|�|d �}t����fd d �|D�S|j}d|kr�|ks�nt d ��dg|}d } |dk�r | dtt|dd��7} || k�r\t��} t|�D]2} ||| �} | | || <| || d| | <�q&nNt�} | j}t|�D]8} ||�} | | v�r�||�} �q||| ��| || <�qp|S)amChooses 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. Repeated elements can be specified one at a time or with the optional counts parameter. For example: sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to: sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5) To choose a sample from a range of integers, use range() for the population argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), 60) z\Sampling from a set deprecated since Python 3.9 and will be removed in a subsequent version.r4zAPopulation must be a sequence. For dicts or sets, use sorted(d).Nz2The number of counts does not match the populationzCounts must be integersrz)Total of counts must be greater than zero)rpcsg|]}���|��qSr9r9)rZ�s�r� cum_counts� populationr9r:� �r\z!Random.sample..z,Sample larger than population or is negative��r.r5r<)rA�_SetrPrQr]� _Sequencer_rG�list� _accumulater^�poprJr&r��_bisectrf�_ceil�_log�set�add)r7r�rpr�ro�total� selectionsr��result�setsize�poolr�r��selected� selected_addr9r�r:r&vsT5 �             z Random.sample)� cum_weightsrpcs�|j�t����dur�|durHt��d7�����fdd�td|�D�Sztt|���Wq�ty�t|t�sr�|}td|���d�Yq�0n|dur�td��t���kr�t d���dd��dkr�t d ��t ��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|]}�������qSr9r9�rZr�)r ror�r$r9r:r��r\z"Random.choices..z4The number of choices must be a keyword argument: k=z2Cannot specify both weights and cumulative weightsz3The number of weights does not match the populationr>z*Total of weights must be greater than zeror<cs$g|]}������d���qS)rr9r�)rr��hir�r$r�r9r:r��s�) r$rGrr�_repeatr�r�r_rArJr^r�)r7r��weightsr�rpr9)rr�r r�ror�r$r�r:r�s<  ��   �zRandom.choicescCs||||��S)zHGet a random number in the range [a, b) or [a, b] depending on rounding.�r$r�r9r9r:r+�szRandom.uniformr�r1cCsz|��}z |durdn||||}Wnty>|YS0||krbd|}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��?r1)r$�ZeroDivisionError�_sqrt)r7�low�high�mode�urUr9r9r:r*s     zRandom.triangularcCsP|j}|�}d|�}t|d|}||d}|t|� krqDq|||S)z\Normal distribution. mu is the mean, and sigma is the standard deviation. r1r�r0)r$� NV_MAGICCONSTr�)r7�mu�sigmar$�u1�u2�z�zzr9r9r:r s   zRandom.normalvariatecCs`|j}|j}d|_|durT|�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�r1)r$r6�TWOPIr�r��_cos�_sin)r7r�r�r$r��x2pi�g2radr9r9r:r,s  z Random.gausscCst|�||��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 )r7r�r�r9r9r:rRszRandom.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. r1)r�r$)r7�lambdr9r9r:r\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�r1)r$r�r�r��_pir��_acos)r7r��kappar$r�rqr�r��dr��q�f�u3�thetar9r9r:r,ms$   $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.0r1r/gH�����z>g�P����?r2N)r^r$r��LOG4r�r�� SG_MAGICCONST�_e)r7�alpha�betar$�ainv�bbb�cccr�r��vr8r�rqr�r��pr9r9r:r�s@        zRandom.gammavariatecCs(|�|d�}|r$|||�|d�SdS)z�Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. r1r�)r)r7r�r��yr9r9r:r�s zRandom.betavariatecCsd|��}d|d|S)z3Pareto distribution. alpha is the shape parameter.r1r�)r7r�r�r9r9r:r!�s zRandom.paretovariatecCs"d|��}|t|� d|S)zfWeibull distribution. alpha is the scale parameter and beta is the shape parameter. r1)r$r�)r7r�r�r�r9r9r:r-�s zRandom.weibullvariate)N)Nr4)Nr<)N)N)r�r1N)$�__name__� __module__� __qualname__�__doc__rXr;r'rr(rcrdrermri�BPFrjrfr"r%r#rr)r&rr+r*r rrrr,rrr!r-� __classcell__r9r9rVr:res>  *  !   ,  c& & *Arc@s@eZdZdZdd�Zdd�Zdd�Zdd �Zd d �ZeZ Z d S) rz�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).r=r@r5)rJrK�_urandom� RECIP_BPFrYr9r9r:r$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 non-negativer=rwr@)r^rJrKr�)r7rp�numbytesr8r9r9r:rs  zSystemRandom.getrandbitscCst|�S)rv)r�rzr9r9r:r"szSystemRandom.randbytescOsdS)z.z.3fz sec, z times z"avg %g, stddev %g, min %g, max %g ) � statisticsr�r��timer�r��min�max�printr�) ror�r�r��meanr��t0�data�t1�xbarr�r�r�r9r�r:�_test_generatorSs   r���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 �t|td �dS)Nr9)r�r1)g{�G�z�?r1)皙�����?r1)r�r/)r�r1)g�������?r1)r1r1)r/r1)g4@r1)gi@r1)�@r�)r�r1gUUUUUU�?) r�r$r rr,rrrr*)�Nr9r9r:�_testds                r��fork)�after_in_child�__main__)r�)Ur��warningsrrP�mathrr�rr�rr�rr�rr�rr�r r�r r�r r�r r�r rr�osrr��_collections_abcrr�rr�� itertoolsrr�rr�rr��_os�_randomrLr� ImportError�hashlib�__all__r�r�r�r�r�rr�_instr'r$r+r*r#rr%r&r)rr rrr,rrrr!r-rr(rr"r�r��hasattr�register_at_forkr�r9r9r9r:�sr/      *,