can you fix my lua program for simulating card probabilities for hearthstone packs
-- Dust values for normal and golden cards
local dust_values = {
normal = {
common = 5,
golden_common = 50,
rare = 20,
golden_rare = 100,
epic = 100,
golden_epic = 400,
legendary = 400,
golden_legendary = 1600,
},
golden = {
golden_common = 50,
golden_rare = 100,
golden_epic = 400,
golden_legendary = 1600,
},
}
-- Probability table for Normal and Golden Packs
local probabilities = {
normal = {
common = 0.71,
golden_common = 0.015,
rare = 0.22,
golden_rare = 0.005,
epic = 0.06,
golden_epic = 0.001,
legendary = 0.01,
golden_legendary = 0.0005,
},
golden = {
golden_common = 0.15,
golden_rare = 0.05,
golden_epic = 0.015,
golden_legendary = 0.005,
},
}
-- Function to check if the total probability sums to 1
function check_probabilities()
local total_normal = 0
local total_golden = 0
for _, prob in pairs(probabilities.normal) do
total_normal = total_normal + prob
end
for _, prob in pairs(probabilities.golden) do
total_golden = total_golden + prob
end
print("Total normal probabilities: " .. total_normal)
print("Total golden probabilities: " .. total_golden)
-- Ensure the total probabilities sum to 1 (or close to it)
assert(math.abs(total_normal - 1) < 0.0001, "Error: Normal pack probabilities do not sum to 1!")
assert(math.abs(total_golden - 1) < 0.0001, "Error: Golden pack probabilities do not sum to 1!")
end
-- Function to randomly pick a card type based on the probabilities
function pick_card(pack_type)
local roll = math.random()
local cumulative_prob = 0
local card_type
-- Corrected: We now select the correct card probabilities for normal and golden packs
if pack_type == "normal" then
for card, prob in pairs(probabilities.normal) do
cumulative_prob = cumulative_prob + prob
print("Checking card: " .. card .. " with cumulative probability: " .. cumulative_prob)
if roll < cumulative_prob then
card_type = card
break
end
end
elseif pack_type == "golden" then
for card, prob in pairs(probabilities.golden) do
cumulative_prob = cumulative_prob + prob
print("Checking card: " .. card .. " with cumulative probability: " .. cumulative_prob)
if roll < cumulative_prob then
card_type = card
break
end
end
end
-- If we still get a nil card_type, print an error for debugging
if card_type == nil then
print("Error: Unexpected card type for pack: " .. pack_type)
end
return card_type
end
-- Function to simulate a single pack
function simulate_pack(is_golden_pack)
local pack_dust = 0
local pack_type = is_golden_pack and "golden" or "normal"
-- Simulate the 5 cards in the pack
for i = 1, 5 do
local card_type = pick_card(pack_type)
-- Add the dust value for the picked card type
if card_type then
pack_dust = pack_dust + dust_values[pack_type][card_type]
else
print("Error: card_type is nil!")
return 0 -- Return zero if card_type is nil (shouldn't happen)
end
end
return pack_dust
end
-- Function to simulate multiple packs and calculate the average dust value
function simulate_average(num_simulations, is_golden_pack)
local total_dust = 0
for i = 1, num_simulations do
total_dust = total_dust + simulate_pack(is_golden_pack)
end
return total_dust / num_simulations
end
-- Run the simulation
math.randomseed(os.time())
-- Check that probabilities sum to 1
check_probabilities()
local num_simulations = 100000
local avg_normal_dust = simulate_average(num_simulations, false)
local avg_golden_dust = simulate_average(num_simulations, true)
-- Output the results as a single string
print(
string.format(
"Average dust value for a Normal Pack: %.0f\nAverage dust value for a Golden Pack: %.0f",
avg_normal_dust,
avg_golden_dust
)
)