|
-
Nov 24th, 2025, 08:26 AM
#1
[RESOLVED] Probability calculations
I’m working on a probability calculator, where coin flip probability for 3 heads from 3 flips is (1/2)^3*100
What I’m not sure how to calculate is from 4 flips, what is the probability of 2 heads and 2 tails?
After that I’ll be looking for how to calculate the probabilities when throwing regular 6 sided dice…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 24th, 2025, 09:30 AM
#2
Re: Probability calculations
I would do with it with the negative binomial.
Head probability -> pH = 0.5, Tail -> pT = 0.5
/Edit: This is not correct
p = 1 - (1 - pH) * (1 - pH) * (1 - pT) * (1 - pT)
From Copilot:
Total possible outcomes:
Each flip has 2 outcomes (H or T), so for 4 flips:
Total outcomes = 2^4 = 16.
Number of favorable outcomes:
We want exactly 2 heads and 2 tails. The number of ways to arrange 2 heads among 4 flips is given by the binomial coefficient:
(4 over 2) -> 4! / (2! * 2) = 6
So there are 6 sequences like HHTT, HTHT, etc.
Probability:
Each sequence has probability:
(1/2)^4 = 1/16
Multiply by the number of favorable sequences:
Probability=6×(1/16) = 6/16 = 0.375
Last edited by Arnoutdv; Nov 24th, 2025 at 09:44 AM.
-
Nov 24th, 2025, 09:46 AM
#3
Re: Probability calculations
This is the Binomial Distribution.
P(X=k) = (n over k) * p^k * (1 - p)^(n-k)
Where
(n over k) = number of combinations (n choose k)
p = probability of success (for a fair coin, p=0.5)
n = number of trials
k = number of successes
-
Nov 24th, 2025, 10:11 AM
#4
Re: Probability calculations
Thanks…
I understand most of that, except the favourable outcomes calculation. How would I scale that, for example 6 flips and a wanted outcome of 4 heads, 2 tails?
Edit: I was referring to post #2
Last edited by .paul.; Nov 24th, 2025 at 10:14 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 24th, 2025, 10:33 AM
#5
Re: Probability calculations
The p would be: 1/6
Each roll has 6 possible outcomes, so for 4 rolls -> 6^4 = 1296 possible outcomes
A generated function:
Code:
Imports System
Module DiceProbability
' Function to calculate factorial
Private Function Factorial(n As Integer) As Double
Dim result As Double = 1
For i As Integer = 1 To n
result *= i
Next
Return result
End Function
' Function to calculate n choose k (binomial coefficient)
Private Function BinomialCoefficient(n As Integer, k As Integer) As Double
Return Factorial(n) / (Factorial(k) * Factorial(n - k))
End Function
' Function to calculate probability
Public Function ProbabilityOfExactHits(nRolls As Integer, targetHits As Integer, sides As Integer) As Double
Dim p As Double = 1.0 / sides ' Probability of rolling the target number
Dim q As Double = 1.0 - p ' Probability of NOT rolling the target number
Dim combinations As Double = BinomialCoefficient(nRolls, targetHits)
Dim probability As Double = combinations * Math.Pow(p, targetHits) * Math.Pow(q, nRolls - targetHits)
Return probability
End Function
Sub Main()
Dim rolls As Integer = 4
Dim hits As Integer = 2
Dim sides As Integer = 6
Dim result As Double = ProbabilityOfExactHits(rolls, hits, sides)
Console.WriteLine($"Probability of getting {hits} times a 5 in {rolls} rolls: {result:P4}")
End Sub
End Module
-
Nov 24th, 2025, 10:43 AM
#6
Re: Probability calculations
Thanks. I’ll try that and see how I can use it. I understand all of post #2 except the binomial coefficient (I must’ve missed school that year )
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|