Created
August 24, 2012 08:59
-
-
Save mono0x/3447808 to your computer and use it in GitHub Desktop.
Paper Fortune
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
# -*- 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