All language subtitles for 10. Randomising the Dice

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,910 --> 00:00:09,790 So in the last lesson, we switched up our stateless widget into a stateful widget. And now all the parts 1 2 00:00:09,790 --> 00:00:16,900 of our screen which will update when the user interacts with it, namely parts that need to change their 2 3 00:00:16,900 --> 00:00:22,190 state, is now inside something called a dice page state. 3 4 00:00:22,300 --> 00:00:31,600 And here, we have a button that detects when the user presses on it. And when that happens, we tell the 4 5 00:00:31,600 --> 00:00:34,810 app that we need to change state. 5 6 00:00:35,050 --> 00:00:40,210 And the thing that needs to change is everywhere that uses the leftDiceNumber. 6 7 00:00:41,170 --> 00:00:50,620 So when that happens, we look through this tree and our app will mark all the widgets that use that updated 7 8 00:00:50,680 --> 00:00:51,520 value. 8 9 00:00:51,760 --> 00:00:54,700 And it finds this one right here. 9 10 00:00:54,790 --> 00:01:04,450 So now, it will update our image widget and redraw it with the updated dice image. 10 11 00:01:04,600 --> 00:01:09,400 There's just one thing. We're still having to manually update the number here, 11 12 00:01:09,400 --> 00:01:10,040 right? 12 13 00:01:10,060 --> 00:01:17,500 If I type four and I hit save and I press the button, then it changes to four. But the user cannot change 13 14 00:01:17,500 --> 00:01:18,440 our code. 14 15 00:01:18,490 --> 00:01:29,970 So how can we get this left dice number to automatically change to a new random number between 1 and 6? 15 16 00:01:29,980 --> 00:01:34,690 Well in order to do this, we need to import a new library. 16 17 00:01:34,780 --> 00:01:42,530 The math library. If you were trying to get really good at knitting, then you might go into your local 17 18 00:01:42,530 --> 00:01:43,460 library. 18 19 00:01:43,460 --> 00:01:46,700 You might take out all of the books on knitting, 19 20 00:01:46,760 --> 00:01:49,790 take it home. Study it. Read it. Learn it. 20 21 00:01:49,790 --> 00:01:56,330 And in an ideal world, then you would be a world expert on knitting. And you'll be able to do all sorts 21 22 00:01:56,330 --> 00:02:00,770 of knitting: knitting hats, knitting socks, knitting jumpers. 22 23 00:02:00,770 --> 00:02:06,620 But if next week you decided that you wanted to be able to do things in the garden, you wanted to be able 23 24 00:02:06,620 --> 00:02:14,830 to grow things, then you might check out the parts of the gardening library. In order to generate random 24 25 00:02:14,830 --> 00:02:15,630 numbers, 25 26 00:02:15,790 --> 00:02:23,080 we can check out the Dart math library. And this is a library that contains all sorts of mathematical 26 27 00:02:23,080 --> 00:02:27,420 constants and functions plus a random number generator. 27 28 00:02:27,430 --> 00:02:29,690 That's exactly what we need. 28 29 00:02:29,770 --> 00:02:36,550 And once you use this library in your code by importing it into your project, then the things that your 29 30 00:02:36,550 --> 00:02:44,770 program will be able to do include generate random numbers, or know what is the base of the natural logarithms. 30 31 00:02:44,770 --> 00:02:48,990 That's a lot more maths than I would know, off the top of my head. 31 32 00:02:49,150 --> 00:02:56,290 So let's go ahead and put that into our project. So we're going to go right up to the top and you can 32 33 00:02:56,290 --> 00:03:02,290 see that we're already importing a library. And this is the material.dart library. 33 34 00:03:02,290 --> 00:03:07,870 And this includes all the things such as how should a flat bottom look as opposed to a raise button? 34 35 00:03:07,870 --> 00:03:15,190 The fact that we're able to simply just type out a widget like a flat button, and it knows exactly what 35 36 00:03:15,190 --> 00:03:20,500 that should look like, how it should behave, is all thanks to this library. 36 37 00:03:20,530 --> 00:03:26,920 So now we're going to power up our project even further by importing the Dart maths library. 37 38 00:03:27,100 --> 00:03:28,840 So start writing maths, 38 39 00:03:28,960 --> 00:03:31,520 it'll find the right one for you. Hit enter. 39 40 00:03:31,630 --> 00:03:34,630 And we've now got the maths library. 40 41 00:03:34,630 --> 00:03:40,480 Now initially, it'll show up as grey because it's telling you that you've quoted this library, but you're 41 42 00:03:40,480 --> 00:03:41,640 not using it yet. 42 43 00:03:41,800 --> 00:03:43,300 It's just getting a bit ahead of us. 43 44 00:03:43,300 --> 00:03:50,500 We're gonna use it right now. Instead of setting our leftDiceNumber equal to 4 or 3 or 1 manually, which 44 45 00:03:50,500 --> 00:03:51,930 is quite painful, 45 46 00:03:51,970 --> 00:03:59,500 instead we're going to use something that comes along with the math library which is called random. And 46 47 00:03:59,560 --> 00:04:04,450 random has a function called nextInt. 47 48 00:04:04,720 --> 00:04:06,300 And how do I know this? 48 49 00:04:06,310 --> 00:04:13,660 Well I read through the documentations. If we look at the random generator, then we can see that it has 49 50 00:04:13,750 --> 00:04:16,430 a method called nextInt. 50 51 00:04:17,020 --> 00:04:27,520 And this generates a non-negative random integer, so a random whole number, from 0 up to max and not including, 51 52 00:04:27,850 --> 00:04:29,800 so excluding the max. 52 53 00:04:30,220 --> 00:04:36,490 So the max is what you put inside the parentheses of this nextInt function. 53 54 00:04:36,490 --> 00:04:46,340 And it will try to generate whole numbers between 0 and whatever it is this number is minus 1. So let's 54 55 00:04:46,340 --> 00:04:47,570 see it in practice. 55 56 00:04:47,570 --> 00:04:55,430 We're using the random number generator that comes from our Dart maths library. And we're using a method 56 57 00:04:55,430 --> 00:04:56,900 called nextInt 57 58 00:04:57,110 --> 00:05:05,780 that comes from the random number generator. And we're providing a maximum value between these parentheses. 58 59 00:05:06,980 --> 00:05:15,170 So if we put in a number of six, what numbers do you think will be generated by our random number generator? 59 60 00:05:15,170 --> 00:05:16,730 Well we can always test it out, 60 61 00:05:16,730 --> 00:05:17,030 right? 61 62 00:05:17,060 --> 00:05:19,560 There's nothing mysterious in programming. 62 63 00:05:19,670 --> 00:05:26,580 Let's copy our line of code and let's head over to dartpad. And inside our main function, let's paste 63 64 00:05:26,590 --> 00:05:35,720 in our leftDiceNumber. And let's add our int to create this new leftDiceNumber. 64 65 00:05:35,720 --> 00:05:42,220 And of course, we need to import our Dart maths library. 65 66 00:05:42,350 --> 00:05:44,790 Now we have pretty much the same conditions. 66 67 00:05:44,840 --> 00:05:47,170 We have our Dart maths library imported, 67 68 00:05:47,200 --> 00:05:55,420 so we can use our random number generator. And we've created this new variable called leftDiceNumber. 68 69 00:05:55,550 --> 00:06:01,160 We've set it it's equal the random number generator using that nextInt method. 69 70 00:06:01,910 --> 00:06:09,800 So now all we have to do is just to print out our leftDiceNumber into the console, and we click run 70 71 00:06:09,830 --> 00:06:12,460 a couple of times just to see what it'll print out. 71 72 00:06:12,650 --> 00:06:17,070 So we get 5. 1. 3. 72 73 00:06:17,270 --> 00:06:26,740 And if you do this long enough, you'll realize that we'll get numbers between zero to six minus one. 73 74 00:06:26,740 --> 00:06:30,830 So whatever we provide in here minus 1, which is 5. 74 75 00:06:31,340 --> 00:06:34,130 So we'll get all the numbers between 0 and 5. 75 76 00:06:34,130 --> 00:06:36,580 So it could be any of those numbers. 76 77 00:06:36,770 --> 00:06:44,240 Now in our case, we can't actually have zero because we don't actually have an image called dice0. 77 78 00:06:44,750 --> 00:06:53,580 And if you run this a few times and you click on the button, eventually the image will disappear because 78 79 00:06:53,580 --> 00:06:56,640 it doesn't have a dice0.png. 79 80 00:06:57,360 --> 00:07:05,130 Now what can we do to change that range from 0 - 5, to 1 - 6? 80 81 00:07:05,130 --> 00:07:07,800 Well we just add one to this. 81 82 00:07:07,800 --> 00:07:13,490 If it's gonna be a zero, it turns into one. If it's gonna be a five, it turns into six. 82 83 00:07:13,500 --> 00:07:26,330 So now we can hit save and safely click on a button, and we will never get an empty image ever again. 83 84 00:07:26,450 --> 00:07:35,480 So now in our flat button, when it detects a press, it will update the state of the screen to change all 84 85 00:07:35,480 --> 00:07:43,280 the places that use leftDiceNumber to use this new value that we've generated using our random generator 85 86 00:07:43,580 --> 00:07:47,150 which comes from our Dart maths library. 86 87 00:07:47,150 --> 00:07:52,690 If only learning maths in real life was as simple as importing a maths library into your brain, right? 87 88 00:07:53,420 --> 00:07:59,990 But in this lesson, we've looked at how we can use that library and the random number generator. 88 89 00:08:00,020 --> 00:08:02,690 Now I have a challenge for you. 89 90 00:08:02,780 --> 00:08:09,050 I want you to be able to do the same thing that we've done for the left dice, but for the right dice. 90 91 00:08:09,080 --> 00:08:14,510 Because at the moment, it doesn't do anything when I click on it. And I want it to have the same behavior 91 92 00:08:14,870 --> 00:08:17,720 of changing the number when I click on it, 92 93 00:08:17,780 --> 00:08:24,200 just like this one. Once you are ready, pause the video and try to complete the challenge. In the next lesson, 93 94 00:08:24,380 --> 00:08:25,490 I'll show you the solution. 10365

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