Practice Random Numbers in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

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

Showing a random 20 of 50 problems.

Example 1

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 2

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

Example 3

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 4

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 5

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

Example 6

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 7

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

Example 8

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 9

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 10

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

Example 11

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

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
Rolling two fair dice and summing, what is the probability the sum is 7?

Example 14

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

Example 15

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

Example 16

easy
What is the input to a pseudo-random generator that determines its starting state?

Example 17

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

Example 18

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

Example 19

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

Example 20

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