Probability comparison in AP Computer Science A

In AP Computer Science A, a probability comparison is a conditional that tests a random value against a threshold to make an event happen a set percentage of the time, like if (Math.random() < 0.05) creating a 5% chance, since Math.random() returns a double from 0 (inclusive) up to 1 (exclusive).

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is probability comparison?

A probability comparison is what you get when you combine a random number with an if statement. Math.random() hands you a double somewhere in the range [0, 1), meaning 0 is possible but 1 is not. Every value in that range is equally likely. So if you write if (Math.random() < 0.4), you're slicing off the bottom 40% of that range. The Boolean expression comes out true 40% of the time, and the body of the if runs with exactly that probability.

This is selection in action (EK 2.3.A.2). The Boolean expression Math.random() < 0.4 evaluates to true or false, and the if statement uses that value to decide whether to execute a code segment. Stack the thresholds with else-if and you can split probability across several outcomes. For example, < 0.5 catches a 50% case, else if (rand < 0.8) catches the next 30%, and else mops up the final 20%. One critical habit: call Math.random() once, store it in a variable, then compare. Calling it again in each branch generates a brand new random number every time and wrecks your probabilities.

Why probability comparison matters in AP® Computer Science A

Probability comparison lives in Topic 2.3 (If Statements and Control Flow) in Unit 2: Selection and Iteration, and it directly supports learning objective AP Comp Sci A 2.3.A, which asks you to develop code for branching logic and determine what that code does. It's also one of the most common ways the AP exam makes selection feel real. Instead of abstract conditions, you're coding things like "this method returns true 25% of the time" or "the simulation rains on 30% of days." If you can't translate a percentage into a threshold comparison, a whole family of FRQ subparts becomes unwritable. It's a small pattern, but it shows up everywhere simulations do.

How probability comparison connects across the course

If-Else and Multi-Way Selection (Unit 2)

A single threshold gives you a two-way split, but else-if chains let you divide [0, 1) into several mutually exclusive slices. Cumulative thresholds like < 0.5, < 0.8, else are just probability comparison scaled up to three or more outcomes.

Math.random() and Expressions (Unit 1)

The whole trick depends on knowing what Math.random() actually returns, a double in [0, 1) with every value equally likely. Probability comparison is where that Unit 1 method call meets Unit 2 selection logic for the first time.

Boolean Expressions (Unit 2)

Math.random() < 0.05 is just a relational Boolean expression like any other. Once you see that, probability comparison stops being a special trick and becomes ordinary if-statement logic with a random left-hand side.

Iteration and Simulation (Unit 2)

Put a probability comparison inside a loop and you get a simulation, like flipping a coin 1,000 times and counting heads. This loop-plus-random-condition combo is the backbone of simulation-style exam questions.

Is probability comparison on the AP® Computer Science A exam?

On the multiple-choice section, probability comparison shows up as "determine the result" questions, exactly what AP Comp Sci A 2.3.A asks. You might see code with Math.random() and be asked what percentage of the time a statement executes, or which threshold produces a desired probability. Watch for traps like calling Math.random() twice (two independent random values, not one) or off-by-a-slice errors like using <= instead of <. On free-response questions, Methods and Control Structures prompts regularly require you to write this pattern yourself, with specs like "returns true 40% of the time." The clean answer is one Math.random() call compared against the decimal form of the percentage. No released FRQ uses the phrase "probability comparison" verbatim, but the pattern itself is standard FRQ material.

Probability comparison vs Equality comparison with a random value

Writing if (Math.random() == 0.05) does not create a 5% chance. Math.random() returns a double from a huge range of possible values, so hitting exactly 0.05 is essentially impossible and the branch almost never runs. Probability comparison uses < (or another range check) because probability comes from the size of the slice you capture, not from matching one specific value. To get a 5% chance, you want Math.random() < 0.05, which captures the entire bottom 5% of the range.

Key things to remember about probability comparison

  • Math.random() returns a double from 0 (inclusive) up to 1 (exclusive), and every value in that range is equally likely.

  • Comparing Math.random() to a threshold creates a probability, so Math.random() < 0.4 is true exactly 40% of the time.

  • Always use a range comparison like <, never ==, because a random double will almost never equal one exact value.

  • Call Math.random() once and store the result in a variable if you need to check it against multiple thresholds, or each branch will use a different random number.

  • Else-if chains with cumulative thresholds (like < 0.5, then < 0.8, then else) split probability across three or more mutually exclusive outcomes.

  • This pattern supports learning objective AP Comp Sci A 2.3.A, since the if statement's Boolean expression is what turns a random number into a branching decision.

Frequently asked questions about probability comparison

What is a probability comparison in AP Computer Science A?

It's an if statement that compares a random value to a threshold so code runs a set percentage of the time. For example, if (Math.random() < 0.05) executes its body with a 5% probability because it captures the bottom 5% of Math.random()'s [0, 1) range.

Does Math.random() == 0.5 give a 50% chance?

No. Math.random() returns a double, and the odds of it equaling exactly 0.5 are essentially zero, so that condition almost never fires. For a 50% chance you need a range check like Math.random() < 0.5.

How is Math.random() < 0.3 different from Math.random() <= 0.3?

In practice, almost nothing. Both give effectively a 30% chance because the probability of landing on exactly 0.3 is vanishingly small. The convention on the AP exam is to use <, which maps cleanly to "the bottom 30% of the range."

How do I make something happen with three different probabilities in Java?

Call Math.random() once, store it, then use cumulative thresholds in an else-if chain. For 50/30/20 odds, check rand < 0.5 first, then else if (rand < 0.8), then else, which catch slices of size 0.5, 0.3, and 0.2.

Is probability comparison on the AP CSA exam?

Yes. It falls under Topic 2.3 (If Statements and Control Flow) and learning objective AP Comp Sci A 2.3.A. Multiple-choice questions ask you to determine how often code executes, and free-response questions often require writing methods that return true with a given probability.