Random Numbers Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Random Numbers.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Random numbers are values chosen without a predictable pattern, or in computing, values that imitate that behavior closely enough for practical use. Computers often generate pseudo-random numbers using algorithms that look random even though they are created deterministically.

The computer follows a rule, but the outputs are mixed enough to behave like random choices for many tasks.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Random numbers help models include uncertainty, variation, and chance.

Common stuck point: Pseudo-random does not mean useless. It means the values come from an algorithm instead of true physical randomness.

Sense of Study hint: Ask what range of values is possible, whether each outcome should be equally likely, and whether repeating the same starting seed should reproduce the same sequence.

Worked Examples

Example 1

medium
To simulate a 25% chance event using `random.random()` returning rโˆˆ[0,1)r \in [0,1), what condition should you check?

Answer

r<0.25r < 0.25

First step

1
A uniform float in [0,1)[0,1) lands below 0.25 with probability 0.25.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan โ€” every worked solution, all subjects

Example 2

medium
You have a uniform float rโˆˆ[0,1)r \in [0,1). Write a formula to map it to integers 1..101..10 inclusive.

Example 3

medium
Estimating an integral by Monte Carlo: 1200 random points in a 4ร—44 \times 4 square; 750 land under a curve y=f(x)y = f(x). Estimate the area under the curve.

Example 4

hard
A simulation runs 10,000 fair coin flips. By the standard deviation npq\sqrt{npq}, what is the approximate spread of heads counts?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
A fair die roll picks uniformly from 1 to 6. What is the probability of rolling a 4?

Example 2

easy
`random.randint(1, 10)` returns an integer. How many distinct values can it return (inclusive)?

Example 3

easy
A pseudo-random generator is seeded with the same seed twice. How do the two output sequences compare?

Example 4

easy
To simulate a fair coin flip, what range of integers and rule would you use?

Example 5

easy
Are pseudo-random numbers truly unpredictable to someone who knows the algorithm and seed?

Example 6

easy
Over many fair die rolls, roughly what fraction should be even numbers (2, 4, 6)?

Example 7

easy
A program needs a random float in [0,1)[0,1). Which is a correct interpretation of 0.5 from such a generator?

Example 8

easy
To pick a random element from a list of 5 items (indices 0-4), what index range should the random integer cover?

Example 9

medium
Rolling two fair dice and summing, what is the probability the sum is 7?

Example 10

medium
A simulation expects a uniform distribution over 1-100 but the generated values cluster near 1-10. What likely went wrong, by the common mistake?

Example 11

medium
You want a random integer from 1 to 6 but only have a generator for 0 to 1 floats. Give a formula using the float rr.

Example 12

medium
A test must be reproducible but uses random data. How do you get fresh-looking randomness yet identical runs each time?

Example 13

medium
Estimating ฯ€\pi by throwing random points in a unit square and counting those inside the quarter circle: if 785 of 1000 land inside, estimate ฯ€\pi.

Example 14

medium
A shuffle uses `randint(0, n)` to pick swap indices for an array of length nn. Why is this buggy?

Example 15

medium
A weather model uses random numbers so each run differs slightly. What aspect of reality are the random numbers representing?

Example 16

challenge
A flawed PRNG only outputs even numbers from 0 to 9 (0,2,4,6,8). A student uses it to simulate a fair 10-sided die. List two distribution flaws this causes.

Example 17

challenge
A simulation seeds with the current time in seconds and starts 1000 parallel runs in the same second. Why might many runs be identical, and what is the fix?

Example 18

challenge
You need an unbiased random bit but your coin is biased (P(heads)=pp, pโ‰ 0.5p \ne 0.5). Describe von Neumann's trick to extract a fair bit.

Example 19

medium
You want a random integer from 0 to 99 using `randint`. What call gives a uniform value, and what is the probability of any specific number?

Example 20

medium
A simulation runs 6000 fair die rolls. Roughly how many 3's should you expect, and will the count be exactly that?

Example 21

easy
A fair 20-sided die is rolled. What is the probability of rolling a 13?

Example 22

easy
`random.randint(1, 50)` returns an integer. How many distinct values can it return (inclusive)?

Example 23

easy
You roll a fair die. What is the probability of rolling a number greater than 4?

Example 24

easy
True or false: a PRNG with a known seed produces predictable output to anyone with the algorithm.

Example 25

easy
A random float in [0,1)[0,1) is multiplied by 100 and floored. What integer range does the result cover?

Example 26

easy
A fair coin is flipped 4 times. What is the probability of getting exactly 4 heads?

Example 27

medium
Two dice are rolled and summed. What is the probability the sum is 10?

Example 28

medium
Estimating ฯ€\pi with random points in a unit square: 318 of 400 land inside the quarter circle. Estimate ฯ€\pi.

Example 29

medium
You want a random integer from 5 to 15 using `randint`. What call should you use, and how many possible outputs are there?

Example 30

medium
To simulate a biased coin with P(heads)=0.7P(\text{heads}) = 0.7, what comparison on a uniform rโˆˆ[0,1)r \in [0,1) gives heads?

Example 31

medium
A simulation gives results that change every run. To debug a specific failing case, what is the standard fix?

Example 32

medium
Rolling 3 fair dice, what is the probability all three show the same number?

Example 33

medium
A fair 4-sided die outputs {1,2,3,4}\{1,2,3,4\}. What is the expected value of one roll?

Example 34

medium
A list of 8 elements (indices 0-7) needs a random element. Which call gives a correct index?

Example 35

medium
You flip a fair coin 100 times. About how many heads do you expect, and is the count usually exactly that?

Example 36

hard
Rolling two dice repeatedly until the sum equals 7, what is the probability of getting it on the first try?

Example 37

hard
A PRNG outputs values 0..9 but produces 0 twice as often as any other digit. Compute P(0)P(0) and P(7)P(7).

Example 38

hard
Drawing 2 cards from a 52-card deck without replacement, what is the probability both are hearts?

Example 39

hard
A simulation needs 1 million random numbers per run and uses a 32-bit PRNG. Why is period (length before sequence repeats) a concern?

Example 40

hard
To sample without bias from a list of 100, a programmer uses โŒŠ100โ‹…rโŒ‹\lfloor 100 \cdot r \rfloor with uniform rโˆˆ[0,1)r \in [0,1). Why is this correct, and what would โŒŠ100โ‹…rโŒ‹+1\lfloor 100 \cdot r \rfloor + 1 give?

Example 41

challenge
Using rejection sampling, you generate uniform rโˆˆ[0,1)r \in [0,1) and accept if r<0.4r < 0.4. Out of 1000 attempts, about how many are accepted, and what does the accepted set look like?

Example 42

medium
A fair 8-sided die is rolled 800 times. Roughly how many 5's do you expect?

Background Knowledge

These ideas may be useful before you work through the harder examples.

simulation