ProceduralOCR J277
DashboardCoursePracticeReviewProgressGlossary
Saved on this deviceContinue learning

Procedural · OCR J277 programming practice

Progress stays in this browser unless you export it.

Course/Robust programs/Random integer generation
J277 · 2.2.3

Unit 10 · Lesson 7

Random integer generation

Lesson progress0 / 7

Build the idea

Start with the concept and its key vocabulary.

Key idea

Translate random(min, max) into Python and use the generated value safely in a program.

OCR ERL random(min, max) generates an integer including both endpoints. Python uses import random and random.randint(min, max), which is also inclusive. Store the generated result if it is needed more than once. Normal programs usually allow a fresh sequence; these exercises read a seed so their expected results remain repeatable and can be checked exactly.

Random number

A value selected unpredictably from a specified range.

Inclusive range

A range that can produce both its minimum and maximum values.

Seed

A starting value that makes a pseudo-random sequence repeatable for testing.

Pseudo-random

Generated by an algorithm to behave like random data.

Keep these in mind

  • ERL random(1, 6) maps to Python random.randint(1, 6).
  • Both minimum and maximum can be generated.
  • Import Python's random module before calling randint.
  • Store one roll instead of generating a different value each time it is mentioned.
  • Seeding is useful for repeatable tests but is not required for ordinary random play.
  • Do not validate a generated random number as if it were user input.

Learning objective

Translate random(min, max) into Python and use the generated value safely in a program.

Exam tip

Remember that random(min, max) includes both endpoints, and translate it to random.randint(min, max), not random.random().

Common mistake

Calling random.randint twice does not reuse the first value. Assign the roll to a variable when later instructions need that same roll.

PreviousNext