CS14: Random Number Generation
Using the RANDOM function, generating numbers in ranges, applications in simulations and games, and understanding seeding.
Using the RANDOM function, generating numbers in ranges, applications in simulations and games, and understanding seeding.
Random numbers are used in:
x ← RANDOM() // Returns a real number between 0 and 0.999... y ← RANDOM(10) // Returns an integer between 0 and 9 z ← RANDOM(6) // Returns an integer between 0 and 5
// Dice roll (1-6) dice ← RANDOM(6) + 1 // Coin flip (0 or 1) coin ← RANDOM(2) // 0 = heads, 1 = tails // Random percentage (1-100) percentage ← RANDOM(100) + 1 // Random day of month (1-31) day ← RANDOM(31) + 1 // Random letter position (0-25 for A-Z) letterPos ← RANDOM(26)
// Generate a random uppercase letter (A-Z) letterPos ← RANDOM(26) // 0 to 25 randomLetter ← CHR(letterPos + 65) // 65 = ASCII for 'A' OUTPUT randomLetter
OUTPUT "Rolling the dice..."
roll ← RANDOM(6) + 1
OUTPUT "You rolled a ", roll
IF roll = 6 THEN
OUTPUT "Bonus turn!"
ENDIF
encounter ← RANDOM(10) // 0 to 9
IF encounter < 3 THEN // 30% chance
OUTPUT "An enemy appears!"
ELSE
OUTPUT "The path is clear"
ENDIF
choice ← RANDOM(3) // 0, 1, or 2
CASE OF choice
0 : OUTPUT "Computer chose Rock"
1 : OUTPUT "Computer chose Paper"
2 : OUTPUT "Computer chose Scissors"
ENDCASE
// Simulate whether a car arrives at a junction (40% chance)
carArrives ← RANDOM(100) // 0-99
IF carArrives < 40 THEN
OUTPUT "Car waiting at junction"
waiting ← waiting + 1
ELSE
OUTPUT "No car"
ENDIF
// Simulate weather based on probabilities
chance ← RANDOM(100) // 0-99
IF chance < 50 THEN
OUTPUT "Sunny"
ELSEIF chance < 80 THEN
OUTPUT "Cloudy"
ELSE
OUTPUT "Rainy"
ENDIF
// Simulate customers arriving at a shop
// Average 3 customers per 10-minute period
FOR minute ← 1 TO 10
arrival ← RANDOM(10) // 0-9 (30% chance each minute)
IF arrival < 3 THEN
OUTPUT "Customer arrived at minute ", minute
queueLength ← queueLength + 1
ENDIF
NEXT minute
OUTPUT "Total customers: ", queueLength
// Generate 100 random test scores for testing
scores ← []
FOR i ← 0 TO 99
scores[i] ← RANDOM(101) // Random score 0-100
NEXT i
// Test the average function
total ← 0
FOR i ← 0 TO 99
total ← total + scores[i]
NEXT i
average ← total / 100
OUTPUT "Average of 100 random scores: ", average
RANDOM_SEED(42) // Set the seed OUTPUT RANDOM(100) // Always outputs the same number, e.g. 73 OUTPUT RANDOM(100) // Always outputs the same next number, e.g. 28 RANDOM_SEED(42) // Reset the seed OUTPUT RANDOM(100) // 73 again - same sequence! OUTPUT RANDOM(100) // 28 again
| Use Case | Seed Value | Why |
|---|---|---|
| Debugging / Testing | Fixed seed (e.g. 42) | Same "random" numbers each run for reproducible results |
| Games / Real use | System clock (auto-seed) | Different numbers each run for true unpredictability |
| Security / Cryptography | True random source (hardware) | Pseudorandom is predictable and NOT secure enough |
Q1: Write pseudo-code to simulate rolling two dice and output the total.
Q2: Write pseudo-code to generate a random number between 50 and 100 (inclusive).
Q3: Explain what a seed is and why it is useful for debugging.
Q4: Write pseudo-code that simulates a coin flip 10 times and counts the number of heads.
Q5: A game has a 1 in 8 chance of finding treasure. Write pseudo-code to simulate this.
die1 ← RANDOM(6) + 1 die2 ← RANDOM(6) + 1 total ← die1 + die2 OUTPUT "You rolled ", die1, " and ", die2, " = ", total
number ← RANDOM(51) + 50 // RANDOM(51) gives 0-50, +50 gives 50-100
heads ← 0
FOR i ← 1 TO 10
flip ← RANDOM(2) // 0 or 1
IF flip = 0 THEN
heads ← heads + 1
ENDIF
NEXT i
OUTPUT "Heads: ", heads
chance ← RANDOM(8) // 0 to 7
IF chance = 0 THEN // 1 in 8 chance
OUTPUT "You found treasure!"
ELSE
OUTPUT "No treasure here"
ENDIF
✗ Thinking RANDOM() generates truly random numbers ✓ Computer-generated 'random' numbers are pseudo-random — they use a mathematical formula starting from a seed value. They are deterministic, not truly random.
✗ Forgetting to set the range when generating random numbers ✓ RANDOM() generates a decimal between 0 and 1 by default. To get integers in a range, use RANDOM_INT(min, max) or transform: INT(RANDOM() * (max - min + 1)) + min.
✗ Not understanding that the same seed produces the same sequence ✓ Pseudo-random number generators produce the same sequence for the same seed. This is useful for testing but means numbers are predictable if the seed is known.
✗ Confusing RANDOM_INT(1,6) with RANDOM_INT(0,6) for dice simulation ✓ RANDOM_INT(1,6) simulates a die correctly (1-6). RANDOM_INT(0,6) includes 0 and 7 values, giving 7 possible outcomes instead of 6.
Write pseudo-code for a program that simulates rolling two dice and calculates the total. The program should roll 1000 times and count how many times the total is 7. Output the count and the percentage. [4 marks]
count ← 0 FOR i ← 1 TO 1000 die1 ← RANDOM_INT(1, 6) die2 ← RANDOM_INT(1, 6) total ← die1 + die2 IF total = 7 THEN count ← count + 1 ENDIF NEXT i percentage ← (count / 1000) * 100 OUTPUT 'Total of 7 appeared ' & count & ' times' OUTPUT 'Percentage: ' & percentage & '%'
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including random number generation and simulation for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including random number generation and simulation to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including random number generation and simulation, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.