ó
 ,µ[c           @   sË   d  Z  d d l Z d d l Z d d l Z d d l m Z e d ƒ d d d „ ƒ Z e d ƒ d d d „ ƒ Z	 d	 „  Z
 e d
 ƒ d d d d „ ƒ Z e d ƒ d d „ ƒ Z e d ƒ d d „ ƒ Z d S(   sS   
Utilities for generating random numbers, random sequences, and
random selections.
iÿÿÿÿN(   t   py_random_statei   g       @c         C   s*   g  t  |  ƒ D] } | j | d ƒ ^ q S(   sK   
    Return sample sequence of length n from a power law distribution.
    i   (   t   ranget   paretovariate(   t   nt   exponentt   seedt   i(    (    s=   lib/python2.7/site-packages/networkx/utils/random_sequence.pyt   powerlaw_sequence   s    i   c   	      C   sÊ   | d k  r t  d ƒ ‚ n  |  d k r6 t  d ƒ ‚ n  |  d } d | } xy t rÅ d | j ƒ  } | j ƒ  } t | | d | ƒ } d d | | } | | | d | d | | k rM PqM qM W| S(   s|  Return 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.zipf_rv(alpha=2, xmin=3, seed=42)  # doctest: +SKIP

    References
    ----------
    .. [1] Luc Devroye, Non-Uniform Random Variate Generation,
       Springer-Verlag, New York, 1986.
    i   s   xmin < 1s   a <= 1.0g      ð?i   (   t
   ValueErrort   Truet   randomt   int(	   t   alphat   xminR   t   a1t   bt   ut   vt   xt   t(    (    s=   lib/python2.7/site-packages/networkx/utils/random_sequence.pyt   zipf_rv"   s    0

	$c         C   s\   d g } t  t |  ƒ ƒ } x: t d t |  ƒ ƒ D]# } | j | | |  | | ƒ q1 W| S(   sE   Return normalized cumulative distribution from discrete distribution.g        i    (   t   floatt   sumR   t   lent   append(   t   distributiont   cdft   psumR   (    (    s=   lib/python2.7/site-packages/networkx/utils/random_sequence.pyt   cumulative_distributionb   s
    	!i   c   
      C   s   d d l  } | d k	 r! | } n* | d k	 r< t | ƒ } n t j d ƒ ‚ g  t |  ƒ D] } | j ƒ  ^ qX } g  | D] } | j | | ƒ d ^ qw }	 |	 S(   s#  
    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

    iÿÿÿÿNs8   discrete_sequence: distribution or cdistribution missingi   (   t   bisectt   NoneR   t   nxt   NetworkXErrorR   R
   t   bisect_left(
   R   R   t   cdistributionR   R   R   R   t   inputseqt   st   seq(    (    s=   lib/python2.7/site-packages/networkx/utils/random_sequence.pyt   discrete_sequencel   s    		%)c         C   sc   | t  |  ƒ k r! t d ƒ ‚ n  t ƒ  } x, t  | ƒ | k  rX | j t |  | ƒ ƒ q- Wt | ƒ S(   s   Return k items without replacement from a weighted sample.

    The input is a dictionary of items with weights as values.
    s   sample larger than population(   R   R   t   sett   addt   weighted_choicet   list(   t   mappingt   kR   t   sample(    (    s=   lib/python2.7/site-packages/networkx/utils/random_sequence.pyt   random_weighted_sample‹   s    	c         C   sW   | j  ƒ  t |  j ƒ  ƒ } x4 |  j ƒ  D]& \ } } | | 8} | d k  r) | Sq) Wd S(   st   Return a single element from a weighted sample.

    The input is a dictionary of items with weights as values.
    i    N(   R
   R   t   valuest   items(   R+   R   t   rndR,   t   w(    (    s=   lib/python2.7/site-packages/networkx/utils/random_sequence.pyR)   ™   s
    
(   t   __doc__R
   t   syst   networkxR   t   networkx.utilsR    R   R   R   R   R&   R.   R)   (    (    (    s=   lib/python2.7/site-packages/networkx/utils/random_sequence.pyt   <module>   s   		?	
			