All language subtitles for 039 Random Module.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1

One of the first things I want to talk about is randomisation.

2

And this is a concept that we're super familiar with.

3

Randomization is really,

4

really important when we want to create computer programs that have a degree of

5

unpredictability. Now, the biggest category of that is, of course, games right?

6

Can you imagine if you had to play Tetris and every single time,

7

the block that fell down was like predictable.

8

You always knew that it was going to be a T and then it was going to be an L

9

like, there would be no fun in that right?

10

So how do we create this randomness for our programs? Now in nature and in our

11

everyday lives, it's really easy to create randomness.

12

If you splash some paint on a canvas, that's going to be pretty random.

13

If you take a look at the TV static from the analog TVs,

14

that's also a whole bunch of randomness there.

15

But when we're talking about computers,

16

then these machines are what we would call deterministic.

17

They will perform repeatable actions in a fully predictable way.

18

So how do we wrangle these machines that operate basically on ones and zeros to

19

get them to create some random numbers? Well,

20

there's a whole bunch of maths that can be applied to create what are called a

21

pseudo-random number generators.

22

And the one that Python uses is something called the Mersenne Twister.

23

And if you really want to read about it,

24

then you can have a look on Wikipedia and take a look under the algorithmic

25

detail. But to be honest, it's a little bit too much information for anybody,

26

unless you're really interested in these types of number generators.

27

But what I recommend though,

28

is a free video from the Khan Academy where they explain pseudorandom number

29

generators, and it's really well produced.

30

And it tells you a lot of different aspects of pseudorandom number generation.

31

Have a look at that

32

if you're interested to learn a little bit more about pseudorandom number

33

generators. We've talked a lot about randomness,

34

but lets see it in action.

35

Let's write some code and produce some random numbers. Head over to the Day 4

36

starting Repl and then go ahead and fork your own copy.

37

We know that we want to get hold of some random numbers,

38

but we don't really want to implement the Mersenne Twister ourselves because

39

it's horrendously complicated using lots and lots of math and probably it'll

40

take us months to write the code for. So how can we get random numbers?

41

If you think about it, it's a little bit like searching for something in Google.

42

The Google algorithm is horrendously complicated and it's taken them years and

43

years to refine and perfect to such an extent where I think Google knows me

44

better than myself because when I search for something,

45

it always gives me the most relevant results.

46

And if you're questioning the complexity of the Google algorithm,

47

just go to Bing and try searching for something and see how relevant the results

48

are. So they have the super complex algorithm,

49

which we're never going to get a chance to see, let alone understand.

50

And yet every day we're able to search things on Google and use this Google

51

algorithm to get the things that we want,

52

like the air speed velocity of a laden swallow.

53

Now we can do the same thing when we want to generate random numbers. Because

54

this is such frequently needed functionality,

55

the Python team have already created a random module.

56

So if you head over to askpython.com and search for the Python random module,

57

then you'll be able to see the documentation for this random module.

58

And you can see that it contains a whole bunch of functions that you can use

59

that allows you to generate random integers,

60

so random whole numbers or random floating point numbers.

61

So let's see how we can tap into this random module. In order to tap into the

62

random module, we first have to import it.

63

So we write import random. And now we're able to use this random module in our

64

main.py code.

65

We can start using the documentation to generate a random integer. For example,

66

they show us in the code snippet here that we could generate a random integer by

67

using the randint function, providing an a and B.

68

And it will return a random integer between a and b,

69

including both of those numbers.

70

And there's even a little bit of code snippet to show us how it's done.

71

So this line of code generates a random whole number between 100 and 200 and

72

including those two numbers. So it could be anything between those two numbers.

73

So let's see if we can implement this in our code.

74

Let's say that we want to create a random integer and we use the same code that

75

we saw in the code snippet where we say random,

76

that's tapping into this module here, .randint.

77

And then we at a set of parentheses and then we specify the start, and

78

then the end.

79

So this is basically a range of numbers that we're specifying, any integer

80

between 1 and 10.

81

And now we can go ahead and print our random integer, just like that.

82

And every single time I run it,

83

you'll notice that I'll get a different number or maybe sometimes the same,

84

but it's a random number between the range that I've specified.

85

Now I've mentioned that the random module is a Python module.

86

So what exactly is a module? Well,

87

you've seen that we've mostly been writing our code all on the same page in a

88

sort of script style, right?

89

And everything just kind of gets executed from top to bottom.

90

But sometimes your code will get so long because you're trying to create

91

something complicated and it's no longer possible to understand what's going on

92

in such a large piece of code.

93

What people will do in that case is to split the code up into individual modules

94

where each module is responsible for a different bit of functionality of your

95

program. And if you have a complex project with many, many modules,

96

then you could have collaboration on your project, right?

97

Lots of people could be working on it, each person working on a different thing.

98

Say if a factory was building a car,

