test/genzipf.cpp
int zipf(double alpha, int n)
{
    static int first = TRUE;      // Static first time flag
    static double c = 0;          // Normalization constant
    double z;                     // Uniform random number (0 < z < 1)
    double sum_prob;              // Sum of probabilities
    double zipf_value;            // Computed exponential value to be returned
    int    i;                     // Loop counter

    // Compute normalization constant on first call only
    if (first == TRUE)
    {
        for (i=1; i<=n; i++)
            c = c + (1.0 / pow((double) i, alpha));
        c = 1.0 / c;
        first = FALSE;
    }

    // Pull a uniform random number (0 < z < 1)
    do
    {
        z = rand_val(0);
    }
    while ((z == 0) || (z == 1));

    // Map z to the value
    sum_prob = 0;
    for (i=1; i<=n; i++)
    {
        sum_prob = sum_prob + c / pow((double) i, alpha);
        if (sum_prob >= z)
        {
            zipf_value = i;
            break;
        }
    }

    // Assert that zipf_value is between 1 and N
    assert((zipf_value >=1) && (zipf_value <= n));

    return(zipf_value);
}
//===========================================================================
//=  Function to generate Zipf (power law) distributed random variables     =
//=    - Input: alpha and N                                                 =
//=    - Output: Returns with Zipf distributed random variable              =
//===========================================================================
Function to generate Zipf (power law) distributed random variables
Input: alpha and N
Output: Returns with Zipf distributed random variable
Function to generate Zipf ( power law ) distributed random variables Input : alpha and N Output : Returns with Zipf distributed random variable
int zipf(double alpha, int n)
{
    static int first = TRUE;      
    static double c = 0;          
    double z;                     
    double sum_prob;              
    double zipf_value;            
    int    i;                     
    if (first == TRUE)
    {
        for (i=1; i<=n; i++)
            c = c + (1.0 / pow((double) i, alpha));
        c = 1.0 / c;
        first = FALSE;
    }
    do
    {
        z = rand_val(0);
    }
    while ((z == 0) || (z == 1));
    sum_prob = 0;
    for (i=1; i<=n; i++)
    {
        sum_prob = sum_prob + c / pow((double) i, alpha);
        if (sum_prob >= z)
        {
            zipf_value = i;
            break;
        }
    }
    assert((zipf_value >=1) && (zipf_value <= n));
    return(zipf_value);
}
int zipf ( double alpha , int n ) { static int first = TRUE ; static double c = 0 ; double z ; double sum_prob ; double zipf_value ; int i ; if ( first == TRUE ) { for ( i = 1 ; i <= n ; i ++ ) c = c + ( 1.0 / pow ( ( double ) i , alpha ) ) ; c = 1.0 / c ; first = FALSE ; } do { z = rand_val ( 0 ) ; } while ( ( z == 0 ) || ( z == 1 ) ) ; sum_prob = 0 ; for ( i = 1 ; i <= n ; i ++ ) { sum_prob = sum_prob + c / pow ( ( double ) i , alpha ) ; if ( sum_prob >= z ) { zipf_value = i ; break ; } } assert ( ( zipf_value >= 1 ) && ( zipf_value <= n ) ) ; return ( zipf_value ) ; }
Function to generate Zipf (power law) distributed random variables
 Input: alpha and N
 Output: Returns with Zipf distributed random variable
Function to generate Zipf ( power law ) distributed random variables Input : alpha and N Output : Returns with Zipf distributed random variable
// Static first time flag // Normalization constant // Uniform random number (0 < z < 1) // Sum of probabilities // Computed exponential value to be returned // Loop counter // Compute normalization constant on first call only // Pull a uniform random number (0 < z < 1) // Map z to the value // Assert that zipf_value is between 1 and N