1 00:00:01,470 --> 00:00:03,220 Hello I will come again. 2 00:00:03,220 --> 00:00:09,280 This lesson is going to be an exercise on what I want you to do is this I want you to write the program 3 00:00:09,280 --> 00:00:17,530 that the rules a dice for one million times and put uses a number between 1 to 6 just like hard Dyce's. 4 00:00:17,680 --> 00:00:21,010 And that tells us either the numbers of 1 to 6. 5 00:00:21,010 --> 00:00:23,990 How many times they showed up. 6 00:00:24,190 --> 00:00:31,020 So once again run a dice or roll the dice for one million times and find out how many times they'd want 7 00:00:31,030 --> 00:00:31,620 to show up. 8 00:00:31,630 --> 00:00:35,110 How many times did you show up on the Trishul. 9 00:00:35,140 --> 00:00:36,610 So on and so forth. 10 00:00:36,850 --> 00:00:43,190 Now to get us started I'm going to give you one piece of hands or one piece of advice essentially this 11 00:00:43,320 --> 00:00:50,470 jump into X hold in the expert I'm going to start an application and that's going to be a command line 12 00:00:50,470 --> 00:00:50,750 tool. 13 00:00:50,770 --> 00:00:58,410 I'm going to call it dice project and the dice project I'm going to go ahead get my. 14 00:01:00,040 --> 00:01:08,050 Clean it up better formatting and everything in here I'm going to show you one thing and that is if 15 00:01:08,050 --> 00:01:18,160 I have ints random for instance random instead and that's ARTC for random this will essentially will 16 00:01:18,160 --> 00:01:20,920 produce a random number for me. 17 00:01:20,920 --> 00:01:22,550 That's all I'm going to tell you. 18 00:01:22,750 --> 00:01:34,090 So if I say slight whatever you find in the value random ints and 5 run bill then run this application 19 00:01:34,510 --> 00:01:38,960 it will show you that it has produced one a random number. 20 00:01:39,760 --> 00:01:47,200 Let's see that together the random number is minus 10 billion the world is read only again this time 21 00:01:47,200 --> 00:01:49,630 is going to give us a different random number. 22 00:01:49,810 --> 00:01:51,220 That's all I'm going to tell you. 23 00:01:51,250 --> 00:01:57,440 So you can use the arc for random to produce a random number and based on that. 24 00:01:57,520 --> 00:02:00,500 Here is your task. 25 00:02:00,700 --> 00:02:07,690 You have to essentially write a program that runs the dice for a one million times and tells us how 26 00:02:07,690 --> 00:02:10,270 many times 5:59 showed up. 27 00:02:10,270 --> 00:02:11,980 So pause the video here. 28 00:02:12,220 --> 00:02:13,530 Given the choice on your own. 29 00:02:13,600 --> 00:02:19,520 And then once you're done you can just you know work along with me and you can see how I'm going to 30 00:02:19,520 --> 00:02:22,000 do it again. 31 00:02:22,090 --> 00:02:27,320 So I'm going to go back to school and I'm going to show you the solution myself in scores. 32 00:02:27,340 --> 00:02:28,600 I'm going to do this. 33 00:02:28,840 --> 00:02:32,850 I'm going to say my random end which is ARTC for Andong. 34 00:02:32,850 --> 00:02:34,320 And it produces this. 35 00:02:34,480 --> 00:02:35,670 Let's do this. 36 00:02:35,770 --> 00:02:45,070 So many times so for eons it is zero or a smaller than let's say 100 times for an hour and not a million 37 00:02:45,070 --> 00:02:51,500 times and then I'm going to go ahead and say let's reinvent everything clean them up. 38 00:02:51,580 --> 00:03:01,180 Number one this is showing me so any random numbers 100 random numbers but none of them is essentially 39 00:03:01,180 --> 00:03:06,270 what I want which is within a range of 1 to 6. 40 00:03:06,280 --> 00:03:11,470 So what I'm doing is I'm going to go ahead and say this random number that you produce. 41 00:03:11,470 --> 00:03:15,820 Let's get a marginal loss of faith against six. 42 00:03:16,090 --> 00:03:20,600 If we do this we get numbers that are between zero to five. 43 00:03:20,710 --> 00:03:23,580 So we get 0 1 2 3 4 5. 44 00:03:23,770 --> 00:03:25,990 Any of those numbers will show up. 45 00:03:26,110 --> 00:03:27,780 But that's not what they want either. 46 00:03:27,850 --> 00:03:34,130 Simply because I want numbers of 1 to 6 I want the ones I want sixes. 47 00:03:34,150 --> 00:03:38,680 I also don't want any zeros such as this one that's going up in here. 48 00:03:38,680 --> 00:03:41,880 So what I'm going to do is I'm going to add the one tweet in here. 49 00:03:42,130 --> 00:03:47,590 I don't remember because of the presidents of the operations this first happens first then that the 50 00:03:47,610 --> 00:03:48,800 spot happens. 51 00:03:48,830 --> 00:03:52,460 If it is difficult Remember you can just pantheists this on here. 52 00:03:52,600 --> 00:03:59,210 So you remember what's going on or you could actually do it the other way on say one plus the odds. 53 00:03:59,500 --> 00:04:06,280 So it's going to produce any number between 0 2 5 1 1 2 8 so essentially becomes any number between 54 00:04:06,280 --> 00:04:07,820 1 to 6. 55 00:04:07,840 --> 00:04:10,850 That's Rhonda. 56 00:04:10,980 --> 00:04:16,480 So now I have all of these different results 5 6 2 6 4 6 1 3 3 to you. 57 00:04:16,500 --> 00:04:23,430 All the numbers are crunched between 1 to 6 which is acceptable numbers for a dice. 58 00:04:23,430 --> 00:04:28,610 Now the next thing we have to do is we have two accounts that we have to find out how many times have 59 00:04:28,620 --> 00:04:31,640 one happen when it comes to who's up in threes. 60 00:04:31,710 --> 00:04:33,090 So on and so forth. 61 00:04:33,090 --> 00:04:42,260 So for that I'm going to go ahead and say I'm going to call it count of one that is currently zero and 62 00:04:42,380 --> 00:04:46,800 counter 2 is also currently 0. 63 00:04:46,960 --> 00:04:48,510 Let me actually call it this. 64 00:04:48,540 --> 00:04:50,200 It's going to be easier for me. 65 00:04:50,460 --> 00:05:02,030 So counter 3 is also 0 count four is also zero and then I count five. 66 00:05:02,760 --> 00:05:08,370 And then I have finally counter 6 so these are the different counters that each of them is going to 67 00:05:08,370 --> 00:05:09,010 count. 68 00:05:09,030 --> 00:05:12,830 One of the many numbers of the of the dice. 69 00:05:12,840 --> 00:05:16,140 So when I go through my for loop I'm going to go ahead. 70 00:05:16,160 --> 00:05:17,010 I'm safe. 71 00:05:17,020 --> 00:05:24,750 If the random INS was one if I won it what should I do. 72 00:05:24,750 --> 00:05:32,340 I should say counter one increment itself and I don't mean to print the values exactly there so I'm 73 00:05:32,340 --> 00:05:33,840 going to draw that. 74 00:05:33,840 --> 00:05:38,610 So let me just get myself a little bit further spacing in here because this is going to be a bit of 75 00:05:38,610 --> 00:05:39,820 a longer call. 76 00:05:39,840 --> 00:05:44,770 I want to make sure that you know I can somehow zoom into everything for you guys. 77 00:05:46,420 --> 00:05:48,950 And now I have to write this if statement. 78 00:05:48,950 --> 00:05:50,310 I know that five times. 79 00:05:50,310 --> 00:06:03,900 So I have to write else if how random it was to then obviously counter to gets incremented it duplicated 80 00:06:03,910 --> 00:06:04,900 so many times. 81 00:06:04,900 --> 00:06:07,660 Coffee coffee coffee coffee coffee. 82 00:06:07,690 --> 00:06:14,300 So if it was true if it was theory then sorry that I was yes. 83 00:06:14,340 --> 00:06:24,950 If it was 3 then three if it became four Dan four if it was five then five. 84 00:06:25,150 --> 00:06:30,850 And then I don't need to really check for six because obviously if it is not one to five it's certainly 85 00:06:30,920 --> 00:06:32,170 the sixth one. 86 00:06:32,200 --> 00:06:38,290 So like this I'm going to run for a loop a hundred times and 100 times I'm going to produce a random 87 00:06:38,290 --> 00:06:44,190 number between 1 to 6 and then I'm going to count them once the for loop is done. 88 00:06:44,240 --> 00:06:51,690 And I have to print them I'm going to say and that's long and I'm going to say what happened. 89 00:06:51,700 --> 00:06:58,860 How many times and times and for that I'm going to use counter one. 90 00:06:58,930 --> 00:07:06,690 So one happened counter one times and of course we have to duplicate this six times as well. 91 00:07:06,730 --> 00:07:12,200 So I'm going to say two three four five six. 92 00:07:12,250 --> 00:07:27,260 And of course that is going to be two three four or half and capital F 5 6 happened that too many times. 93 00:07:27,430 --> 00:07:31,910 So let's run let's see if we get what we want to get. 94 00:07:31,910 --> 00:07:37,070 So it says 1 happened 11 times to have it 19 times so on and so forth. 95 00:07:37,070 --> 00:07:39,760 Let's run this for a million times. 96 00:07:39,780 --> 00:07:47,390 That is one hundred thousand that's 1 million times so almost all of them are happening within Iran. 97 00:07:47,390 --> 00:07:53,720 One hundred sixty six thousand that's kind of makes sense because they all should have a similar chance 98 00:07:53,720 --> 00:07:56,040 of showing up again. 99 00:07:56,060 --> 00:08:03,170 Now that this is working I'm going to keep this up in here for a second so if you want to you know look 100 00:08:03,170 --> 00:08:07,970 at my quote for any reason you can pause there be doing here because I'm going to change all of these 101 00:08:07,970 --> 00:08:12,200 and I'm going to rewrite this record so I'm going to pause here for a few seconds and then I'm going 102 00:08:12,200 --> 00:08:16,890 to rewrite this statement with a switch statement. 103 00:08:19,210 --> 00:08:19,610 OK. 104 00:08:19,660 --> 00:08:20,320 Let's get to it. 105 00:08:20,320 --> 00:08:28,240 So instead of this knowing if else if else if else if a situation delete all of that I'm saying when 106 00:08:28,240 --> 00:08:34,720 you find a new random number switch the switch not the random number random. 107 00:08:34,770 --> 00:08:43,280 And in case it was a one obviously counter one odds to itself and breaks. 108 00:08:43,320 --> 00:08:47,960 And now let's duplicate that case one more time. 109 00:08:48,080 --> 00:08:49,030 It didn't. 110 00:08:49,180 --> 00:08:49,720 OK. 111 00:08:49,900 --> 00:08:53,430 Two three four five and six. 112 00:08:53,440 --> 00:08:58,690 So if when we switch if you got two then counter two should go up. 113 00:08:58,810 --> 00:09:01,730 If we got three then counter two should go up. 114 00:09:01,750 --> 00:09:07,840 If we got four then counter four would go up five out of five. 115 00:09:07,870 --> 00:09:11,020 I have six counter six. 116 00:09:11,020 --> 00:09:16,420 And then of course at the end again you have two princedom we have to find out how many times counter 117 00:09:16,420 --> 00:09:21,570 1 happened how many times counter to this fourth essentially is going to stay intact. 118 00:09:21,610 --> 00:09:28,390 So let's run this go it is working perfectly fine this time as well. 119 00:09:28,400 --> 00:09:37,280 Like I said when we were discussing the switch lesson switches not necessarily less code is just more 120 00:09:37,280 --> 00:09:42,780 organized so you can see it easier in this age if cases when you do this thing. 121 00:09:42,890 --> 00:09:46,880 And if that's the case the break we don't continue with any of these. 122 00:09:46,930 --> 00:09:52,610 And because of that its performance is a little bit better than an if else statement. 123 00:09:52,910 --> 00:09:55,480 So this is writing it with a switch. 124 00:09:55,490 --> 00:09:58,110 So I'm going to keep it here for a second. 125 00:09:58,190 --> 00:10:03,020 And you know what just so that you can all you can see all of the content on my screen. 126 00:10:03,170 --> 00:10:09,170 I'm going to drop that 3 4 5 and 6 so you can see it only for the first three. 127 00:10:09,170 --> 00:10:11,700 And you know the rest of them are exactly identical. 128 00:10:11,870 --> 00:10:17,020 So I'm going to pause the video here for a second and then I'm going to rewrite this another way. 129 00:10:20,270 --> 00:10:20,720 OK. 130 00:10:20,790 --> 00:10:28,230 So now so far we managed to simplify some parts of our switch and now I want to simplify our counters 131 00:10:28,320 --> 00:10:30,080 so I count them individually. 132 00:10:30,200 --> 00:10:31,650 One two three four or five. 133 00:10:31,640 --> 00:10:35,220 And that doesn't really seem like a very intuitive way of doing it. 134 00:10:35,280 --> 00:10:41,870 So I'm going to delete all of this for now I'm going to delete all of that. 135 00:10:42,330 --> 00:10:47,600 As that as I'm going to delete all of these guys for now. 136 00:10:47,760 --> 00:10:56,940 I'm going to go ahead and say I have only one integer up there called Count headers and that is an array 137 00:10:57,570 --> 00:10:59,740 field 6 I'll use. 138 00:10:59,760 --> 00:11:03,210 Which are all of the current Sile's zero. 139 00:11:03,600 --> 00:11:07,820 So these are my counters so my counter's is an array. 140 00:11:08,010 --> 00:11:09,990 This first one is for one. 141 00:11:09,990 --> 00:11:11,430 This one is for two. 142 00:11:11,430 --> 00:11:12,390 This one is for three. 143 00:11:12,390 --> 00:11:14,130 This one is full for this one for a while. 144 00:11:14,130 --> 00:11:15,870 This one is for six. 145 00:11:15,900 --> 00:11:23,650 Now here is what they wanted to do I want to say if I got one then this first one should go up. 146 00:11:23,670 --> 00:11:25,680 Should become one. 147 00:11:25,680 --> 00:11:32,250 If I got one again then the first one should become two three so on and so forth not elements in the 148 00:11:32,250 --> 00:11:40,460 array should increase themselves as they should increments themselves as we find new dice numbers that 149 00:11:40,470 --> 00:11:41,740 they represent them. 150 00:11:41,940 --> 00:11:50,670 However I have a problem and that problem is this the end I don't see my array are numbered 0 1 2 3 151 00:11:50,970 --> 00:11:52,600 4 5 1. 152 00:11:52,650 --> 00:11:55,440 I actually look for 1 to 6. 153 00:11:55,440 --> 00:12:00,930 So because of that I'm going to change my counters to 7 I'm going to imagine that is one at the very 154 00:12:00,930 --> 00:12:03,510 beginning that I don't use for anything. 155 00:12:03,810 --> 00:12:05,720 And then there's another one at the end as well. 156 00:12:05,790 --> 00:12:09,870 So now I've got six elements in my array. 157 00:12:09,900 --> 00:12:12,360 The first one I'm going to completely ignore. 158 00:12:12,390 --> 00:12:14,180 Let's play that minus one. 159 00:12:14,220 --> 00:12:17,000 So we know that that one is always going to be ignored. 160 00:12:17,010 --> 00:12:19,370 I don't use it for anything. 161 00:12:19,380 --> 00:12:35,700 So now if in my switch I get chased one I can say counters one increment yourself if case is on by two 162 00:12:36,570 --> 00:12:38,950 to what am I typing. 163 00:12:39,010 --> 00:12:43,240 And then counters 2 will increment itself. 164 00:12:43,470 --> 00:12:48,650 And of course we break after we do the same thing for all six of them. 165 00:12:48,780 --> 00:12:53,890 So for now I'm going to say two three four five six. 166 00:12:54,090 --> 00:12:59,270 So in case of three then that becomes three in case of four. 167 00:12:59,280 --> 00:13:06,030 Then Number four increases itself in case of five then five increases it's not a case of six then six 168 00:13:06,030 --> 00:13:07,670 increases it's. 169 00:13:08,340 --> 00:13:14,450 So now that they can count up correctly how am I going to show them I'm going to say let's go outside 170 00:13:14,460 --> 00:13:15,320 the for loop. 171 00:13:15,350 --> 00:13:16,540 So that's here. 172 00:13:16,770 --> 00:13:25,080 I'm going to say I'm not there for loop this time for it and let's call it C equals 1 because we start 173 00:13:25,080 --> 00:13:31,070 from 1 and then see the smaller require six. 174 00:13:31,210 --> 00:13:34,060 That's the last object in here. 175 00:13:34,120 --> 00:13:40,690 I'm going to say for that C++ to do this and this log 176 00:13:42,840 --> 00:13:50,510 something happens something times. 177 00:13:50,520 --> 00:13:58,080 So what is the first something the first thing you see for instance or what happens how many times it 178 00:13:58,080 --> 00:14:04,060 happens count thare's one time which is see again. 179 00:14:04,080 --> 00:14:05,970 So let's see what I have written. 180 00:14:06,000 --> 00:14:08,400 It says go from 1 to 6. 181 00:14:08,400 --> 00:14:19,900 Say for instance if it is 1 1 happens counter of 1 times 2 happens counter of 2 times so on and so forth. 182 00:14:19,920 --> 00:14:28,970 Let's on this it says one happens one hundred sixty six thousand or so on and so forth all the way to 183 00:14:29,060 --> 00:14:30,200 solve it quickly. 184 00:14:30,240 --> 00:14:37,590 What we have done in my array I have 7 elements ignore the first one from 1 to 6. 185 00:14:37,620 --> 00:14:39,650 Each of them are zeroes. 186 00:14:39,840 --> 00:14:45,340 Whenever we find one count there one this guy counts up. 187 00:14:45,350 --> 00:14:48,920 So for instance this one becomes one and we find that two. 188 00:14:48,960 --> 00:14:52,370 This one becomes one when we find another one. 189 00:14:52,380 --> 00:14:55,540 This one becomes two when we find another one. 190 00:14:55,560 --> 00:15:02,600 This one becomes three so on and so forth they count themselves until the for loop is finished. 191 00:15:02,610 --> 00:15:08,550 So let me reset these viruses and let's try to simplify it to one last time. 192 00:15:08,970 --> 00:15:17,640 So when I look at my statement it feels like one case is one counter one counts up in cases to counter 193 00:15:17,640 --> 00:15:20,770 two counts up all the way to the end. 194 00:15:21,000 --> 00:15:23,210 So how about I do this. 195 00:15:23,370 --> 00:15:33,750 How about I say Well when you produce a random number and remember that number is either 1 2 3 4 5 or 196 00:15:33,750 --> 00:15:34,700 6. 197 00:15:34,860 --> 00:15:46,070 I'm going to say when you produce a random number say counters elements at that location or random in 198 00:15:46,330 --> 00:15:50,550 location increments yourself all right. 199 00:15:50,590 --> 00:15:54,270 Let me drop one of these and I have to re-explain it really quickly for you. 200 00:15:54,960 --> 00:15:56,750 So I don't hear any of that. 201 00:15:56,760 --> 00:16:00,300 I don't need any of the comments I'd rather opt out. 202 00:16:00,300 --> 00:16:02,700 And this is my code in its entirety. 203 00:16:02,700 --> 00:16:04,370 This is all you have to do. 204 00:16:04,380 --> 00:16:07,980 So let's see what's happening if you're doing something a million times. 205 00:16:08,100 --> 00:16:16,770 Every time we put using a random number between 1 to 6 and we have an array of counters that has elements 206 00:16:16,770 --> 00:16:20,260 from 1 to 6 they are all zeroes. 207 00:16:20,460 --> 00:16:25,910 So when I get the random numbers such as 5 it says counter's elements. 208 00:16:25,920 --> 00:16:31,390 Number five this line this one increased increment yourself. 209 00:16:31,410 --> 00:16:34,980 Increase yourself when I produce another random number. 210 00:16:34,980 --> 00:16:36,000 And that is don't know. 211 00:16:36,010 --> 00:16:40,500 To me it says this small increments yourself go up. 212 00:16:40,500 --> 00:16:44,520 So on and so forth until my entire for loop is finished. 213 00:16:44,700 --> 00:16:52,480 And then at the end I'm going to say print everybody who is there let's run this. 214 00:16:52,570 --> 00:16:57,800 So one happens one hundred sixty six thousand two happens so on and so forth. 215 00:16:58,020 --> 00:17:05,520 So as you can see we started from a more complicated approach with so many if else statements then we 216 00:17:05,520 --> 00:17:11,580 simplify it a little bit by adding a switch then we simplify it a little bit by removing all the separate 217 00:17:11,580 --> 00:17:13,910 counters and putting them all in array. 218 00:17:14,130 --> 00:17:20,940 And then we finally simplify it by not counting them based on if else's statements are rather based 219 00:17:20,940 --> 00:17:23,950 on their index in the array. 220 00:17:24,180 --> 00:17:29,430 So I'm going to keep this here for a second if you want to have another final look you know fix your 221 00:17:29,430 --> 00:17:30,930 code by any chance. 222 00:17:31,420 --> 00:17:33,190 And then I will see you in the next lesson.