o
    h                     @   s   d Z ddlZddlmZ g d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 r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/networkx/utils/random_sequence.py
<listcomp>   s    z%powerlaw_sequence.<locals>.<listcomp>)range)nr   r   r   r   r   r      s   r   r   c           	      C   s   |dk rt d| dkrt d| d }d| }	 d|  }| }t||d|    }dd|  | }|| |d  |d  || krI	 |S q)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)	alphaxminr   a1buvxtr   r   r   r       s   1 r   c                 C   s>   dg}t | }tt| D ]}||| | | |   q|S )zFReturns normalized cumulative distribution from discrete distribution.g        )sumr   lenappend)distributioncdfpsumr   r   r   r   r   a   s
   r      c                    s`   ddl  |dur|n|du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   nxNetworkXErrorr   )r   r'   cdistributionr   inputseqseqr   )r-   r(   r   r   r   k   s   
r   c                 C   sJ   |t | kr
tdt }t ||k r!|t| | t ||k s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   r   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)r6   r   rndr7   wr   r   r   r      s   r   )r
   N)r   N)NNN)N)__doc__networkxr.   networkx.utilsr   __all__r   r   r   r   r   r   r   r   r   r   <module>   s    @
