Skip to content

Course home

Random number generation in a programming language

Random number generation in a programming language

Easy
12345678
Question 3

The program should simulate allocating a random loading bay by generating a random integer between 12 and 75 (including 12 and 75).

Figure 1

1      Random randGen = new Random();
2      int assignedBay;
3
4      Console.WriteLine("Enter your preferred bay (12-75):");
5      int preferredBay = Convert.ToInt32(Console.ReadLine());
6      while (preferredBay < 12 || preferredBay > 75)
7      {
8          Console.WriteLine("Invalid bay, choose between 12 and 75");
9          preferredBay = Convert.ToInt32(Console.ReadLine());
10     }
11     Console.WriteLine("Allocating random loading bay...");
12     if (assignedBay == preferredBay)
13     {
14         Console.WriteLine("Success! Your preferred bay was allocated!");
15     }

Write the C# code that should be used on line 3 in Figure 1 to:

  • generate a random number between 12 and 75 inclusive
  • assign this number to the appropriate variable from the program.

You must use randGen.Next(a, b) in your C# code.

Note: randGen.Next(a, b) generates a random integer in the range a to b starting at a but finishing one before b.

[2]
Markscheme

Random number generation in a programming language Questions

  1. GCSE
  2. /Computer Science
  3. /Random number generation in a programming language

8 exam-style questions on AQA GCSE Computer Science Random number generation in a programming language. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank