[p2p-hackers] zipf's law

Hal Finney hal at finney.org
Thu Jun 16 16:01:37 UTC 2005


John Casey writes:
> I was wondering if any one might help explain what is the best way to
> generate random variables that are distributed according to a zipf
> distribution with a slope of -1 and an output range of 1 to 30.

Maybe I'm misunderstanding...

The standard method for generating random values according to any given
distribution is to generate a uniform random value and map it to the
desired distribution.

In this case, if you want the 30 output values to have proportions
1, 1/2, 1/3, 1/4, ... 1/30, you would first sum these values.  Let
S = 1 + 1/2 + 1/3 + 1/4 + ... + 1/30.  Then generate a uniform random
value x in the range [0,S].  Then map it to your 30 bins by testing
it against the ranges [0,1], [1, 1+1/2], [1+1/2, 1+1/2+1/3], and so on.
The bin number is then your output variable.

Sorry about providing the obvious if you already knew this!

Hal Finney



More information about the P2p-hackers mailing list