Introduction to Random Number Generation

Welcome! In this chapter, we are going to look at how we can make computer programs a bit more unpredictable. Usually, computers follow instructions exactly the same way every time. But what if you are building a game and want a different enemy to appear each time? Or what if you want to simulate a dice roll? That is where random number generation comes in!

Don't worry if this seems a bit mysterious at first. By the end of these notes, you will understand exactly how to tell a computer to "pick a number, any number."

What is Random Number Generation?

Random number generation is the process where a computer program selects a number from a specified range without any predictable pattern.

The Dice Analogy:
Think of it like rolling a standard six-sided dice. You know the result will be a number between 1 and 6, but you don't know exactly which one it will be until the dice stops rolling. In programming, we can tell the computer to "roll the dice" for us.

Why do we use it?

  • Gaming: To decide how much damage a sword hit does, or where a "power-up" appears on the screen.
  • Simulations: To model real-world events that involve chance, like the weather or the stock market.
  • Security: To create strong, unique passwords or encryption keys.

Quick Review: Random number generation allows a program to be dynamic and unpredictable, rather than doing the exact same thing every time it runs.

How to Use Random Numbers in Code

To use random numbers, you generally need to provide the computer with two pieces of information: the minimum value and the maximum value.

The Step-by-Step Process

  1. Define the Range: Decide the lowest and highest numbers you want (e.g., 1 to 10).
  2. Call the Function: Use the specific command for your programming language to generate the number.
  3. Store the Result: Assign that random number to a variable so you can use it later in your program.

Examples in Different Languages

While the exact words change, the logic is the same. Here is how you might see it in the three AQA-supported languages:

Python:
import random
my_number = random.randint(1, 6)
(This picks a whole number between 1 and 6 inclusive.)

C#:
Random dice = new Random();
int roll = dice.Next(1, 7);
(Note: In C#, the upper limit is often "exclusive," meaning dice.Next(1, 7) picks between 1 and 6.)

VB.NET:
Dim value As Integer = CInt(Int((6 * Rnd()) + 1))

AQA Pseudo-code

In your exam, you might see it written in pseudo-code like this:
number ← RANDOM_INT(1, 6)

Did you know?

Computers are actually "logical" machines, so they find it very hard to be truly random! Most use complex math to create pseudo-random numbers. They look random to humans, even though a very clever computer could technically predict them. However, for the AQA syllabus, you do not need to know the math behind how they are made—just how to use them!

Common Mistakes to Avoid

Even expert programmers make these mistakes sometimes:

  • Off-by-one errors: Forgetting if the "maximum" number is included. For example, if you want a number between 1 and 10, make sure your code doesn't stop at 9!
  • Forgetting to store the value: If you generate a random number but don't save it to a variable, you can't use it to make decisions in your code.
  • Predictable "Seeds": If you don't set up your random generator correctly, it might pick the exact same "random" numbers every time you restart the program.

Memory Aid: Just remember "RSV"Range (set your min/max), Select (call the function), and Variable (store the result)!

Summary and Key Takeaways

Key Points to Remember:

  • Random number generation is used to make programs unpredictable.
  • You must define a range (a start and end point) for the number.
  • The result should be stored in a variable.
  • You do not need to know the internal algorithms of how computers generate these numbers for your exam.

Key Takeaway: Whenever a question asks you to "simulate," "pick at random," or "create a chance," reach for your random number generation tools!