Lesson video

In progress...

Loading...

Hello, my name is Mr. Hogan.

I'm excited to be learning with you today.

We are going to have such a great time learning about programming iteration.

I'll be supporting you with our learning during these lessons.

I'm so pleased that you have decided to learn about randomization.

We are going to do brilliantly, so I'm so looking forward to this lesson.

I really hope you enjoy it.

I know I will.

So the outcome of this lesson is I can import the random module into my code to produce random numbers.

That's right, it's all about creating and producing random numbers in our code.

We have two key words in today's lesson.

So we have module, which is a file with pre-made code you can use in your own programmes.

And we have standard library.

This is a collection of built-in modules that come with Python.

So we'll be using these throughout the lesson, but don't worry if you don't understand them.

You could always rewind the video to come back to this point and review the definitions.

This lesson is split into two.

So we've got the first part, which is use modules from the standard library, and then the second part where we're going to enhance loops with modules and string selection.

So let's move on with the first part of the lesson.

Use modules from the standard Library.

A module is a file that contains Python code that has already been created.

A library is a collection of related modules.

Python standard Library has useful modules that you can import to enhance your programmes.

Alex is asking, "Why use modules? I can create code myself." Good question where you can save time.

There's no need to create the code yourself.

You can make your code cleaner and easier to read.

You can access features like random numbers, dates, maths functions and more.

So really good reasons why you should use modules.

Time for a quick check.

And this is a true and false check for understanding.

So a module is a file that contains Python code that has already been written.

Is this true or false? Remember, you can pause the video anytime or rewind it.

So the answer is true.

Why? Well modules are files containing pre-written Python code that can be used in other programmes.

Alex is asking another question.

Sounds like it will save me loads of time, but where can I actually find the list of all the libraries and what they do? Brilliant question again Alex.

So programmers very rarely know all the functionality of a language.

The Python documentation is available at docs.

python.

org.

It lists all the modules and standards library and explains how to use them.

So you can go there to find out more about modules in the standard library.

Alex is asking another question, how do I start saving my time and start using libraries and modules? Use a module from the standard library to generate random numbers is a great way to get started.

There's a random module you can use.

When you roll a dice by hand, the result is determined by many things such as the starting position of the dice, friction, gravity and air resistance.

The outcome of the resulting throw is therefore pretty random.

Alex is asking, "Where might you need to generate randomness in a computer game?" Well, in any dice game to generate random words for a word guessing game, to play a game like rock, paper, scissors against a computer.

This code imports the randint function from the random module.

So we can see in our code we've got from random import randit.

The function lets you generate a random whole number.

It creates a random number between one and five, including both one and five and stores it in the variable called number.

So you can see here we are passing one and five into the randint function.

And then this prints the random number to the screen on line four.

Alex is asking, "What other modules are useful to use?" Well, the time module is part of Python's standard library.

It gives you tools to work with time.

For example, adding delays or checking how long something takes to run.

You can use the time module in programmes to pause before showing the next step.

For example, in a game or quiz, measure how long something takes.

For example, it's great for performance testing and you can use it to create countdowns or timers.

All great things that the time module allows you to do in Python.

So let's have a look at some code.

So this time we are importing sleep from the time module on line one.

So this function lets you pause the programme.

This prints the message to the screen on line three, and then on line four, this pauses the programme for three seconds.

And then finally on line five, this prints the message to the screen.

Let's have a quick check.

Which function from the time module is used to pause the execution of a Python programme for a specified duration? Is it A time, B sleep, or C delay? You can pause the video at any time or rewind it to help you.

You're doing really well that you've got this far.

Take your time to think about the answer.

So let's have a look at the answer.

It is B, sleep.

Time for a practise question.

You're doing really well to get this far.

Really have a think about the answer to this question.

Take your time.

So let's go through it.

One, create a Python programme that uses the random and time modules.

The programme should A, ask the user to enter a number between 1 and 10.

B, print the user's number.

C, print "I am generating a random number.

." D, pause for two seconds before generating the number.

E, generate a random number between 1 and 10 using the random module.

And F, print "My random number is: X," where X is the random number.

So you're gonna have to pause this video I would think and continue and try to create your Python programme.

Remember you can rewind the video anytime as well to help you.

Good luck.

Let's have a look at this answer.

So this is a sample answer and you can look at this by going to oak.

link/random-time.

So on lines one and two, you can see that we've imported the randint function from the random module and we've imported the sleep function from the time module.

And as we read through this code, you can see that we are asking the user to enter a number between 1 and 10 on line four and storing it in the user number variable.

And in five, we are printing and displaying your number is whatever the user number has been entered by the user.

And then line seven, we are printing, "I am generating a random number" and eight, we're using that sleep function okay for two.

And then we are imported.

So we are using the randint function on line 10.

So creating a random number between 1 and 10 and installing it in a random number and then displaying that random number on line 11 by using the print function.

My random number is whatever the random number is.

So good luck.

Your code may slightly differ, but that's fine.

