CODEBHAVYA โ€ข MATHEMATICS FOR PROGRAMMING

๐ŸŽฒ Probability for Programming

Measure uncertainty, combine events correctly and understand the probability ideas behind simulation, reliability, randomized algorithms and machine-learning predictions.

๐ŸŽฏ 1. Learning Objectives

After completing this topic, you should be able to:

  • Identify experiments, outcomes, sample spaces and events.
  • Calculate probability for equally likely outcomes.
  • Use complements and addition rules.
  • Distinguish mutually exclusive, independent and dependent events.
  • Calculate conditional probability and simple expected values.
  • Use theoretical and experimental probability in programs.

๐Ÿงฉ 2. Experiment, Outcome, Sample Space and Event

Random Experiment

A repeatable process whose exact result is uncertain, such as tossing a coin or generating a random number.

Outcome

One possible result. A die roll may produce the outcome 4.

Sample Space S

The set of all possible outcomes.

Die: S = {1,2,3,4,5,6}

Event A

A selected group of outcomes from S.

Even die result: A = {2,4,6}

๐Ÿ“ 3. Basic Probability

When all outcomes are equally likely:

P(A) = Number of favourable outcomes / Total number of outcomes
0
Impossible
0.25
Unlikely
0.5
Even chance
0.75
Likely
1
Certain
Example: Probability of an even die result = 3/6 = 1/2.

๐Ÿ” 4. Complement of an Event

The complement Aโ€ฒ contains every outcome where A does not occur.

P(Aโ€ฒ) = 1 โˆ’ P(A)
Useful for โ€œat least oneโ€: P(at least one success) = 1 โˆ’ P(no successes).

โž• 5. Addition Rule

General Rule

P(A โˆช B) = P(A) + P(B) โˆ’ P(A โˆฉ B)

Subtract the overlap because it was counted twice.

Mutually Exclusive Events

P(A โˆช B) = P(A) + P(B)

These events cannot occur together, so P(A โˆฉ B) = 0.

โœ–๏ธ 6. Independent Events

Events A and B are independent when one event does not change the probability of the other.

P(A โˆฉ B) = P(A) ร— P(B)
Example: Two heads in two fair coin tosses = 1/2 ร— 1/2 = 1/4.

๐Ÿ”— 7. Conditional Probability

P(A|B) means the probability of A after knowing that B occurred.

P(A|B) = P(A โˆฉ B) / P(B),   P(B) > 0
Without replacement: probabilities usually change after each selection, so the events are dependent.

๐ŸŽฏ 8. Common Probability Models

ExperimentTotal outcomesExample eventProbability
One fair coin2Head1/2
One fair die6Number > 42/6 = 1/3
One standard card52Ace4/52 = 1/13
Two fair coins4Exactly one head2/4 = 1/2

๐Ÿงฎ 9. Counting and Probability

Permutations and combinations help count large sample spaces without listing every outcome.

Card Hands

The number of unordered 5-card hands is โตยฒCโ‚….

2,598,960 hands

Exactly k Successes

Choose which k trials succeed, then multiply the appropriate probabilities.

โฟCโ‚– pแต(1โˆ’p)โฟโปแต

๐Ÿงช 10. Probability Explorer

Select a probability model, enter the requested values and click Calculate Probability. Changing inputs hides the old result.

Select a model or example, then click Calculate Probability.

๐Ÿ’ป 11. Probability in Programming

Randomized Algorithms

Random choices can simplify algorithms, balance data structures and reduce predictable worst-case inputs.

Simulation

Programs repeat random experiments to estimate probabilities when direct calculation is difficult.

Reliability and Testing

Component failure probabilities help estimate system reliability and prioritize test scenarios.

AI and Machine Learning

Models often return class probabilities or confidence scores that support decisions under uncertainty.

๐Ÿ“Š 12. Expected Value

Expected value is the long-run average obtained by weighting every value by its probability.

E(X) = ฮฃ x ยท P(X=x)
Fair die: E(X) = (1+2+3+4+5+6)/6 = 3.5. A single roll cannot be 3.5; it is the long-run average.

๐Ÿงพ 13. Solved Problems

Problem 1: Die

Problem: Find P(number divisible by 3) on one die.

Solution: Favourable outcomes {3,6}: P = 2/6 = 1/3.

Problem 2: At Least One Head

Problem: Toss two fair coins.

Solution: P(at least one H) = 1 โˆ’ P(TT) = 1 โˆ’ 1/4 = 3/4.

Problem 3: Without Replacement

Problem: A bag has 3 red and 2 blue balls. Find P(two red).

Solution: 3/5 ร— 2/4 = 3/10.

Problem 4: Reliability

Problem: Two independent components each work with probability 0.9.

Solution: P(both work) = 0.9ร—0.9 = 0.81.

โœ๏ธ 14. Practice Problems

Solve each problem first, then use Show Solution to verify your method.

1. A fair die is rolled. Find P(odd number).

2. If P(A)=0.42, find P(Aโ€ฒ).

3. Independent events have probabilities 0.6 and 0.5. Find P(both).

4. A card is drawn from a standard deck. Find P(king or queen).

5. Three fair coins are tossed. Find P(at least one head).

๐Ÿ“ 15. Quick Revision

  • 0 โ‰ค P(A) โ‰ค 1; impossible events have probability 0 and certain events have probability 1.
  • For equally likely outcomes, probability = favourable/total.
  • Complement: P(Aโ€ฒ)=1โˆ’P(A).
  • Addition: P(AโˆชB)=P(A)+P(B)โˆ’P(AโˆฉB).
  • Independent both: P(AโˆฉB)=P(A)P(B).
  • Conditional probability: P(A|B)=P(AโˆฉB)/P(B).
  • Experimental probability approaches theoretical probability as trials increase.
  • Expected value is a weighted long-run average.

๐ŸŽฒ Probability Practice

Complete 20 questions from easy to hard. Your score, every option, correct answers and explanations appear after final submission.

Start 20-Question Test โ†’