Random Number Generation (AQA GCSE Computer Science): Revision Notes
Random number generation
What are random numbers in programming?
Programming languages have built-in functions that can create random numbers for you. This is really useful when you're making games, simulations, or any programme where you need unpredictable values. For example, you might want to generate a random score between 1 and 10 when a player completes a level in a game.
Random number generation is one of the most commonly used features in programming, especially in game development, data simulation, and testing scenarios where you need to create varied, unpredictable outcomes.
How random number generation works
When you want to generate a random number, you typically need to specify the range - this means telling the computer the smallest and largest numbers you want it to choose from. The computer will then pick any whole number within that range.
Usually, you provide two parameters (pieces of information) to the random number function:
- The lower limit (smallest possible number)
- The upper limit (largest possible number)
The function then returns a random whole number somewhere between these two limits.
Understanding Parameters: Think of parameters as the "instructions" you give to the function. Just like giving someone directions, you need to be specific about what you want - in this case, the minimum and maximum values for your random number.
Random number generation in pseudocode
In AQA pseudocode, we use a special subroutine called RANDOM_INT to generate random numbers. Here's how it works:
RANDOM_INT(lowest_number, highest_number)
Let's look at some examples:
RANDOM_INT(1, 5) // Picks a random number between 1 and 5
RANDOM_INT(20, 30) // Picks a random number between 20 and 30
Worked example
Here are two different ways you can use the RANDOM_INT subroutine:
Worked Example: Using RANDOM_INT in Different Ways
Method 1: Storing in a variable
x ← RANDOM_INT(1, 10)
This line stores a random number between 1 and 10 in a variable called x. You can use this variable later in your programme.
Method 2: Direct output
OUTPUT RANDOM_INT(20, 50)
This line immediately displays a random number between 20 and 50 to the user without storing it in a variable.
When to use each method:
- Use Method 1 when you need to use the same random number multiple times
- Use Method 2 for one-time use when you just need to display the result
Random numbers in real programming languages
Different programming languages have their own ways of generating random numbers, but they all do the same basic job:
Python uses this syntax to pick a random number between 1 and 100:
import random
print(random.randint(1, 100))
VB.Net uses this approach:
Dim r As Random = New Random
Console.WriteLine(r.Next(1, 101))
C# looks very similar to VB.Net:
Random r = new Random();
Console.WriteLine(r.Next(1, 101));
Syntax Differences: Notice that while the syntax looks different in each language, they're all doing the same thing - creating random numbers within a specific range. The core concept remains identical across all programming languages.
Exam tips
Critical Exam Points:
- Remember that RANDOM_INT takes two parameters: the lowest number first, then the highest number
- In pseudocode, both the lower and upper limits are usually included in the possible results
- You can either store the random number in a variable or use it directly in an OUTPUT statement
- Different programming languages may have slightly different syntax, but the concept remains the same
Remember!
Key Points to Remember:
- Random number generation is used to create unpredictable values in programmes, especially useful for games and simulations
- The RANDOM_INT(x, y) subroutine in pseudocode generates a random whole number between x and y
- You need to provide two parameters: the lowest possible number and the highest possible number
- You can either store the result in a variable or use it directly in your programme
- Real programming languages like Python, VB.Net, and C# have their own syntax but work on the same principle