Skip to content

Instantly share code, notes, and snippets.

@lucianspec
Last active May 8, 2016 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucianspec/42cdcec7a4ba6fbc86e0aba1c1e744d2 to your computer and use it in GitHub Desktop.
Save lucianspec/42cdcec7a4ba6fbc86e0aba1c1e744d2 to your computer and use it in GitHub Desktop.
roll scipt for fun
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