Really good.

Well done for getting this far.

I've really enjoyed doing that first part of the lesson with you.

Hopefully, you've used modules from a standard library now.

We're gonna move on to the second part, which is enhanced loops with modules and string selection.

So true random number generators use unpredictable physical means to generate numbers, such as atmospheric noise.

As these are truly random, they are suitable for use in cryptography.

So keeping data secret.

Pseudo random number generators use mathematical algorithms which are computer generated and therefore highly predictable.

Unless you are generating random numbers for security purposes, a pseudo number random generator would be fine.

For instance, in a computer game.

Let's have a quick check.

And this is a true or false check for understanding.

True random number generators use unpredictable physical means to generate numbers.

Is this true or false? Remember, you can pause the video at any time or even rewind it to remind yourself of the knowledge that you may need to answer this question.

Let's have a look at the answer.

It is true.

Well done for getting that right.

Why is it right? Well, true random number generators rely on physical phenomena that are unpredictable like we said about the atmospheric pressures and things like that.

The code below imports the whole module.

It references the module and the function when you want to use it.

So this is a bit different to what we've done before.

So on line one, we've imported the whole module random, and then on line three, we've actually said well random.

randit and then put one and five inside parenthesis.

So we've passed in one and five to give us a random number between one and five, but we didn't specify randint when we imported the random module on line one.

The code below imports the specific function from a module and the references it directly in the code.

So you can see the difference now.

On line one we've got from random import randint, and then on line three we don't have to put the random dots.

We can just put randint and then pass in the values of one and five.

So using this method allows you to only import what you need.

This code imports the random module.

The code uses the choice function from the random module to pick one character at random from the string computer and stores it in the variable random letter.

So take your time and look at this code.

Sofia is asking what if I want to select more than one random letter? I would have to run the programme again.

Great question.

Yeah, so we can see here lines three, four, and five that you know that's just does it once.

What if we want to do more than one random letter? Well you can use a for loop that repeats the block of code.

So now on lines four to seven we have a for loop.

So for number in range of five.

So it's gonna loop through five times.

The following programme uses the random module.

Characters are selected from a string of allowed characters.

The for loop repeats the password length times to build the password character by character.

Without the random module, we couldn't generate unpredictability, different characters each time.

So let's take a look at the code.

So we've obviously imported then the whole of the random module on line one, and we have the characters variable being assigned to the characters that follow on lines four and five and six.

We've got a variable which is password length, which stores the value eight.

And then on line eight, we are displaying generated password to the user.

And then line 9 to 11, this is the for loop.

So we've got four position in range, password length, random chart equals random.

choice of the characters and then reprint random_char and then end equals.

So take your time and look at this code.

Remember you can pause it at any time.

You can enhance the code by using the string module.

It gives you access to lots of useful pre-made sets of characters.

For example, all the letters of the alphabet or all the digits.

string.

ascii_letters is a shortcut for all uppercase and lowercase letters.

So we don't have to type it in ourselves.

String.

digits gives you all the number characters from 0 to 9, so we can use it now in our code.

So on line four now, we've got characters = string.

ascii_letters + string.

digits.

Let's have a quick check.

What does the string module provide that can be helpful in a password generation? Is it A, functions to encrypt the password? Is it B, pre-made sets of characters like letters and digits? Or is it C, a way to make password visible while the user types? Remember you can pause the video at any time or rewind it.

So the answer is B, pre-made sets of characters like letters and digits.

Well done on getting this far on the lesson.

I hope you got that right and if you didn't, don't worry about it, but you can rewind the video to find out why that is the answer to remind yourself.

So time for a quick practise.

One, use the random and string modules to enhance a loop that generates random passwords, which are eight characters long.

The programme should A, ask the user how many passwords they want to generate.

B, use a loop to generate the number of passwords the user has requested.

C, use random.

choice and string.

ascii_letters and string.

digits to select characters.

And D, print each password on a new line.

To help you, you'll need a for loop to repeat for each password, a for loop nested inside that to build each password, the string module to get characters and the random.

choice function to pick them.

And your text output could look something like this on the right hand side of the slide.

Remember you can pause the video at any time or rewind it to remind yourself of what we've done in this lesson to help you.

Good luck.

So a possible answer is here, and it doesn't have to be exactly the same, but hopefully you've got a very similar and you've got the output that was on the previous slide.

You can also go to oak.

link/random-string and this code will be there for you to look at.

So well done if you've got this correct, and if you haven't, don't worry, go back and take another look at the question and perhaps update your own code.

Well done.

We've come to the end of this lesson.

You've done really well.

So in summary, you can use tools from the standard library to add extra features to your programmes.

One example is the random module, which lets you create unpredictable results like generating random numbers.

By reading and using the language documentation, you can learn how to import these modules and use them correctly in your code.

So remember, you can go to the documentation, the Python documentation, and look for other modules that may help you.

So well done on getting this far and really, really so enjoyed this lesson.

Well done.