Random Number Generation (OCR GCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Random Number Generation

What is random number generation?

  • Random number generation is a programming concept that involves a computer generating a random number to be used within a program to add an element of unpredictability

  • Examples of where this concept could be used include:

    • Simulating the roll of a dice

    • Selecting a random question (from a numbered list)

    • National lottery

    • Cryptography 

Concept

OCR exam reference

Python

Random numbers

number = random(1,10)

number = random(-1.0,10.0)

import random

number = random.randint(1,10)

number = random.randint(-1.0,10.0)

Examples in Python

Random code

import random # importing random module

user = input("Enter a username: ") # asking user to enter a username
pw = input("Enter a password: ") # aksing user to enter a password

if user == "admin" and pw == "1234": # checking if the user and password are correct
  code = random.randint(1000,9999) # generating a random 4 digit code
  print("Your code is", code) # printing the code

National lottery

import random # importing random module

num1 = random.randint(1,59) # generating a random number between 1 and 59
num2 = random.randint(1,59)
num3 = random.randint(1,59)
num4 = random.randint(1,59)
num5 = random.randint(1,59)
num6 = random.randint(1,59)

print("Your lucky dip numbers are: ", num1, num2, num3, num4, num5, num6) # printing the numbers

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.