Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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
share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.