Back to 课程

Computer Science GCES OCR

0% Complete
0/0 Steps
  1. Cpu Architecture Performance And Embedded Systems Ocr
    5 主题
  2. Primary And Secondary Storage Ocr
    6 主题
  3. Data Storage And Compression Ocr
    12 主题
  4. Networks And Topologies Ocr
    6 主题
  5. Wired And Wireless Networks Protocols And Layers Ocr
    6 主题
  6. Identifying And Preventing Threats To Computer Systems And Networks Ocr
    2 主题
  7. Operating Systems And Utility Software Ocr
    2 主题
  8. Ethical Legal Cultural And Environmental Impact Ocr
    2 主题
  9. Computational Thinking Searching And Sorting Algorithms Ocr
    3 主题
  10. Designing Creating And Refining Algorithms Ocr
    5 主题
  11. Programming Fundamentals And Data Types Ocr
    5 主题
  12. Additional Programming Techniques Ocr
    7 主题
  13. Defensive Design And Testing Ocr
    6 主题
  14. Boolean Logic Diagrams Ocr
    2 主题
  15. Programming Languages And Integrated Development Environments Ides Ocr
    3 主题
  16. The Exam Papers Ocr
    2 主题
  17. Structuring Your Responses Ocr
    3 主题
课 Progress
0% Complete

Exam code:J277

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

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注