Skip to content

Instantly share code, notes, and snippets.

@mono0x
Created August 24, 2012 08:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0x/3447808 to your computer and use it in GitHub Desktop.
Save mono0x/3447808 to your computer and use it in GitHub Desktop.
Paper Fortune
# -*- coding: utf-8 -*-
require 'date'
require 'digest/sha1'
module PaperFortune
def self.paper_fortune(w, *a)
n = w.inject(&:+)
r = Random.new(Digest::SHA1.hexdigest(Marshal.dump(a)).to_i(16)).rand(n)
w.each_index.find {|i| r < w[0..i].inject(&:+) }
end
end
japanese = [
'大吉',
'中吉',
'小吉',
'吉',
'半吉',
'末吉',
'末小吉',
'凶',
'小凶',
'半凶',
'末凶',
'大凶',
]
weights = [ 3, 5, 7, 10, 10, 15, 15, 10, 10, 7, 5, 3 ]
name = 'mono0x'
p name, japanese[PaperFortune.paper_fortune(weights, Date.today, name)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment