One of the first things I want to talk about is randomisation.
And this is a concept that we're super familiar with.
Randomization is really,
really important when we want to create computer programs that have a degree of
unpredictability. Now, the biggest category of that is, of course, games right?
Can you imagine if you had to play Tetris and every single time,
the block that fell down was like predictable.
You always knew that it was going to be a T and then it was going to be an L
like, there would be no fun in that right?
So how do we create this randomness for our programs? Now in nature and in our
everyday lives, it's really easy to create randomness.
If you splash some paint on a canvas, that's going to be pretty random.
If you take a look at the TV static from the analog TVs,
that's also a whole bunch of randomness there.
But when we're talking about computers,
then these machines are what we would call deterministic.
They will perform repeatable actions in a fully predictable way.
So how do we wrangle these machines that operate basically on ones and zeros to
get them to create some random numbers? Well,
there's a whole bunch of maths that can be applied to create what are called a
pseudo-random number generators.
And the one that Python uses is something called the Mersenne Twister.
And if you really want to read about it,
then you can have a look on Wikipedia and take a look under the algorithmic
detail. But to be honest, it's a little bit too much information for anybody,
unless you're really interested in these types of number generators.
But what I recommend though,
is a free video from the Khan Academy where they explain pseudorandom number
generators, and it's really well produced.
And it tells you a lot of different aspects of pseudorandom number generation.
Have a look at that
if you're interested to learn a little bit more about pseudorandom number
generators. We've talked a lot about randomness,
but lets see it in action.
Let's write some code and produce some random numbers. Head over to the Day 4
starting Repl and then go ahead and fork your own copy.
We know that we want to get hold of some random numbers,
but we don't really want to implement the Mersenne Twister ourselves because
it's horrendously complicated using lots and lots of math and probably it'll
take us months to write the code for. So how can we get random numbers?
If you think about it, it's a little bit like searching for something in Google.
The Google algorithm is horrendously complicated and it's taken them years and
years to refine and perfect to such an extent where I think Google knows me
better than myself because when I search for something,
it always gives me the most relevant results.
And if you're questioning the complexity of the Google algorithm,
just go to Bing and try searching for something and see how relevant the results
are. So they have the super complex algorithm,
which we're never going to get a chance to see, let alone understand.
And yet every day we're able to search things on Google and use this Google
algorithm to get the things that we want,
like the air speed velocity of a laden swallow.
Now we can do the same thing when we want to generate random numbers. Because
this is such frequently needed functionality,
the Python team have already created a random module.
So if you head over to askpython.com and search for the Python random module,
then you'll be able to see the documentation for this random module.
And you can see that it contains a whole bunch of functions that you can use
that allows you to generate random integers,
so random whole numbers or random floating point numbers.
So let's see how we can tap into this random module. In order to tap into the
random module, we first have to import it.
So we write import random. And now we're able to use this random module in our
main.py code.
We can start using the documentation to generate a random integer. For example,
they show us in the code snippet here that we could generate a random integer by
using the randint function, providing an a and B.
And it will return a random integer between a and b,
including both of those numbers.
And there's even a little bit of code snippet to show us how it's done.
So this line of code generates a random whole number between 100 and 200 and
including those two numbers. So it could be anything between those two numbers.
So let's see if we can implement this in our code.
Let's say that we want to create a random integer and we use the same code that
we saw in the code snippet where we say random,
that's tapping into this module here, .randint.
And then we at a set of parentheses and then we specify the start, and
then the end.
So this is basically a range of numbers that we're specifying, any integer
between 1 and 10.
And now we can go ahead and print our random integer, just like that.
And every single time I run it,
you'll notice that I'll get a different number or maybe sometimes the same,
but it's a random number between the range that I've specified.
Now I've mentioned that the random module is a Python module.
So what exactly is a module? Well,
you've seen that we've mostly been writing our code all on the same page in a
sort of script style, right?
And everything just kind of gets executed from top to bottom.
But sometimes your code will get so long because you're trying to create
something complicated and it's no longer possible to understand what's going on
in such a large piece of code.
What people will do in that case is to split the code up into individual modules
where each module is responsible for a different bit of functionality of your
program. And if you have a complex project with many, many modules,
then you could have collaboration on your project, right?
Lots of people could be working on it, each person working on a different thing.
Say if a factory was building a car,
it wouldn't make sense for one person to build the entire car from wheels,
axles to chassis, that's crazy.
Instead you have different people working on different modules like the tire
module or the chassis module or the engine module.
And then once they put everything together,
you end up with the final car or in our case, the final program.
So we know that the random module is a module that the Python team created to
make it easier for us to generate our random numbers without needing to get into
the complexities of all of the math that's required to generate pseudorandom
numbers.
But how can we create our own modules and how do modules work anyways?
Well, it's actually very simple. If you go ahead and click on the files icon,
you can see, we currently only have one file that we're writing in,
which is main.py,
the Python file. And the main.py is the entry point to our program.
This is the file that will be executed when we run our code. Now,
if I go ahead and click on this icon to add a new file,
let's say I call it my_module.py and I create a new Python file.
And inside my module, I'm going to maybe work on something else.
Let's say I want into the store, the value of PI.
Okay. I think that's all I remember.
So I know the value of PI and I want to be able to use it in my different
programs. Now that I've created my_module.py, well,
now I can use my module wherever I want just by importing it. So I could import
my_module. And now down here, I can say,
let's go ahead and print my_module.pi.
And now if I go ahead and comment out of this code and hit run,
then you'll see,
we get that value of PI that got dragged in because I imported my module,
but now we've got our code separated out into separate modules that are
responsible for different things.
And that is how the random module works as well.
So I'm going to go ahead and delete all of this stuff relating to PI,
but you can keep yours if you want, if you wanna keep it for reference.
Coming back to creating random numbers.
We've seen how we can create random whole numbers,
but what if I wanted to create a random floating point number? Well,
it tells me how to do it in the AskPython documentation.
And all I have to do is use the random module that contains the random function
and it will return a random floating-point number between 0 and 1.
But it's really important to note that in this case it doesn't actually include
1. So it goes up to like 0.9999999, but not including 1.
Let's go ahead and try that out.
So we'll say random_float = random.random
and then I set of parentheses. And this will give me output some sort of number
in the interval between 0 and 1, but not including 1.
So let's go ahead and see if it works.
First,
we print a random integer between 1 and 10 and then we print our random float
between 0 and 1. And every time we run the code,
you'll notice that we get different random numbers.
Now here's a question.
This random function always generates a number between 0 and 1 and it has
many decimal places.
But what if I wanted a random floating-point number between 0 and 5?
How might we do that? Now
it's not as complicated as you think it is and you'll be able to do it just by
looking at the code you've got here.
Have a think about it and see if you can come up with the answer.
I'll go through it with you afterwards. Pause the video now and give that a go.
All right.
So we know that this random float can be anything between
0.000000
to 0.999999..., effectively repeating right? Now
what if we were able to multiply this random number by a number?
Let's say that I multiply the random_float by 5.
Well, what if this random_float was no 0.1? Well,
0.1 times 5 would be 0.5.
But what if the random_float was 0.4? 0.4 times 5 would be 2.
Or what if it was 0.9?
0.9 times five is 4.5.
So effectively by multiplying this random_float by 5,
I will end up with random numbers,
but of course they're now floats, right? So it could be 0.000
... all the way to 4.999999
and et cetera, et cetera.
So this is how we can expand that range from 0 to 1 all the way to 0 to
5, but not including 5.
So what can you do with random numbers? Well,
if we think back to our love calculator that we made in the last lesson,
um, I mean, its kind of based on pseudo-science right?
Like just because you have a certain number of letters that exist in the word
true and love,
it doesn't mean that you're actually going to be more compatible.
I'm sorry to break anyone's heart out there. But if it's random anyways,
then why don't we just use a random number generator saving ourselves all of
that trouble from counting Ts and counting Us.
We can instead just simply say love_score = random.randint between
1 and 100. And then go ahead and just print "Your love score is",
and then let's insert our love score and turn this into an Fstring.
Let's print that. And you can see that we're getting a different love score
every single time I run this code. It's between 1 and a hundred
and you know,
who can tell that this is any different from the previous method, right?
So we can use our random number generator for this. We can use it for,
um,
creating a dice or flipping a coin or we can use it in games that we create.
If you head over to the next lesson, I've got a code challenge for you
where you are going to be building a program in order to pick from heads or
tails and help the user out when they don't have access to a coin. For all of
that and more, I'll see you on the next lesson.