video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your computing teacher.

Now, for this unit you are going to need to make sure that you've got a really good understanding of sequence.

And if you're not sure about that, then you can try doing the programming part one unit before doing this one.

Now for this lesson, you're going to need a Replit account, and a Replit account you can set up with your parent or carer's permission and that will help you code in an online environment.

You're also going to need a pen and paper so that you can make any notes and answer any of the questions that I give you.

Once you've got all of that ready, we can begin.

In this lesson, you will be able to locate information using the Python documentation, you'll import modules into your Python code, and you'll demonstrate how to generate random numbers in Python.

We're looking at randomization in this lesson.

So let's think about rolling a dice.

Why do you use a dice in real life games? Pause the video and just have a think about that.

Well, playing board games where you need to generate a random number to see how many moves you should make.

That would be one example.

You might've had something a little bit different.

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

The outcome of the resulting throw is therefore pretty random.

Where might you need to generate randomness in a computer game? Pause the video again while you have think about that.

Well, the answer might be any dice game, or to generate random words, or to generate random numbers for a guess the number game or something like that.

Maybe you had one of those.

So let's make a prediction now.

This is a completely new piece of code for you, so don't worry too much if you get this wrong.

I want you to read that code carefully and just see if you can make a prediction.

And this is your question.

What will be the output of print when this programme is executed? Have a little think about that.

Pause the video while you do, and then we'll have a look at the answer in a few seconds.

What's the answer then? So the answer is it is not possible to know the output without executing the programme.

And the other thing is, this programme will always produce the same output whenever it is executed.

Do you think that might be true or false? It's actually false.

And that's because it's actually generating a random value.

It's randomly picking something, which we'll learn more about in a moment.

If you've already done programming part one, then this will be quite obvious to you 'cause you'll have done this already, but I'm just going to have a quick demo of how to use Replit just in case you're not sure of how to use it.

So I've just set up a brand new Replit account by going to our repl.

it, and then signing up.

And again, if you're going to do this, you need to make sure you've got your parent or carer's permission before signing up to any new accounts.

So once you set that up, you'll be able to go to your homepage and this is where you just see, you can go to your repls.

So anything that you've made in the past, and you can have a look at some things there.

But if you want to just make a new Python file, then you go to plus sign, you choose Python there.

And then it always brings up a silly name, so I want you to change that because I want you to make sure that you're always using sensible names for your file.

So I'm just going to call this random because we're actually doing things to do with random in this lesson.

Then I'm going to go to create repl.

Then it takes me to my development environment.

Now, I've set up my development environment so that it's got giant text, and it's got a dark background.

But you can change your settings here, so you can change it to light if you want to, and you can reduce the size of the font or make it the same as mine if you want to.

So when you're in Python, this is your Python development environment.

You write your code in this middle pane here, and then your output for your code is always displayed on this right-hand side.

So this is a programme that you're going to be looking at in a few moments.

You're going to have to copy this code over and run it and see what happens.

Now, you're most likely going to make a syntax error like missing a square bracket, for example.

And if you do that, you'll run the code, and you'll see a syntax error appear on the right-hand side.

And it'll normally give you a clue about which line, so line three it's saying, but actually it's because something happens here that actually makes it look at what's what's being held here.

So you have to try and figure out what the syntax error is.

And you can have a good look and you can see, oh, it's because the square bracket is missing and you can check that with the code that I'm going to give you.

And then when you fix it, you can run it again.

So this programme here is importing random.

It's got some options which are red, green, and blue.

And then it's making a variable called selection, and it's assigning random.

choice to it.

And then it's printing whatever's being held in that variable on the screen there.

And every time I run it, it does something slightly different.

So you're going to have a go at investigating that as well in a few moments.

But hopefully that just helps you with Replit if you're not quite sure how to use it.

So what I want you to do so that you can figure out what this code actually does, I want you to open up your Replit account, and I want you to start typing in this code, and then run it to see what happens.

So don't forget you are going to get some of those syntax when you type this because you are copying from a screen onto another screen, so you're bound to make a few mistakes.

So always make sure that you're looking right back at this code and just seeing where you might have gone wrong.

So it's probably going to be a square bracket, a comma, or a speech mark that you miss out.

I can almost guarantee that.

And then what I want you to do once you've done that in your Replit account, I want you to see what did the programme output.

Was it the same every time the programme was run? And see if he can figure out why.

So think about those questions when you're doing it.

So pause the video now while you have a go at that.

Okay, so now we're going to think about Python documentation.

Programming languages come with documentation to help developers get the most out of the language.

And we talked about this a little bit in our part one of this unit.

Developers very rarely know all the functionality of a language.

That is true you know.

Accessing this documentation is commonplace for finding out how to perform a particular function within a language.

Now, the previous piece of code used the random module.

To find out specifically what this module does, you can visit the following link.

