Skip to content

Course home

Random number generation in a programming language

Random number generation in a programming language

Easy
12345678
Question 2

An incomplete Python program for simulating deep-sea drone depth calibration is shown in Figure 1 below.

The program should generate a random target calibration depth between 150 and 650 meters (including 150 and 650). This will be the secret calibration depth the user has to match.

Figure 1

1      import random
2
3      print("Enter your target depth calibration value:")
4      userDepth = int(input())
5      while userDepth < 150 or userDepth > 650:
6          print("Depth outside system tolerance")
7          userDepth = int(input())
8      print("Tolerance check passed")
9      if targetDepth == userDepth:
10         print("System synchronized with targeted depth")

Write the Python code that should be used on line 2 in Figure 1 to:

  • generate a random integer between 150 and 650 inclusive
  • assign this number to the appropriate variable from the program.

You must use random.randrange(a, b) in your Python code.

Note that random.randrange(a, b) generates a random integer in the range a a\,a to bbb, starting at a a\,a but finishing one before bbb (exclusive of bbb).

[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