Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,250 --> 00:00:03,910
One of the first things I want to talk about is randomisation.
2
00:00:04,390 --> 00:00:07,930
And this is a concept that we're super familiar with.
3
00:00:08,530 --> 00:00:10,690
Randomization is really,
4
00:00:10,690 --> 00:00:14,890
really important when we want to create computer programs that have a degree of
5
00:00:14,950 --> 00:00:19,540
unpredictability. Now, the biggest category of that is, of course, games right?
6
00:00:19,840 --> 00:00:23,440
Can you imagine if you had to play Tetris and every single time,
7
00:00:23,440 --> 00:00:25,780
the block that fell down was like predictable.
8
00:00:26,020 --> 00:00:30,430
You always knew that it was going to be a T and then it was going to be an L
9
00:00:30,460 --> 00:00:32,440
like, there would be no fun in that right?
10
00:00:32,770 --> 00:00:37,770
So how do we create this randomness for our programs? Now in nature and in our
11
00:00:38,950 --> 00:00:42,460
everyday lives, it's really easy to create randomness.
12
00:00:42,760 --> 00:00:46,480
If you splash some paint on a canvas, that's going to be pretty random.
13
00:00:46,600 --> 00:00:51,340
If you take a look at the TV static from the analog TVs,
14
00:00:51,610 --> 00:00:53,890
that's also a whole bunch of randomness there.
15
00:00:54,370 --> 00:00:56,440
But when we're talking about computers,
16
00:00:56,740 --> 00:01:00,670
then these machines are what we would call deterministic.
17
00:01:00,910 --> 00:01:05,910
They will perform repeatable actions in a fully predictable way.
18
00:01:06,400 --> 00:01:11,400
So how do we wrangle these machines that operate basically on ones and zeros to
19
00:01:11,920 --> 00:01:15,280
get them to create some random numbers? Well,
20
00:01:15,280 --> 00:01:19,150
there's a whole bunch of maths that can be applied to create what are called a
21
00:01:19,180 --> 00:01:21,490
pseudo-random number generators.
22
00:01:21,910 --> 00:01:25,420
And the one that Python uses is something called the Mersenne Twister.
23
00:01:25,900 --> 00:01:28,180
And if you really want to read about it,
24
00:01:28,210 --> 00:01:32,020
then you can have a look on Wikipedia and take a look under the algorithmic
25
00:01:32,020 --> 00:01:36,310
detail. But to be honest, it's a little bit too much information for anybody,
26
00:01:36,670 --> 00:01:41,050
unless you're really interested in these types of number generators.
27
00:01:41,560 --> 00:01:43,000
But what I recommend though,
28
00:01:43,000 --> 00:01:47,980
is a free video from the Khan Academy where they explain pseudorandom number
29
00:01:47,980 --> 00:01:50,950
generators, and it's really well produced.
30
00:01:50,980 --> 00:01:55,980
And it tells you a lot of different aspects of pseudorandom number generation.
31
00:01:57,010 --> 00:01:57,790
Have a look at that
32
00:01:57,790 --> 00:02:02,020
if you're interested to learn a little bit more about pseudorandom number
33
00:02:02,020 --> 00:02:05,650
generators. We've talked a lot about randomness,
34
00:02:05,710 --> 00:02:07,180
but lets see it in action.
35
00:02:07,210 --> 00:02:11,980
Let's write some code and produce some random numbers. Head over to the Day 4
36
00:02:11,980 --> 00:02:15,340
starting Repl and then go ahead and fork your own copy.
37
00:02:15,760 --> 00:02:18,970
We know that we want to get hold of some random numbers,
38
00:02:19,300 --> 00:02:23,950
but we don't really want to implement the Mersenne Twister ourselves because
39
00:02:24,220 --> 00:02:28,480
it's horrendously complicated using lots and lots of math and probably it'll
40
00:02:28,480 --> 00:02:33,250
take us months to write the code for. So how can we get random numbers?
41
00:02:33,880 --> 00:02:37,870
If you think about it, it's a little bit like searching for something in Google.
42
00:02:38,530 --> 00:02:43,530
The Google algorithm is horrendously complicated and it's taken them years and
43
00:02:43,660 --> 00:02:48,370
years to refine and perfect to such an extent where I think Google knows me
44
00:02:48,370 --> 00:02:50,980
better than myself because when I search for something,
45
00:02:50,980 --> 00:02:53,410
it always gives me the most relevant results.
46
00:02:53,920 --> 00:02:57,760
And if you're questioning the complexity of the Google algorithm,
47
00:02:58,090 --> 00:03:02,980
just go to Bing and try searching for something and see how relevant the results
48
00:03:02,980 --> 00:03:06,100
are. So they have the super complex algorithm,
49
00:03:06,130 --> 00:03:09,340
which we're never going to get a chance to see, let alone understand.
50
00:03:09,850 --> 00:03:14,850
And yet every day we're able to search things on Google and use this Google
51
00:03:15,100 --> 00:03:17,680
algorithm to get the things that we want,
52
00:03:18,010 --> 00:03:21,640
like the air speed velocity of a laden swallow.
53
00:03:22,240 --> 00:03:26,920
Now we can do the same thing when we want to generate random numbers. Because
54
00:03:26,920 --> 00:03:29,410
this is such frequently needed functionality,
55
00:03:29,710 --> 00:03:33,190
the Python team have already created a random module.
56
00:03:33,700 --> 00:03:38,700
So if you head over to askpython.com and search for the Python random module,
57
00:03:39,100 --> 00:03:42,700
then you'll be able to see the documentation for this random module.
58
00:03:43,180 --> 00:03:46,750
And you can see that it contains a whole bunch of functions that you can use
59
00:03:47,170 --> 00:03:50,110
that allows you to generate random integers,
60
00:03:50,110 --> 00:03:53,650
so random whole numbers or random floating point numbers.
61
00:03:54,040 --> 00:03:59,040
So let's see how we can tap into this random module. In order to tap into the
62
00:03:59,260 --> 00:04:02,410
random module, we first have to import it.
63
00:04:02,440 --> 00:04:07,440
So we write import random. And now we're able to use this random module in our
64
00:04:10,000 --> 00:04:11,800
main.py code.
65
00:04:12,580 --> 00:04:17,470
We can start using the documentation to generate a random integer. For example,
66
00:04:17,710 --> 00:04:22,710
they show us in the code snippet here that we could generate a random integer by
67
00:04:23,320 --> 00:04:27,430
using the randint function, providing an a and B.
68
00:04:27,850 --> 00:04:31,480
And it will return a random integer between a and b,
69
00:04:31,630 --> 00:04:33,700
including both of those numbers.
70
00:04:34,150 --> 00:04:37,390
And there's even a little bit of code snippet to show us how it's done.
71
00:04:37,720 --> 00:04:42,720
So this line of code generates a random whole number between 100 and 200 and
72
00:04:44,110 --> 00:04:48,310
including those two numbers. So it could be anything between those two numbers.
73
00:04:49,060 --> 00:04:51,340
So let's see if we can implement this in our code.
74
00:04:51,820 --> 00:04:56,820
Let's say that we want to create a random integer and we use the same code that
75
00:04:56,950 --> 00:04:59,650
we saw in the code snippet where we say random,
76
00:04:59,890 --> 00:05:04,600
that's tapping into this module here, .randint.
77
00:05:04,960 --> 00:05:09,960
And then we at a set of parentheses and then we specify the start, and
78
00:05:10,360 --> 00:05:11,350
then the end.
79
00:05:11,830 --> 00:05:16,830
So this is basically a range of numbers that we're specifying, any integer
80
00:05:16,960 --> 00:05:18,670
between 1 and 10.
81
00:05:19,480 --> 00:05:24,480
And now we can go ahead and print our random integer, just like that.
82
00:05:26,050 --> 00:05:28,060
And every single time I run it,
83
00:05:28,330 --> 00:05:32,050
you'll notice that I'll get a different number or maybe sometimes the same,
84
00:05:32,140 --> 00:05:35,650
but it's a random number between the range that I've specified.
85
00:05:37,000 --> 00:05:41,890
Now I've mentioned that the random module is a Python module.
86
00:05:41,980 --> 00:05:44,860
So what exactly is a module? Well,
87
00:05:44,890 --> 00:05:49,480
you've seen that we've mostly been writing our code all on the same page in a
88
00:05:49,480 --> 00:05:50,920
sort of script style, right?
89
00:05:50,920 --> 00:05:53,980
And everything just kind of gets executed from top to bottom.
90
00:05:54,460 --> 00:05:58,490
But sometimes your code will get so long because you're trying to create
91
00:05:58,490 --> 00:06:03,490
something complicated and it's no longer possible to understand what's going on
92
00:06:03,620 --> 00:06:06,140
in such a large piece of code.
93
00:06:06,650 --> 00:06:11,650
What people will do in that case is to split the code up into individual modules
94
00:06:13,190 --> 00:06:18,190
where each module is responsible for a different bit of functionality of your
95
00:06:18,380 --> 00:06:22,430
program. And if you have a complex project with many, many modules,
96
00:06:22,700 --> 00:06:25,520
then you could have collaboration on your project, right?
97
00:06:25,550 --> 00:06:29,630
Lots of people could be working on it, each person working on a different thing.
98
00:06:29,990 --> 00:06:31,820
Say if a factory was building a car,
99
00:06:32,150 --> 00:06:36,260
it wouldn't make sense for one person to build the entire car from wheels,
100
00:06:36,260 --> 00:06:38,810
axles to chassis, that's crazy.
101
00:06:38,960 --> 00:06:43,220
Instead you have different people working on different modules like the tire
102
00:06:43,220 --> 00:06:47,000
module or the chassis module or the engine module.
103
00:06:47,420 --> 00:06:49,220
And then once they put everything together,
104
00:06:49,490 --> 00:06:53,990
you end up with the final car or in our case, the final program.
105
00:06:54,740 --> 00:06:59,570
So we know that the random module is a module that the Python team created to
106
00:06:59,570 --> 00:07:04,570
make it easier for us to generate our random numbers without needing to get into
107
00:07:04,850 --> 00:07:09,850
the complexities of all of the math that's required to generate pseudorandom
108
00:07:09,920 --> 00:07:10,753
numbers.
109
00:07:10,970 --> 00:07:15,080
But how can we create our own modules and how do modules work anyways?
110
00:07:15,260 --> 00:07:19,430
Well, it's actually very simple. If you go ahead and click on the files icon,
111
00:07:19,490 --> 00:07:23,120
you can see, we currently only have one file that we're writing in,
112
00:07:23,420 --> 00:07:25,100
which is main.py,
113
00:07:25,130 --> 00:07:29,150
the Python file. And the main.py is the entry point to our program.
114
00:07:29,300 --> 00:07:33,590
This is the file that will be executed when we run our code. Now,
115
00:07:33,590 --> 00:07:37,100
if I go ahead and click on this icon to add a new file,
116
00:07:37,490 --> 00:07:42,490
let's say I call it my_module.py and I create a new Python file.
117
00:07:45,650 --> 00:07:50,270
And inside my module, I'm going to maybe work on something else.
118
00:07:50,300 --> 00:07:53,270
Let's say I want into the store, the value of PI.
119
00:07:55,850 --> 00:07:57,260
Okay. I think that's all I remember.
120
00:07:57,680 --> 00:08:02,360
So I know the value of PI and I want to be able to use it in my different
121
00:08:02,360 --> 00:08:06,080
programs. Now that I've created my_module.py, well,
122
00:08:06,080 --> 00:08:11,080
now I can use my module wherever I want just by importing it. So I could import
123
00:08:13,250 --> 00:08:17,480
my_module. And now down here, I can say,
124
00:08:17,900 --> 00:08:22,900
let's go ahead and print my_module.pi.
125
00:08:24,440 --> 00:08:28,610
And now if I go ahead and comment out of this code and hit run,
126
00:08:29,060 --> 00:08:29,810
then you'll see,
127
00:08:29,810 --> 00:08:34,810
we get that value of PI that got dragged in because I imported my module,
128
00:08:35,780 --> 00:08:40,370
but now we've got our code separated out into separate modules that are
129
00:08:40,370 --> 00:08:42,110
responsible for different things.
130
00:08:42,650 --> 00:08:46,460
And that is how the random module works as well.
131
00:08:46,880 --> 00:08:51,590
So I'm going to go ahead and delete all of this stuff relating to PI,
132
00:08:51,890 --> 00:08:55,500
but you can keep yours if you want, if you wanna keep it for reference.
133
00:08:57,390 --> 00:08:59,910
Coming back to creating random numbers.
134
00:09:00,300 --> 00:09:03,840
We've seen how we can create random whole numbers,
135
00:09:04,080 --> 00:09:08,730
but what if I wanted to create a random floating point number? Well,
136
00:09:08,730 --> 00:09:12,390
it tells me how to do it in the AskPython documentation.
137
00:09:12,780 --> 00:09:17,780
And all I have to do is use the random module that contains the random function
138
00:09:18,180 --> 00:09:23,180
and it will return a random floating-point number between 0 and 1.
139
00:09:24,480 --> 00:09:29,130
But it's really important to note that in this case it doesn't actually include
140
00:09:29,130 --> 00:09:34,050
1. So it goes up to like 0.9999999, but not including 1.
141
00:09:34,590 --> 00:09:36,150
Let's go ahead and try that out.
142
00:09:36,510 --> 00:09:41,190
So we'll say random_float = random.random
143
00:09:41,250 --> 00:09:45,990
and then I set of parentheses. And this will give me output some sort of number
144
00:09:46,320 --> 00:09:50,040
in the interval between 0 and 1, but not including 1.
145
00:09:50,910 --> 00:09:53,430
So let's go ahead and see if it works.
146
00:09:56,820 --> 00:09:57,210
First,
147
00:09:57,210 --> 00:10:01,650
we print a random integer between 1 and 10 and then we print our random float
148
00:10:01,710 --> 00:10:06,210
between 0 and 1. And every time we run the code,
149
00:10:06,240 --> 00:10:09,210
you'll notice that we get different random numbers.
150
00:10:10,290 --> 00:10:11,610
Now here's a question.
151
00:10:12,420 --> 00:10:17,420
This random function always generates a number between 0 and 1 and it has
152
00:10:18,210 --> 00:10:19,560
many decimal places.
153
00:10:20,160 --> 00:10:25,050
But what if I wanted a random floating-point number between 0 and 5?
154
00:10:25,980 --> 00:10:28,620
How might we do that? Now
155
00:10:28,680 --> 00:10:33,510
it's not as complicated as you think it is and you'll be able to do it just by
156
00:10:33,510 --> 00:10:35,190
looking at the code you've got here.
157
00:10:35,880 --> 00:10:39,210
Have a think about it and see if you can come up with the answer.
158
00:10:39,330 --> 00:10:42,420
I'll go through it with you afterwards. Pause the video now and give that a go.
159
00:10:44,430 --> 00:10:44,850
All right.
160
00:10:44,850 --> 00:10:49,850
So we know that this random float can be anything between
161
00:10:50,230 --> 00:10:51,300
0.000000
162
00:10:51,370 --> 00:10:56,370
to 0.999999..., effectively repeating right? Now
163
00:10:58,050 --> 00:11:03,050
what if we were able to multiply this random number by a number?
164
00:11:05,970 --> 00:11:10,620
Let's say that I multiply the random_float by 5.
165
00:11:11,310 --> 00:11:15,150
Well, what if this random_float was no 0.1? Well,
166
00:11:15,180 --> 00:11:19,050
0.1 times 5 would be 0.5.
167
00:11:20,070 --> 00:11:25,070
But what if the random_float was 0.4? 0.4 times 5 would be 2.
168
00:11:28,290 --> 00:11:30,090
Or what if it was 0.9?
169
00:11:30,120 --> 00:11:33,330
0.9 times five is 4.5.
170
00:11:33,840 --> 00:11:38,840
So effectively by multiplying this random_float by 5,
171
00:11:39,840 --> 00:11:42,540
I will end up with random numbers,
172
00:11:42,810 --> 00:11:46,920
but of course they're now floats, right? So it could be 0.000
173
00:11:46,920 --> 00:11:50,940
... all the way to 4.999999
174
00:11:51,360 --> 00:11:53,010
and et cetera, et cetera.
175
00:11:53,410 --> 00:11:58,410
So this is how we can expand that range from 0 to 1 all the way to 0 to
176
00:11:59,650 --> 00:12:02,050
5, but not including 5.
177
00:12:03,130 --> 00:12:05,950
So what can you do with random numbers? Well,
178
00:12:06,010 --> 00:12:10,870
if we think back to our love calculator that we made in the last lesson,
179
00:12:11,380 --> 00:12:15,070
um, I mean, its kind of based on pseudo-science right?
180
00:12:15,070 --> 00:12:20,050
Like just because you have a certain number of letters that exist in the word
181
00:12:20,050 --> 00:12:20,890
true and love,
182
00:12:20,890 --> 00:12:24,160
it doesn't mean that you're actually going to be more compatible.
183
00:12:24,950 --> 00:12:29,590
I'm sorry to break anyone's heart out there. But if it's random anyways,
184
00:12:29,620 --> 00:12:34,270
then why don't we just use a random number generator saving ourselves all of
185
00:12:34,270 --> 00:12:37,510
that trouble from counting Ts and counting Us.
186
00:12:37,900 --> 00:12:42,900
We can instead just simply say love_score = random.randint between
187
00:12:47,830 --> 00:12:52,830
1 and 100. And then go ahead and just print "Your love score is",
188
00:12:56,620 --> 00:13:01,420
and then let's insert our love score and turn this into an Fstring.
189
00:13:02,230 --> 00:13:06,310
Let's print that. And you can see that we're getting a different love score
190
00:13:06,340 --> 00:13:10,540
every single time I run this code. It's between 1 and a hundred
191
00:13:10,660 --> 00:13:12,400
and you know,
192
00:13:12,520 --> 00:13:16,300
who can tell that this is any different from the previous method, right?
193
00:13:16,660 --> 00:13:21,490
So we can use our random number generator for this. We can use it for,
194
00:13:21,520 --> 00:13:22,180
um,
195
00:13:22,180 --> 00:13:27,180
creating a dice or flipping a coin or we can use it in games that we create.
196
00:13:28,540 --> 00:13:33,010
If you head over to the next lesson, I've got a code challenge for you
197
00:13:33,310 --> 00:13:37,990
where you are going to be building a program in order to pick from heads or
198
00:13:37,990 --> 00:13:42,990
tails and help the user out when they don't have access to a coin. For all of
199
00:13:43,030 --> 00:13:45,280
that and more, I'll see you on the next lesson.
19843
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.