A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Random Numbers and Simulation

College Depth 84 in the knowledge graph ☐ I know this ☆ Set as goal
332prerequisites beneath it
See this on the map →
Conditional StatementsFor Loops+2 more→
random pseudorandom simulation monte-carlo modeling AP-CSP

Core Idea

Programs get random-looking values from a pseudorandom number generator, a deterministic algorithm whose output passes many statistical tests for randomness. Library calls such as Python's `random.randint(1, 6)` or Java's `Math.random()` let a program roll dice, shuffle cards or pick random points. A simulation uses such values to imitate a real process many times, so the program can estimate outcomes that are hard to compute directly or too slow, costly or dangerous to test in the real world.

How It's Best Learned

Begin with a dice-rolling program and tally the results of 60 rolls, then 60,000, to see the proportions settle near 1/6. Ask students to predict, then measure, the effect of a simplifying assumption, such as treating every customer as arriving at an even rate. Show that setting a seed makes a "random" run repeat exactly, which links the idea of pseudorandomness to debugging.

Common Misconceptions

Explainer

Many programs need values that cannot be predicted in advance: a game rolls dice, a quiz shuffles questions, a model guesses where a storm might go. Every major language provides a generator for this. In Python, `import random` gives `random.randint(1, 6)`, which returns an integer from 1 to 6 inclusive, and `random.random()`, which returns a float from 0.0 up to but not including 1.0. In Java, `Math.random()` returns a `double` in the same half-open range, so `(int) (Math.random() * 6) + 1` simulates a die.

These values are pseudorandom. The generator is an ordinary algorithm that turns an internal state into the next number and updates the state. Its output looks random and passes many statistical tests, but it is fully determined by its starting state, called the seed. Calling `random.seed(42)` before a run makes the program produce the same sequence every time. That is useful for testing. It also means these generators should not be used for passwords or encryption keys; security code uses special cryptographic generators instead.

A simulation uses random values to imitate a process. To estimate the chance that two dice sum to 7, roll two simulated dice 100,000 times and count. The fraction will be close to 1/6, about 0.167. A classic example estimates pi: pick random points in a 1-by-1 square and count how many land inside the quarter circle of radius 1. That fraction approaches pi/4. Methods like this are called Monte Carlo methods.

Two facts govern every simulation. First, the answer is an estimate. Different runs give slightly different results, and more trials usually shrink the spread. Second, a simulation is only as good as its model. A traffic simulation must assume how fast cars arrive and how drivers react. Those simplifications make the program possible, but if they are wrong, the output is wrong too, however many trials you run.

Simulations are still widely used because they let people explore situations that are expensive, slow or unsafe to test directly, such as crowd evacuations, disease spread or new road layouts. The skill is to state the assumptions openly, run enough trials, and compare results against real data whenever possible.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → While Loops → For Loops → Random Numbers and Simulation

Longest path: 85 steps · 332 total prerequisite topics

Prerequisites (4)

Leads To (0)

No topics depend on this one yet.