99

it wouldn't make sense for one person to build the entire car from wheels,

100

axles to chassis, that's crazy.

101

Instead you have different people working on different modules like the tire

102

module or the chassis module or the engine module.

103

And then once they put everything together,

104

you end up with the final car or in our case, the final program.

105

So we know that the random module is a module that the Python team created to

106

make it easier for us to generate our random numbers without needing to get into

107

the complexities of all of the math that's required to generate pseudorandom

108

numbers.

109

But how can we create our own modules and how do modules work anyways?

110

Well, it's actually very simple. If you go ahead and click on the files icon,

111

you can see, we currently only have one file that we're writing in,

112

which is main.py,

113

the Python file. And the main.py is the entry point to our program.

114

This is the file that will be executed when we run our code. Now,

115

if I go ahead and click on this icon to add a new file,

116

let's say I call it my_module.py and I create a new Python file.

117

And inside my module, I'm going to maybe work on something else.

118

Let's say I want into the store, the value of PI.

119

Okay. I think that's all I remember.

120

So I know the value of PI and I want to be able to use it in my different

121

programs. Now that I've created my_module.py, well,

122

now I can use my module wherever I want just by importing it. So I could import

123

my_module. And now down here, I can say,

124

let's go ahead and print my_module.pi.

125

And now if I go ahead and comment out of this code and hit run,

126

then you'll see,

127

we get that value of PI that got dragged in because I imported my module,

128

but now we've got our code separated out into separate modules that are

129

responsible for different things.

130

And that is how the random module works as well.

131

So I'm going to go ahead and delete all of this stuff relating to PI,

132

but you can keep yours if you want, if you wanna keep it for reference.

133

Coming back to creating random numbers.

134

We've seen how we can create random whole numbers,

135

but what if I wanted to create a random floating point number? Well,

136

it tells me how to do it in the AskPython documentation.

137

And all I have to do is use the random module that contains the random function

138

and it will return a random floating-point number between 0 and 1.

139

But it's really important to note that in this case it doesn't actually include

140

1. So it goes up to like 0.9999999, but not including 1.

141

Let's go ahead and try that out.

142

So we'll say random_float = random.random

143

and then I set of parentheses. And this will give me output some sort of number

144

in the interval between 0 and 1, but not including 1.

145

So let's go ahead and see if it works.

146

First,

147

we print a random integer between 1 and 10 and then we print our random float

148

between 0 and 1. And every time we run the code,

149

you'll notice that we get different random numbers.

150

Now here's a question.

151

This random function always generates a number between 0 and 1 and it has

152

many decimal places.

153

But what if I wanted a random floating-point number between 0 and 5?

154

How might we do that? Now

155

it's not as complicated as you think it is and you'll be able to do it just by

156

looking at the code you've got here.

157

Have a think about it and see if you can come up with the answer.

158

I'll go through it with you afterwards. Pause the video now and give that a go.

159

All right.

160

So we know that this random float can be anything between

161

0.000000

162

to 0.999999..., effectively repeating right? Now

163

what if we were able to multiply this random number by a number?

164

Let's say that I multiply the random_float by 5.

165

Well, what if this random_float was no 0.1? Well,

166

0.1 times 5 would be 0.5.

167

But what if the random_float was 0.4? 0.4 times 5 would be 2.

168

Or what if it was 0.9?

169

0.9 times five is 4.5.

170

So effectively by multiplying this random_float by 5,

171

I will end up with random numbers,

172

but of course they're now floats, right? So it could be 0.000

173

... all the way to 4.999999

174

and et cetera, et cetera.

175

So this is how we can expand that range from 0 to 1 all the way to 0 to

176

5, but not including 5.

177

So what can you do with random numbers? Well,

178

if we think back to our love calculator that we made in the last lesson,

179

um, I mean, its kind of based on pseudo-science right?

180

Like just because you have a certain number of letters that exist in the word

181

true and love,

182

it doesn't mean that you're actually going to be more compatible.

183

I'm sorry to break anyone's heart out there. But if it's random anyways,

184

then why don't we just use a random number generator saving ourselves all of

185

that trouble from counting Ts and counting Us.

186

We can instead just simply say love_score = random.randint between

187

1 and 100. And then go ahead and just print "Your love score is",

188

and then let's insert our love score and turn this into an Fstring.

189

Let's print that. And you can see that we're getting a different love score

190

every single time I run this code. It's between 1 and a hundred

191

and you know,

192

who can tell that this is any different from the previous method, right?

193

So we can use our random number generator for this. We can use it for,

194

um,

195

creating a dice or flipping a coin or we can use it in games that we create.

196

If you head over to the next lesson, I've got a code challenge for you

197

where you are going to be building a program in order to pick from heads or

198

tails and help the user out when they don't have access to a coin. For all of

199

that and more, I'll see you on the next lesson.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.