Last active
May 8, 2016 05:05
-
-
Save lucianspec/42cdcec7a4ba6fbc86e0aba1c1e744d2 to your computer and use it in GitHub Desktop.
roll scipt for fun
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
def generate_pool likes, comments | |
all = likes.dup.concat(comments).uniq | |
puts "总人数#{all.count}" | |
puts "点赞党有#{likes.uniq.count}" | |
puts "评论党有#{comments.uniq.count}" | |
puts "计算权重ing..." | |
hash = all.reduce(Hash.new(0)) {|m, o| | |
priority = 1; | |
priority = 2 if comments.include?(o) | |
m[o] = priority | |
m | |
} | |
pool = hash.map {|k, v| [k]*v}.flatten | |
end | |
def start likes, comments | |
r = Random.new(Time.now.to_i) | |
pool = generate_pool(likes, comments) | |
index = r.rand(pool.size) | |
winner = pool[index] | |
puts "抽奖结束!" | |
puts "Winner is #{winner}" | |
texts = {1 => '点赞党', 2 => '评论党', 3 => '点赞评论党'} | |
type = 0 | |
type = type + 1 if likes.include?(winner) | |
type = type + 2 if comments.include?(winner) | |
puts "本次#{texts[type]}获胜" | |
end | |
def test | |
# l = [点赞党, 点赞评论党,不存在的多次点赞党,不存在的多次点赞党] | |
# c = [评论党, 点赞评论党, 评论聊天党, 评论聊天党] | |
l = ["like", "like_comment", "unknow", "unknow"] | |
c = ["comment", "like_comment", "multi_comment", "multi_comment"] | |
pool = generate_pool(l, c) | |
res = Hash.new(0) | |
r = Random.new(Time.now.to_i) | |
size = pool.size | |
roll_time = 1000000 | |
roll_time.times { | |
winner = pool[r.rand(size)] | |
res[winner] = res[winner] + 1 | |
hash | |
} | |
pp res | |
freq = res.dup | |
freq.each{|k,v| freq[k] = "#{v.to_f/roll_time.to_f*100}%"} | |
pp freq | |
end | |
# {"like"=>"12.512799999999999%", | |
# "multi_comment"=>"24.9907%", | |
# "unknow"=>"12.5142%", | |
# "comment"=>"25.0031%", | |
# "like_comment"=>"24.9792%"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment