Skip to content

Instantly share code, notes, and snippets.

@lithtle
Created May 10, 2012 09:36
Show Gist options
  • Save lithtle/2652151 to your computer and use it in GitHub Desktop.
Save lithtle/2652151 to your computer and use it in GitHub Desktop.
全10種類のコンプガチャ1回300円、全種類コンプまでどれだけお金をするのかシミュレーション。当選確率は一様にしてあるため実際はこれよりだいぶひどい結果が出ると思われる。notepad++で書いたのでタブ幅は2ではない
#! /usr/bin/ruby -Ku
include Math
KINDS = 10 # 景品の種類
COST = 300 # 一回の料金
class Gacha
def initialize()
@trial = 0
@total = 0
s = Array.new(KINDS, false)
while !fullfill?(s)
r = rand(KINDS).floor
s[r] = true
@total += COST
@trial += 1
end
end
def fullfill?(set)
ret = true
set.each do |i|
ret &= i
end
return ret
end
def inspect
return sprintf "try: %5d, cost: %8d", @trial, @total
end
attr_reader :trial, :total
end
class GachaTest
def initialize(n=0)
count = 0
@n = n
@@average_cost = 0.0
@@average_trial = 0
while count < n
g = Gacha.new
@@average_cost += g.total
@@average_trial += g.trial
count += 1
p g
end
end
def inspect
return sprintf "\ntest num: %d\n\taverage trial: %5.3f, average cost: %5.3f", @n, @@average_trial / @n, @@average_cost / @n
end
end
p GachaTest.new(10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment