how to program zipfian distribution

This is a discussion on how to program zipfian distribution within the C Programming forums, part of the General Programming Boards category; The thing is this: A line range is between a and b, in x axis. I have to generate random ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    how to program zipfian distribution

    The thing is this: A line range is between a and b, in x axis. I have to generate random numbers according to the zipfian distribution(skewed towards a) in that range, but I have not found the way to manipulate a function to do that.

    Some idea?

    Thanks a lot!"!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3

    Wink some code

    now i find some code about zipfian, which gives the range between x1 and x2

    Code:
    double HsubV=0;
    
    void InitZipfHsubV(double p,int V=100) {
    	// calculate the V-th harmonic number HsubV. WARNING: V>1
    	HsubV = 0.0;
    	for (int i=1; i<=V; i++)
    		HsubV += 1.0/pow( (double)i, p); //Hsub=zipfian 1/c
    }
    
    //int ino[105]={0};
    float drd;
    float zipf(float x1, float x2, double p,int V=100) {
       float x,i;
       double r, sum;
    
       do {
    	   // assume InitZipfHsubV called, HsubV known
    	   // faster, no need to compute HsubV every time !
    
    	   drd = drand48();
    	   r=drd*HsubV;
    	   sum=1.0; 
    	   drd = drand48();i=drd;
    	   while (sum<r){
    	      //i++;  //commented by Yufei Tao
    
    		  drd = drand48(); i+=drd+1.0;
    
    	      sum += 1.0/pow( (double)i, p);
    	   }
    
    	   /* i follows Zipf distribution and lies between 1 and V */
    	   /* x lies between 0. and 1. and then between x1 and x2 */
    	   x = ( (float) i - 1. ) / ( (float) V - 1. );
    	   x = (x2 - x1) * x + x1;
    
    	   //if( x > x1 && x < x2 ) ino[int(ceil(i))]++ ;//printf( "i:%f r:%f x:%f\n",i,r,x );
       } while (x < x1 || x > x2);
       return(x);
    }

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    3

    Question i don't know what u mean?

    can u tell me in detail?please

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21