How would you find the number of times a coin flips heads in a row given that landing heads occurs with probability p and tails is (1-p)? Also you can’t have an upper bound on the max number of heads in a row

I’m not looking for a recursive solution nor one that involves loops etc
I want it to be an O(1) math function or something similar

I’m not looking for the probability of landing heads N times, I’m looking for if I were to start flipping coins right now, how many times would I get heads in a row (so using math.random)

My memory of probability is a bit shaky but I believe than p(heads)^n will give you your total probability. We have a 50% chance of landing a head (p = 0.5), so if we wanted the probability of landing 5 heads in a row, it would be 0.5^5 = 0.03125

So you can compare this by doing something like math.random < (0.5^n)

function log(num, base)
return math.log(num) / math.log(base)
end
function numHeads(headsProbability)
local probResult = math.random()
local heads = math.floor(log(1/(1 - probResult), 1/headsProbability))
return heads
end