Results 1 to 6 of 6

Thread: [RESOLVED] Probability calculations

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Resolved [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…

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,733

    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.

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,733

    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

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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.

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,733

    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

  6. #6

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width