A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.
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.
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.
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.
No topics depend on this one yet.