
    \Mh                         d Z ddlZddlmZ g dZ ed          dd            Z ed          dd	            Zd
 Z ed          dd            Z	 ed          dd            Z
 e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                 >    fdt          |           D             S )zK
    Return sample sequence of length n from a power law distribution.
    c                 @    g | ]}                     d z
            S    )paretovariate).0iexponentseeds     ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/utils/random_sequence.py
<listcomp>z%powerlaw_sequence.<locals>.<listcomp>   s+    ???Dx!|,,???    )range)nr   r   s    ``r   r   r      s*    
 @????eAhh????r   r   c                 B   |dk     rt          d          | dk    rt          d          | dz
  }d|z  }	 d|                                z
  }|                                }t          ||d|z   z  z            }dd|z  z   |z  }||z  |dz
  z  |dz
  z  ||z  k    rni|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)	alphaxminr   a1buvxts	            r   r   r       s    b axx$$$zz$$$	B	2A$++--KKMMqcBhK''((C!G_#q5AGC(AE11 Hr   c                     dg}t          |           }t          t          |                     D ])}|                    ||         | |         |z  z              *|S )zFReturns normalized cumulative distribution from discrete distribution.g        )sumr   lenappend)distributioncdfpsumr   s       r   r   r   a   sc     %C|D3|$$%% 4 4

3q6LOd223333Jr      c                     ddl ||n&|t          |          nt          j        d          fdt	          |           D             }f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                 8    g | ]}                                 S  )r   )r   r   r   s     r   r   z%discrete_sequence.<locals>.<listcomp>   s!    000!000r   c                 B    g | ]}                     |          d z
  S r   )bisect_left)r   sbisectr+   s     r   r   z%discrete_sequence.<locals>.<listcomp>   s.    
<
<
<a6c1%%)
<
<
<r   )r4   r   nxNetworkXErrorr   )r   r*   cdistributionr   inputseqseqr4   r+   s      `  @@r   r   r   k   s     MMM 		!%l33F
 
 	

 1000uQxx000H =
<
<
<
<8
<
<
<CJr   c                    |t          |           k    rt          d          t                      }t          |          |k     r6|                    t	          | |                     t          |          |k     6t          |          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   samples       r   r   r      su     	3w<<8999UUF
f++//

?7D11222 f++//<<r   c                     |                                 t          |                                           z  }|                                 D ]\  }}||z  }|dk     r|c S 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)r>   r   rndr?   ws        r   r	   r	      sm     ++--#gnn..//
/C  1q77HHH  r   )r   N)r   N)NNN)N)__doc__networkxr5   networkx.utilsr   __all__r   r   r   r   r   r	   r0   r   r   <module>rJ      s5   
     * * * * * *   @ @ @ @ = = = =@      > 
 
 
 
 
 
 
 
 
 
r   