3_2_9 Random number generation in a programming language

You should be able to:

  • Be able to use random number generation.

REVISE:

How to use random number generation

To generate numbers randomly in Python we need to import the library function known as random.

import random

We can then use this function to randomise.

Examples of this could be:

Random Choice

import random
myArray = ["ONE","TWO","THREE"]
print (random.choice(myArray))

This just picks a random item from an array.

Random Integer

import random
print (random.randint(1,20))

This will pick a random integer between 1 and 20.

Random Integer from a Range

import random
print (random.randrange(1,20))

This will pick a random integer between 1 and 20 but will not include the number 20.

Shuffle

To use shuffle you also need to import shuffle from random.

import random
from random import shuffle
myArray = ["HELLO","GOODBYE","NICE","HOW ARE YOU"]
shuffle(myArray)
print(myArray)

PROGRAMMING CHALLENGE:

Password Generator Machine

Create an app that creates a random password when a user first creates an account. The random password should:

  • Include letters and numbers
  • The password should be generated at random
  • The password should be displayed to the user

Extending the Program - Extra Mile Task

The user should have the option to:

  • Create a new random password if they aren't happy with it
  • Create a list of 3 passwords for the user to choose from - the selected password should then be displayed to the user

TEST:

  1. Download and print the test paper here: https://drive.google.com/open?id=0B5fLtQ0Xgr2PT1dRV1doQmlOX1k
  2. Try the mock test yourself.
  3. Use the 3.2.9 Walking Talking Mock below to guide you through answering the questions.

SOURCE RECOGNITION - PLEASE NOTE: The examination examples used in these walking talking mocks are samples from AQA from their non-confidential section of the public site. They also contain questions designed by TeachIT for AQA as part of the publicly available lesson materials.