AlkantarClanX12

Your IP : 18.216.53.7


Current Path : /opt/alt/python34/lib64/python3.4/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/__pycache__/random.cpython-34.pyc

�
e f�e�@sdZddlmZddlmZmZddl	m
ZmZ
mZmZmZddl	mZmZmZmZddlmZddlm Z!m"Z#ddl$m%Z&d	d
ddd
ddddddddddddddddddgZ'd e
d!�ed"�Z(d"eZ)ed#�Z*d$ed%�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�fdd�Zd
d�Z	dd�Z
dd�Zdde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

zRandom.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�	<genexpr>�sz"Random.setstate.<locals>.<genexpr>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._randbelowcCsBy|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�}||||||<||<q1Wn`t}xWttdt|���D]:}||�|d�}||||||<||<q�WdS)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.

        NrO)rP�reversed�ranger_r5)r.r/r�	randbelowrb�jrTr0r0r1rs		"$"zRandom.shufflecCs�t|t�rt|�}nt|t�s<td��n|j}t|�}d|koh|kns|td��ndg|}d}|dkr�|dtt	|dd��7}n||kr%t
|�}x�t|�D]:}|||�}	||	||<|||d	||	<q�Wnlt�}
|
j
}xWt|�D]I}||�}	x|	|
krt||�}	qYW||	�||	||<qDW|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).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<Stub method.  Not used for a system random number generator.Nr0)r.�args�kwdsr0r0r1r�szSystemRandom.seedcOstd��dS)zAMethod should not be called for a system random number generator.z*System entropy source does not have state.N)r8)r.r�r�r0r0r1�_notimplemented�szSystemRandom._notimplementedN)
r�r�r�r�rr'rr�r%r&r0r0r0r1r(s
cCsddl}t|d|j�d}d}d}d
}|j�}xVt|�D]H}	||�}
||
7}||
|
}t|
|�}t|
|�}qPW|j�}tt||d�ddd�||}t||||�}
td	||
||f�dS)Nr�timesgg _�Br,zsec,�end� z!avg %g, stddev %g, min %g, max %gg _��)r9�printr�rd�min�max�roundr})rV�funcr�r9ZtotalZsqsumZsmallestZlargestZt0rbr/Zt1ZavgZstddevr0r0r1�_test_generator�s&
 
r�i�cCst|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�t|td�dS)N���?�{�G�z�?皙�����?�@��?��������?�4@�i@�@)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r�)r�r��UUUUUU�?)r�r�r�)	r�rrrrrr!r"r )�Nr0r0r1�_test�s r��__main__)Jr��warningsrrY�typesrr�rr�Zmathrrlrr{rrrr�r	rkr
r}rr�rr~r
r��osrr7�_collections_abcrrirrjZhashlibrr?�__all__rur|r�r�r�r�Z_randomrr(r�r�Z_instrrrr rrrrrrrrrrr!r"r#r$r%r&r'r�r0r0r0r1�<module>%sd("		
��=!