Random number generation
Random Number Generation
Programs often need to produce unpredictable values - to simulate a dice roll, shuffle a playlist, generate a test question, or create a game event. Random number generation provides a way to obtain a number within a specified range each time the program runs, without knowing the result in advance.
All three languages generate numbers using a function or method that accepts a lower bound and an upper bound. The critical difference is how the upper bound is treated: Python's randint() includes both bounds; VB.NET's and C#'s Next() excludes the upper bound, so you must add 1 to get an inclusive range.
| Language | Import / setup | Inclusive range a to b | Upper bound behaviour |
|---|---|---|---|
| Python | import random | random.randint(a, b) | Inclusive (b is possible) |
| VB.NET | Dim rnd As New Random() | rnd.Next(a, b + 1) | Exclusive (pass b+1) |
| C# | Random rnd = new Random(); | rnd.Next(a, b + 1) | Exclusive (pass b+1) |
Worked Examples
A standard six-sided die produces a whole number between 1 and 6 inclusive. This is the most common introductory use of random number generation.
AQA pseudocode: roll ← RANDOM_INT(1, 6)
# Python -- randint(a, b) is INCLUSIVE of both a and b
import random
roll = random.randint(1, 6) # any of 1, 2, 3, 4, 5, 6 is possible
print("You rolled:", roll)
' VB.NET -- Next(a, b) is EXCLUSIVE of b, so pass 7 to include 6
Dim rnd As New Random()
Dim roll As Integer = rnd.Next(1, 7) ' range 1..6 inclusive
Console.WriteLine("You rolled: " & roll)
// C# -- Next(a, b) is EXCLUSIVE of b, so pass 7 to include 6
Random rnd = new Random();
int roll = rnd.Next(1, 7); // range 1..6 inclusive
Console.WriteLine("You rolled: " + roll);
Random numbers are not limited to 1-6. Here the program generates a mock quiz score between 0 and 100 inclusive - a custom range chosen to match the problem.
AQA pseudocode: score ← RANDOM_INT(0, 100)
# Python -- custom range 0 to 100 inclusive
import random
score = random.randint(0, 100)
print("Quiz score:", score)
' VB.NET -- custom range 0 to 100 inclusive (pass 101 as upper bound)
Dim rnd As New Random()
Dim score As Integer = rnd.Next(0, 101) ' 0..100 inclusive
Console.WriteLine("Quiz score: " & score)
// C# -- custom range 0 to 100 inclusive (pass 101 as upper bound)
Random rnd = new Random();
int score = rnd.Next(0, 101); // 0..100 inclusive
Console.WriteLine("Quiz score: " + score);
Random values become much more useful when combined with selection. Here the program rolls a die and uses an IF statement to decide whether the player wins - demonstrating how random number generation drives program behaviour.
AQA pseudocode:
roll ← RANDOM_INT(1, 6)
IF roll = 6 THEN
OUTPUT "You win!"
ELSE
OUTPUT "You rolled " + roll + ". Try again."
ENDIF
# Python -- use random result in a decision
import random
roll = random.randint(1, 6)
if roll == 6:
print("You win!")
else:
print("You rolled", roll, ". Try again.")
' VB.NET -- use random result in a decision
Dim rnd As New Random()
Dim roll As Integer = rnd.Next(1, 7) ' 1..6 inclusive
If roll = 6 Then
Console.WriteLine("You win!")
Else
Console.WriteLine("You rolled " & roll & ". Try again.")
End If
// C# -- use random result in a decision
Random rnd = new Random();
int roll = rnd.Next(1, 7); // 1..6 inclusive
if (roll == 6)
Console.WriteLine("You win!");
else
Console.WriteLine("You rolled " + roll + ". Try again.");
Key Takeaways
- Python's
random.randint(a, b)includes both a and b; VB.NET's and C#'srnd.Next(a, b)excludes b, so passb + 1to get an inclusive range. - Always import or initialise the random module or object before calling the function:
import randomin Python;Dim rnd As New Random()in VB.NET;Random rnd = new Random();in C#. - The result is a whole number (integer) stored in a variable and used like any other integer - in output, arithmetic, or selection statements.
- You are not required to understand how pseudo-random numbers are generated - only how to call the function correctly and use the result.