Is there any scripting language function (like in python or bash) that samples from a zipf-like distribution, for exponent 0<a<=1.

PS: I am aware of existence of a numpy function that can generate zipf samples, but it's only for a >1.

2 Answers 2


Based on the basic equations, how about:

import sys
k = float(sys.argv[1])
s = float(sys.argv[2])
N = int(sys.argv[3])
def zipf(k, s, N):
    return (1/k**s) / sum(1/n**s for n in range(1, N+1))
print zipf(k, s, N)

Uses only builtin, basic routines in the language. Could be converted to just about any other language, e.g. perl, ruby. Here is a version in awk:

#!/bin/awk -f
    for (n=1; n<=N; n++) {
    print (1/(k^s))/den;

The awk script requires no input file, but does require variable assignments on the command line:

$ /tmp/zipf.awk -vk=10 -vs=4 -vN=10

Wouldn't a Python expovariate distribution, with the correct value of lambda, work?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .