U
    :vf                     @   s   d Z ddlZddlmZ dddddd	gZed
dddZed
dddZdd ZeddddZ	ed
dddZ
edddd	ZdS )zS
Utilities for generating random numbers, random sequences, and
random selections.
    N)py_random_statepowerlaw_sequencezipf_rvcumulative_distributiondiscrete_sequencerandom_weighted_sampleweighted_choice          @c                    s    fddt | D S )zK
    Return sample sequence of length n from a power law distribution.
    c                    s   g | ]}  d  qS    )paretovariate.0iexponentseed B/tmp/pip-unpacked-wheel-_lngutwb/networkx/utils/random_sequence.py
<listcomp>   s     z%powerlaw_sequence.<locals>.<listcomp>)range)nr   r   r   r   r   r      s    r   c           	      C   s   |dk rt d| dkr t d| d }d| }d|  }| }t||d|    }dd|  | }|| |d  |d  || kr0qq0|S )aw  Returns a random value chosen from the Zipf distribution.

    The return value is an integer drawn from the probability distribution

    .. math::

        p(x)=\frac{x^{-\alpha}}{\zeta(\alpha, x_{\min})},

    where $\zeta(\alpha, x_{\min})$ is the Hurwitz zeta function.

    Parameters
    ----------
    alpha : float
      Exponent value of the distribution
    xmin : int
      Minimum value
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    x : int
      Random value from Zipf distribution

    Raises
    ------
    ValueError:
      If xmin < 1 or
      If alpha <= 1

    Notes
    -----
    The rejection algorithm generates random values for a the power-law
    distribution in uniformly bounded expected time dependent on
    parameters.  See [1]_ for details on its operation.

    Examples
    --------
    >>> nx.utils.zipf_rv(alpha=2, xmin=3, seed=42)
    8

    References
    ----------
    .. [1] Luc Devroye, Non-Uniform Random Variate Generation,
       Springer-Verlag, New York, 1986.
    r   zxmin < 1za <= 1.0g      ?r	   )
ValueErrorrandomint)	alphaZxminr   Za1buvxtr   r   r   r       s    1 c                 C   s@   dg}t | }tdt| D ]}||| | | |   q|S )zFReturns normalized cumulative distribution from discrete distribution.g        r   )sumr   lenappend)distributioncdfZpsumr   r   r   r   r   a   s
       c                    s`   ddl  |dk	r|n|dk	r(t|n
tdfddt| D } fdd|D }|S )a#  
    Return sample sequence of length n from a given discrete distribution
    or discrete cumulative distribution.

    One of the following must be specified.

    distribution = histogram of values, will be normalized

    cdistribution = normalized discrete cumulative distribution

    r   Nz8discrete_sequence: distribution or cdistribution missingc                    s   g | ]}   qS r   )r   r   )r   r   r   r      s     z%discrete_sequence.<locals>.<listcomp>c                    s   g | ]}  |d  qS r   )bisect_left)r   s)bisectr&   r   r   r      s     )r*   r   nxZNetworkXErrorr   )r   r%   Zcdistributionr   Zinputseqseqr   )r*   r&   r   r   r   k   s    
c                 C   s@   |t | krtdt }t ||k r8|t| | qt|S )zReturns k items without replacement from a weighted sample.

    The input is a dictionary of items with weights as values.
    zsample larger than population)r#   r   setaddr   list)mappingkr   sampler   r   r   r      s    c                 C   sB   |  t|   }|  D ] \}}||8 }|dk r|  S qdS )zuReturns a single element from a weighted sample.

    The input is a dictionary of items with weights as values.
    r   N)r   r"   valuesitems)r0   r   Zrndr1   wr   r   r   r      s
    )r
   N)r   N)NNN)N)N)__doc__Znetworkxr+   Znetworkx.utilsr   __all__r   r   r   r   r   r   r   r   r   r   <module>   s(   @
