Created
January 19, 2010 15:24
-
-
Save antirez/280994 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Power law (log tail) distribution | |
# Copyright(C) 2010 Salvatore Sanfilippo | |
# this code is under the public domain | |
# min and max are both inclusive | |
# n is the distribution power: the higher, the more biased | |
def powerlaw(min,max,n) | |
max += 1 | |
pl = ((max**(n+1) - min**(n+1))*rand() + min**(n+1))**(1.0/(n+1)) | |
(max-1-pl.to_i)+min | |
end | |
freq = {} | |
100000.times { | |
n = powerlaw(30,80,2).to_i | |
freq[n] = 0 if !freq[n] | |
freq[n] += 1 | |
} | |
(0..100).each{|x| | |
puts "#{x} => #{freq[x]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment