Search code examples
c++mathrandompower-law

Random number generator that produces a power-law distribution?


I'm writing some tests for a C++ command line Linux app. I'd like to generate a bunch of integers with a power-law/long-tail distribution. Meaning, I get a some numbers very frequently but most of them relatively infrequently.

Ideally there would just be some magic equations I could use with rand() or one of the stdlib random functions. If not, an easy to use chunk of C/C++ would be great.

Thanks!


Solution

  • This page at Wolfram MathWorld discusses how to get a power-law distribution from a uniform distribution (which is what most random number generators provide).

    The short answer (derivation at the above link):

    x = [(x1^(n+1) - x0^(n+1))*y + x0^(n+1)]^(1/(n+1))
    

    where y is a uniform variate, n is the distribution power, x0 and x1 define the range of the distribution, and x is your power-law distributed variate.