If you'd like to do that now, you can pause the video at this point and do that now.

Excellent.

So let's look at truly random versus pseudorandom.

So true random number generators.

These use unpredictable physical means to generate numbers such as atmospheric noise as an example.

As these are truly random, they are suitable for use in cryptography or keeping data secret.

Then you've got pseudorandom number generators.

This uses mathematical algorithms, which are computer generated and therefore highly predictable, Unless you are generating random numbers for security purposes, a pseudorandom generator would be fine, for instance in a computer game.

And we're going to have a look at that now, and you've already had a go at doing that when you've been testing it out in your Python at the moment.

So now we're going to look at this, so spot the difference.

Identify the two differences between the two snippets of code below.

And you're probably going to want to pause while you do that.

Let's take a look, see if you spotted them.

So that was the differences.

If you look at the first line of code, one of them's just got import random.

The other one's got from random import choice.

And then if you look at line three, one of them's got num equals random.

choice.

And then the other line three just has choice on its own.

So the two lines highlighted indicate the different ways in which import can be used with modules or functions.

So the code below imports the whole module and references the module and a function when you want to use it.

So this one here import random will import the entire module random.

And then when you need to access a certain function within that, you have to put the word random in front of it.

Then this one, the code below imports the specific function from a module and references them directly in the code.

So for this one, you just import the bit that you need.

And then when you go for line three, you just need to put choice rather than random.

choice.

So there's two different ways.

And using this method, so the second method that I've shown you, allows you to only import what you need which can save on space and things like that.

Now, let's see what you've remembered then from that last little bit.

Look carefully at this programme.

What is missing from line 1 to make the code work? So look at how line 2 has been written.

That'll give you a clue, and think about what was actually missing from line 1.

Pause the video while you have to think about that.

Excellent, so let's have a look.

So the bit that was missing was from random import randint.

And on line 2, the code directly states randint rather than random.

randint.

So you can tell you're only importing and using what you need.

So open a new Python file in Replit and try out this piece of code.

Run it a few times to see the random numbers that are generated.

Pause the video while you have a go at that.

Now we're going to start looking at comments.

Now this is something that you might've seen in code before, or you might not.

Commenting code is really important.

It allows you to explain what your code does to someone who may read it if you are not there to explain it.

And also to yourself in the future as an aid.

Now, what you're going to have a go at doing now is commenting the code that you've just done.

So add suitable comments to your code to explain what your previous programme did.

So there's that programme on the screen there just to remind you.

Be sure to use the most appropriate type of comment, either multi-line so that's those three apostrophes or quotes that we might call them, or a single line, which is the hashtag.

Comments generally start on the line above the line of code you're talking about.

So you can see there on that sample code on the right-hand side I've put in some spaces and this is where you would put the comments in.

So if you're only going to put it on one line, then you put the hashtag in front of it.

But if you want your comment to go over several lines, then you put those three apostrophes, then your comment, and then another three apostrophes after.

So just have a go now in Replit in your Python programme and just have a go at using those two things.

Pause the video while you have a go.

Fantastic.

So now you're going to have a go at looking at randrange, and there's a worksheet to help you with this.

So use the worksheet to investigate another function called randrange.

Have a go now, pause the video, and come back when you're done.

Excellent.

So this is the structure of randrange then.

The randrange function allows you to choose a minimum and maximum value.

Additionally, the last parameter in the expression states the step value, or what to count up in from the minimum value.

In this example, the step value 2 ensures the number output will always be even.

This could be changed to count up in any number within the range stated.

The minimum, maximum, and step values have been changed in this example.

And they're highlighted there on your screen.

How would the changes in values affect the output, and what would be a potential output of the programme? Pause the video while you have think about that.

Great, so the lowest output will be 20.

So that's what that first value in the parameter is.

And the highest output will be a 100, which is that second value.

And then anything in between these numbers will be in increments of seven.

And that's what that final one is, what we're going to count up by, so we're counting up in sevens.

So an example would be 27, 41, or 62 would be examples of valid outputs from the code.

Right, you're going to have a little task now.

I want you to look at these.

It's probably going to take you a little bit of scratching your head, a little bit of thinking about, but match the randrange statements to the potential output.

So pause the video while you have a go at that.

Excellent, so let's start looking at the answers then.

So the first one was 35.

The second one was 37.

The third one was 128.

The fourth one was 119.

And then the final one was 12.

How many did you get right? Do a thumbs up all the way if you got them all right, or about halfway.

Bit of a guessing game, bit of a matching exercise as well, but then you could have used a little bit about what you know about those values as well in the parameters to help you figure it out.

Excellent, so this lesson you've learned all about using the random module in your programmes.

And now that you know how to use random, you'll be able to make much more exciting programmes as we move on further in this unit.

If you'd like to share anything that you've done this lesson, then please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak.

I'll see you again soon for lesson two.