Skip to content

Course home

Random number generation in a programming language

Random number generation in a programming language

Easy
12345678
Question 4

A program simulates a smart thermostat's automatic calibration routine where a target temperature is randomly selected.

The program must generate a random integer between -5 and 35 (including both -5 and 35 Celsius). This will represent the target test temperature.

Write the VB.NET code that should be used on line 3 of the program below to:

  • generate a random integer between -5 and 35 inclusive
  • assign this number to the appropriate variable from the program.

You must use rng.Next(a, b) in your VB.NET code.

Note: rng.Next(a, b) generates a random integer starting at a up to, but not including, b.

1      Dim rng As New Random()
2      Dim targetTemp As Integer
3
4      Console.WriteLine("Enter a test temperature in Celsius (-5 to 35):")
5      Dim testTemp As Integer = Console.ReadLine()
6      While testTemp < -5 Or testTemp > 35
7          Console.WriteLine("Out of system calibration range.")
8          testTemp = Console.ReadLine()
9      End While
10     Console.WriteLine("Calibration point recorded.")
11     If targetTemp = testTemp Then
12         Console.WriteLine("Target matched exactly during test!")
13     End If
[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