
    wg                         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y)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                 b    t        |       D cg c]  }|j                  |dz
         c}S c c}w )zK
    Return sample sequence of length n from a power law distribution.
       )rangeparetovariate)nexponentseedis       c/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/utils/random_sequence.pyr   r      s+    
 7<Ah?Dx!|,???s   ,r   c                    |dk  rt        d      | dk  rt        d      | dz
  }d|z  }	 d|j                         z
  }|j                         }t        ||d|z   z  z        }dd|z  z   |z  }||z  |dz
  z  |dz
  z  ||z  k  r	 |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 ax$$z$$	B	2A
$++-KKMqcBhK''(C!G_#q5AGC(AE1H     c                     dg}t        |       }t        t        |             D ]  }|j                  ||   | |   |z  z          ! |S )zFReturns normalized cumulative distribution from discrete distribution.g        )sumr   lenappend)distributioncdfpsumr   s       r   r   r   a   sQ     %C|D3|$% 4

3q6LOd2234Jr       c                    ddl }||}n#|t        |      }nt        j                  d      t	        |       D cg c]  }|j                          }}|D cg c]  }|j                  ||      dz
   }	}|	S c c}w c c}w )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 missingr   )bisectr   nxNetworkXErrorr   r   bisect_left)
r   r%   cdistributionr   r*   r&   r   inputseqsseqs
             r   r   r   k   s      		!%l3F
 	

 (-Qx0!0H0 4<
<a6c1%)
<C
<J	 1 =s   A7A<c                     |t        |       kD  rt        d      t               }t        |      |k  r*|j                  t	        | |             t        |      |k  r*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   samples       r   r   r      sX     	3w<899UF
f+/

?7D12 f+/<r    c                     |j                         t        | j                               z  }| j                         D ]  \  }}||z  }|dk  s|c S  y)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   ws        r   r	   r	      sN     ++-#gnn./
/C 1q7Hr    )g       @N)r   N)NNN)N)__doc__networkxr+   networkx.utilsr   __all__r   r   r   r   r   r	    r    r   <module>rC      s   
  * @ @ = =@  > 
 
 
 
r    