If anyone's curious... (which I don't think anyone is...)
do
local memofac = {[0] = 1}
local function memo_fac(x)
while #memofac < x do
local newkey = #memofac+1
memofac[newkey] = memofac[#memofac] * (newkey)
end
return memofac[x]
end
function factorial(x)
assert(type(x) == 'number' and x>=0, "X must be a non-negative integer.", 2)
return memofac[x] or memo_fac(x)
end
end
function comb (n,k)
assert (type(n) == "number" and type(k) == "number" and n >= k,
"Expected numbers for Population and Samples. Samples must be smaller than Population")
return factorial(n)/(factorial(n-k)*factorial(k))
end
function draw (deckSize, handsize, ...)
local numInDeck = 1
local numDrawn = 2
local desired = {...}
local padding = {deckSize, 0}
local padDraws = 0
local prob = 0
for _, v in ipairs(desired) do
padding[numInDeck] = padding[numInDeck] - v[numInDeck] --number of cards not in the requested sets
padding[numDrawn] = padding[numDrawn] + v[numDrawn] --total number of cards requested
end
for _,i in ipairs(desired) do
local workingProb = 0
for j = 0, math.min( i[numInDeck]-i[numDrawn], handsize - padding[numDrawn] ) do
workingProb = comb(padding[numInDeck] , handsize - padding[numDrawn] - j)
for _,k in ipairs(desired) do
if k == i then
workingProb = workingProb * comb(k[numInDeck], k[numDrawn] + j)
else
workingProb = workingProb * comb(k[numInDeck], k[numDrawn])
end
end
workingProb = workingProb / comb(deckSize, handsize)
prob = prob + workingProb
end
end
return prob
end
This gives a "draw" function that will take in the size of the deck, how many cards you're drawing, and then any number of tables with the number of cards in the deck and how many you want...
print(draw(60, 7, {17,2}, {8,2}, {4,1}, {8,1}))
0.038349597671632
The formula btw is:
comb(17,2) * comb(8,2) * comb(4,1) * comb(8,1) * comb(60 -17-8-4-8 , 5 -2-2-1-1) / comb(60, 5)
That figures out the probability of getting EXACTLY 2 of a, 2 of b, 1 of c, and 1 of d, and 1 not in the previous sets. The loop sums the probabilities so you're getting AT LEAST 2,2,1,1 of the requested cards... (i.e. 3,2,1,1,0 ; 2,3,1,1,0 ; 2,2,2,1,0; 2,2,1,2,0...)
|