---
title: "Probability Comparison — AP CSA Definition & Exam Guide"
description: "Probability comparison uses an if statement to test Math.random() against a threshold, like < 0.05 for a 5% chance. A core Unit 2 skill for AP CSA FRQs."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/probability-comparison"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 2"
---

# Probability Comparison — AP CSA Definition & Exam Guide

## Definition

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).

## What It Is

A probability comparison is what you get when you combine a random number with an [if statement](/ap-comp-sci-a/unit-2/if-statements-and-control-flow/study-guide/IbxsFeJQ5T1FNNBEWErO "fv-autolink"). `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](/ap-comp-sci-a/key-terms/probability "fv-autolink").

This is [selection](/ap-comp-sci-a/unit-2/algorithms-with-selection-and-repetition/study-guide/42crNSZyW8IRsntk9IHe "fv-autolink") 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 It Matters

Probability comparison lives in Topic 2.3 (If Statements and Control Flow) in [Unit 2](/ap-comp-sci-a/unit-2 "fv-autolink"): Selection and Iteration, and it directly supports learning objective [AP Comp Sci A](/ap-comp-sci-a "fv-autolink") 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.

## Connections

### 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](/ap-comp-sci-a/unit-1 "fv-autolink") 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](/ap-comp-sci-a/unit-2/for-loops/study-guide/DJuLxKz6SiSAX2cEVmCt "fv-autolink") 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](/ap-comp-sci-a/key-terms/loop "fv-autolink") 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.

## On the AP 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 Takeaways

- 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.

## FAQs

### 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.

## Related Study Guides

- [2.3 If Statements and Control Flow](/ap-comp-sci-a/unit-2/if-statements-and-control-flow/study-guide/IbxsFeJQ5T1FNNBEWErO)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/probability-comparison#resource","name":"Probability Comparison — AP CSA Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/probability-comparison","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/probability-comparison#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:19.225Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/probability-comparison#term","name":"probability comparison","description":"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).","url":"https://fiveable.me/ap-comp-sci-a/key-terms/probability-comparison","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a probability comparison in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Does Math.random() == 0.5 give a 50% chance?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How is Math.random() < 0.3 different from Math.random() <= 0.3?","acceptedAnswer":{"@type":"Answer","text":"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.\""}},{"@type":"Question","name":"How do I make something happen with three different probabilities in Java?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Is probability comparison on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"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."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 2","item":"https://fiveable.me/ap-comp-sci-a/unit-2"},{"@type":"ListItem","position":4,"name":"probability comparison"}]}]}
```
