Skip to content

Instantly share code, notes, and snippets.

@sriprasanna
Created April 22, 2012 17:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sriprasanna/2465475 to your computer and use it in GitHub Desktop.
Save sriprasanna/2465475 to your computer and use it in GitHub Desktop.
Reddit ranking algorithm in Ruby
#!/Users/dustyeike/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
require "rubygems"
require "test/unit"
require "date"
EPOCH = Date.new(1970, 1, 1)
#
# Returns the number of seconds from the epoch to date
def epoch_seconds date
td = date - EPOCH
Time.at(td).day * 86400 + Time.at(td).sec + (Time.at(td).usec).to_f / 1000000
end
def score ups, down
ups - down
end
#
# The hot formula. Should match the equivalent function in postgres.
def hot ups, down, date
s = score ups, down
order = Math.log([s.abs, 1].max, 10)
if s > 0
sign = 1
elsif s < 0
sign = -1
else
sign = 0
end
seconds = epoch_seconds(date) - 1134028003
(order + sign * seconds / 45000).round
end
class TestRedditAlgoritym < Test::Unit::TestCase
def setup
today = DateTime.now
@now = Date.new(today.year, today.month, today.day)
end
def test_epoch_seconds
assert_equal(epoch_seconds(@now), 2678403.0)
end
def test_score
assert_equal(score(10, 5), 5)
end
def test_hot
assert_equal(hot(10,5,@now), -25140)
end
end
@jrochkind
Copy link

I'm trying to understand the reddit ranking algorithm, and came accross this gist.

How can hot(10,5,@now) equal -25140 for any value of @now? And yet, indeed it seems to, I just tried it, and with my different now calculated, well, now, it's still -25140. Seems to indicate something's not right?

Also I think you have a sign reversed somehow, shouldn't more upvotes than downvotes be positive rather than negative?

But even when done right, the ranking algorithm (which I got from the same blog posts you prob did and have been trying to play with too) doesn't seem to actually match what reddit does, I think those blog posts must have left something out; it seems to put way too much emphasis on votes and not enough on time of submission, while actual reddit does the reverse, doesn't it?

Have you figured out anything illuminating?

@simonprev
Copy link

As this is the 2nd result in Google and the gist has a typo and won't work properly, I should post the corrected gist in ruby(not made by me):

https://gist.github.com/jrochkind/2636355

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment