Random Number Generation (OCR GCSE Computer Science)

Revision Note

Flashcards
Robert Hampton

Expertise

Computer Science Content Creator

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

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

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

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.