GCSE Revision Aid: This resource is designed to support your revision and may contain errors. If you find a discrepancy with your class teaching, your teacher is correct — please let us know at gcserevise@scott.scottrix.co.uk.

CS14: Random Number Generation

Foundation Higher AQAEdexcelOCREduqasCCEA

Using the RANDOM function, generating numbers in ranges, applications in simulations and games, and understanding seeding.

Fastmail

📋 What is Random Number Generation?

Random number generation produces numbers that appear unpredictable. In computing, most random numbers are actually pseudorandom - they are generated by a deterministic algorithm but appear random for practical purposes.

Random numbers are used in:

🎲 The RANDOM Function

RANDOM() generates a random number. In most pseudo-code specifications, it returns a random real number between 0 and 1 (not including 1). Some specifications use RANDOM(max) to return a random integer between 0 and max-1.
Basic RANDOM Usage
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
Common RANDOM Forms:
RANDOM() - real number between 0 and 1
RANDOM(n) - integer between 0 and n-1
Check your exam board's specific pseudo-code guide!

📊 Generating Numbers in a Range

To generate a random integer in a range from min to max (inclusive), you need to scale and shift the random number. The formula depends on which RANDOM function your specification uses.
Formula for Range (if RANDOM(n) gives 0 to n-1):
number ← RANDOM(max - min + 1) + min

Example: Dice roll (1 to 6):
roll ← RANDOM(6) + 1
RANDOM(6) gives 0-5, adding 1 gives 1-6

Example: Random number 10 to 20:
number ← RANDOM(11) + 10
RANDOM(11) gives 0-10, adding 10 gives 10-20
Generating Specific Ranges
// 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)
Generating a Random Letter
// Generate a random uppercase letter (A-Z)
letterPos ← RANDOM(26)     // 0 to 25
randomLetter ← CHR(letterPos + 65)   // 65 = ASCII for 'A'
OUTPUT randomLetter

🎮 Uses in Games

Random numbers are essential in games to create unpredictable and varied experiences. Without randomness, games would always play out exactly the same way.
Dice Game
OUTPUT "Rolling the dice..."
roll ← RANDOM(6) + 1
OUTPUT "You rolled a ", roll

IF roll = 6 THEN
    OUTPUT "Bonus turn!"
ENDIF
Random Enemy Encounter
encounter ← RANDOM(10)    // 0 to 9
IF encounter < 3 THEN        // 30% chance
    OUTPUT "An enemy appears!"
ELSE
    OUTPUT "The path is clear"
ENDIF
Rock, Paper, Scissors
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

🔬 Uses in Simulations

Simulations use random numbers to model real-world events that have an element of chance. This allows us to study complex systems without having to observe them in reality.
Traffic Light Simulation
// 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
Weather Simulation
// 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
Queue Simulation
// 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

🧪 Uses in Testing

Random test data generation can quickly produce large volumes of test data. This is useful for stress testing and finding edge cases that a programmer might not think of.
Generating Random Test Scores
// 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

🌱 Seeding Random Numbers

A seed is a starting value for the random number generator. The same seed always produces the same sequence of "random" numbers. This is useful for testing and debugging because results become reproducible.
How Seeding Works
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
Important: Computers generate pseudorandom numbers using algorithms. They are not truly random because the sequence is determined by the seed. For most programming purposes, pseudorandom is sufficient. For security, true randomness from hardware is needed.

❓ Practice Questions

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.

✅ Answers

  1. die1 ← RANDOM(6) + 1
    die2 ← RANDOM(6) + 1
    total ← die1 + die2
    OUTPUT "You rolled ", die1, " and ", die2, " = ", total
  2. number ← RANDOM(51) + 50    // RANDOM(51) gives 0-50, +50 gives 50-100
  3. A seed is a starting value for the random number generator. The same seed always produces the same sequence of pseudorandom numbers. This is useful for debugging because you can reproduce the same "random" results each time you run the program, making it easier to find and fix errors.
  4. 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
  5. chance ← RANDOM(8)     // 0 to 7
    IF chance = 0 THEN          // 1 in 8 chance
        OUTPUT "You found treasure!"
    ELSE
        OUTPUT "No treasure here"
    ENDIF

🎯 Exam Tips

⚠️ Common Errors

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

✍️ Model Answer

Full-Mark Response

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 & '%'

📊 AO Deep Dive

Assessment Objective Analysis

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.

📝 Exam Technique

GCSE Computer Science Exam Tips:
Use RANDOM_INT(min, max) for integer ranges; RANDOM() for decimals 0-1. Always specify the correct inclusive range. For a die: RANDOM_INT(1,6). For a coin: IF RANDOM_INT(0,1) = 0 THEN heads. For simulations, use a FOR loop with the number of trials. Count results and calculate percentages. Mention that computer random numbers are pseudo-random when discussing limitations.

📝 Exam Questions by Topic

🎬 Video Resources

Share this page

Ready to ace your GCSE Computer Science exams?

Get the best revision books and guides to boost your grades.