望ましい手札が得られる確率をシミュレーションで得る
デッキの初期化
目標カードを決めて、その他のカードとまとめてデッキにする
リストのデータ型?
識別可能なデータ型であればよい
code:jl
using Random
function build_deck(target_cards::Dict{String, Int}, deck_size::Int)
deck = String[]
# 目標カードを追加
for (card_name, count) in target_cards
append!(deck, fill(card_name, count))
end
# 残りのカードを追加
others = deck_size - length(deck)
for i in 1:others
push!(deck, "Other-" * string(i))
end
return shuffle!(deck)
end
山上から初期手札を取る
code:jl
function draw_initial_hand(deck::Vector{String}, initial_draw::Int)
end
手札が条件を満たすかのチェック
code:jl
function check_condition(hand::Vector{String}, condition::Function)
return condition(hand)
end
中身?
前処理関数
シミュレーションの実行
code:jl
function simulate(;
deck_size::Int = 40,
initial_draw::Int = 5,
target_cards::Dict{String, Int},
condition::Function,
iterations::Int = 10_000
)
successes = 0
deck = build_deck(target_cards, deck_size)
for _ in 1:iterations
deck = shuffle!(deck)
hand = draw_initial_hand(deck, initial_draw)
if check_condition(hand, condition)
successes += 1
end
end
return successes / iterations
end
統計値
code:jl
using StatsBase
function simulate_multiple(;
deck_size::Int = 40,
initial_draw::Int = 5,
target_cards::Dict{String, Int},
condition::Function,
iterations::Int = 10_000,
num_simulations::Int = 100
)
probabilities = Float64[]
for _ in 1:num_simulations
prob = simulate(
deck_size = deck_size,
initial_draw = initial_draw,
target_cards = target_cards,
condition = condition,
iterations = iterations
)
push!(probabilities, prob)
end
return (
probabilities = probabilities,
mean = mean(probabilities),
std = std(probabilities),
min = minimum(probabilities),
max = maximum(probabilities),
median = median(probabilities)
)
end
function print_statistics(result::NamedTuple)
println("平均値: $(round(result.mean * 100, digits=3))%")
println("標準偏差: $(round(result.std * 100, digits=3))%")
println("中央値: $(round(result.median * 100, digits=3))%")
println("最小値: $(round(result.min * 100, digits=3))%")
println("最大値: $(round(result.max * 100, digits=3))%")
end
code:jl
target_cards = Dict("A" => 3)
condition = hand -> count(card -> card == "A", hand) >= 1
probability = simulate(
deck_size = 40,
initial_draw = 5,
target_cards = target_cards,
condition = condition,
iterations = 100_000
)
println("$(probability * 100)%")