Hello I will come again.
This lesson is going to be an exercise on what I want you to do is this I want you to write the program
that the rules a dice for one million times and put uses a number between 1 to 6 just like hard Dyce's.
And that tells us either the numbers of 1 to 6.
How many times they showed up.
So once again run a dice or roll the dice for one million times and find out how many times they'd want
to show up.
How many times did you show up on the Trishul.
So on and so forth.
Now to get us started I'm going to give you one piece of hands or one piece of advice essentially this
jump into X hold in the expert I'm going to start an application and that's going to be a command line
tool.
I'm going to call it dice project and the dice project I'm going to go ahead get my.
Clean it up better formatting and everything in here I'm going to show you one thing and that is if
I have ints random for instance random instead and that's ARTC for random this will essentially will
produce a random number for me.
That's all I'm going to tell you.
So if I say slight whatever you find in the value random ints and 5 run bill then run this application
it will show you that it has produced one a random number.
Let's see that together the random number is minus 10 billion the world is read only again this time
is going to give us a different random number.
That's all I'm going to tell you.
So you can use the arc for random to produce a random number and based on that.
Here is your task.
You have to essentially write a program that runs the dice for a one million times and tells us how
many times 5:59 showed up.
So pause the video here.
Given the choice on your own.
And then once you're done you can just you know work along with me and you can see how I'm going to
do it again.
So I'm going to go back to school and I'm going to show you the solution myself in scores.
I'm going to do this.
I'm going to say my random end which is ARTC for Andong.
And it produces this.
Let's do this.
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
times and then I'm going to go ahead and say let's reinvent everything clean them up.
Number one this is showing me so any random numbers 100 random numbers but none of them is essentially
what I want which is within a range of 1 to 6.
So what I'm doing is I'm going to go ahead and say this random number that you produce.
Let's get a marginal loss of faith against six.
If we do this we get numbers that are between zero to five.
So we get 0 1 2 3 4 5.
Any of those numbers will show up.
But that's not what they want either.
Simply because I want numbers of 1 to 6 I want the ones I want sixes.
I also don't want any zeros such as this one that's going up in here.
So what I'm going to do is I'm going to add the one tweet in here.
I don't remember because of the presidents of the operations this first happens first then that the
spot happens.
If it is difficult Remember you can just pantheists this on here.
So you remember what's going on or you could actually do it the other way on say one plus the odds.
So it's going to produce any number between 0 2 5 1 1 2 8 so essentially becomes any number between
1 to 6.
That's Rhonda.
So now I have all of these different results 5 6 2 6 4 6 1 3 3 to you.
All the numbers are crunched between 1 to 6 which is acceptable numbers for a dice.
Now the next thing we have to do is we have two accounts that we have to find out how many times have
one happen when it comes to who's up in threes.
So on and so forth.
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
counter 2 is also currently 0.
Let me actually call it this.
It's going to be easier for me.
So counter 3 is also 0 count four is also zero and then I count five.
And then I have finally counter 6 so these are the different counters that each of them is going to
count.
One of the many numbers of the of the dice.
So when I go through my for loop I'm going to go ahead.
I'm safe.
If the random INS was one if I won it what should I do.
I should say counter one increment itself and I don't mean to print the values exactly there so I'm
going to draw that.
So let me just get myself a little bit further spacing in here because this is going to be a bit of
a longer call.
I want to make sure that you know I can somehow zoom into everything for you guys.
And now I have to write this if statement.
I know that five times.
So I have to write else if how random it was to then obviously counter to gets incremented it duplicated
so many times.
Coffee coffee coffee coffee coffee.
So if it was true if it was theory then sorry that I was yes.
If it was 3 then three if it became four Dan four if it was five then five.
And then I don't need to really check for six because obviously if it is not one to five it's certainly
the sixth one.
So like this I'm going to run for a loop a hundred times and 100 times I'm going to produce a random
number between 1 to 6 and then I'm going to count them once the for loop is done.
And I have to print them I'm going to say and that's long and I'm going to say what happened.
How many times and times and for that I'm going to use counter one.
So one happened counter one times and of course we have to duplicate this six times as well.
So I'm going to say two three four five six.
And of course that is going to be two three four or half and capital F 5 6 happened that too many times.
So let's run let's see if we get what we want to get.
So it says 1 happened 11 times to have it 19 times so on and so forth.
Let's run this for a million times.
That is one hundred thousand that's 1 million times so almost all of them are happening within Iran.
One hundred sixty six thousand that's kind of makes sense because they all should have a similar chance
of showing up again.
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
at my quote for any reason you can pause there be doing here because I'm going to change all of these
and I'm going to rewrite this record so I'm going to pause here for a few seconds and then I'm going
to rewrite this statement with a switch statement.
OK.
Let's get to it.
So instead of this knowing if else if else if else if a situation delete all of that I'm saying when
you find a new random number switch the switch not the random number random.
And in case it was a one obviously counter one odds to itself and breaks.
And now let's duplicate that case one more time.
It didn't.
OK.
Two three four five and six.
So if when we switch if you got two then counter two should go up.
If we got three then counter two should go up.
If we got four then counter four would go up five out of five.
I have six counter six.
And then of course at the end again you have two princedom we have to find out how many times counter
1 happened how many times counter to this fourth essentially is going to stay intact.
So let's run this go it is working perfectly fine this time as well.
Like I said when we were discussing the switch lesson switches not necessarily less code is just more
organized so you can see it easier in this age if cases when you do this thing.
And if that's the case the break we don't continue with any of these.
And because of that its performance is a little bit better than an if else statement.
So this is writing it with a switch.
So I'm going to keep it here for a second.
And you know what just so that you can all you can see all of the content on my screen.
I'm going to drop that 3 4 5 and 6 so you can see it only for the first three.
And you know the rest of them are exactly identical.
So I'm going to pause the video here for a second and then I'm going to rewrite this another way.
OK.
So now so far we managed to simplify some parts of our switch and now I want to simplify our counters
so I count them individually.
One two three four or five.
And that doesn't really seem like a very intuitive way of doing it.
So I'm going to delete all of this for now I'm going to delete all of that.
As that as I'm going to delete all of these guys for now.
I'm going to go ahead and say I have only one integer up there called Count headers and that is an array
field 6 I'll use.
Which are all of the current Sile's zero.
So these are my counters so my counter's is an array.
This first one is for one.
This one is for two.
This one is for three.
This one is full for this one for a while.
This one is for six.
Now here is what they wanted to do I want to say if I got one then this first one should go up.
Should become one.
If I got one again then the first one should become two three so on and so forth not elements in the
array should increase themselves as they should increments themselves as we find new dice numbers that
they represent them.
However I have a problem and that problem is this the end I don't see my array are numbered 0 1 2 3
4 5 1.
I actually look for 1 to 6.
So because of that I'm going to change my counters to 7 I'm going to imagine that is one at the very
beginning that I don't use for anything.
And then there's another one at the end as well.
So now I've got six elements in my array.
The first one I'm going to completely ignore.
Let's play that minus one.
So we know that that one is always going to be ignored.
I don't use it for anything.
So now if in my switch I get chased one I can say counters one increment yourself if case is on by two
to what am I typing.
And then counters 2 will increment itself.
And of course we break after we do the same thing for all six of them.
So for now I'm going to say two three four five six.
So in case of three then that becomes three in case of four.
Then Number four increases itself in case of five then five increases it's not a case of six then six
increases it's.
So now that they can count up correctly how am I going to show them I'm going to say let's go outside
the for loop.
So that's here.
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
from 1 and then see the smaller require six.
That's the last object in here.
I'm going to say for that C++ to do this and this log
something happens something times.
So what is the first something the first thing you see for instance or what happens how many times it
happens count thare's one time which is see again.
So let's see what I have written.
It says go from 1 to 6.
Say for instance if it is 1 1 happens counter of 1 times 2 happens counter of 2 times so on and so forth.
Let's on this it says one happens one hundred sixty six thousand or so on and so forth all the way to
solve it quickly.
What we have done in my array I have 7 elements ignore the first one from 1 to 6.
Each of them are zeroes.
Whenever we find one count there one this guy counts up.
So for instance this one becomes one and we find that two.
This one becomes one when we find another one.
This one becomes two when we find another one.
This one becomes three so on and so forth they count themselves until the for loop is finished.
So let me reset these viruses and let's try to simplify it to one last time.
So when I look at my statement it feels like one case is one counter one counts up in cases to counter
two counts up all the way to the end.
So how about I do this.
How about I say Well when you produce a random number and remember that number is either 1 2 3 4 5 or
6.
I'm going to say when you produce a random number say counters elements at that location or random in
location increments yourself all right.
Let me drop one of these and I have to re-explain it really quickly for you.
So I don't hear any of that.
I don't need any of the comments I'd rather opt out.
And this is my code in its entirety.
This is all you have to do.
So let's see what's happening if you're doing something a million times.
Every time we put using a random number between 1 to 6 and we have an array of counters that has elements
from 1 to 6 they are all zeroes.
So when I get the random numbers such as 5 it says counter's elements.
Number five this line this one increased increment yourself.
Increase yourself when I produce another random number.
And that is don't know.
To me it says this small increments yourself go up.
So on and so forth until my entire for loop is finished.
And then at the end I'm going to say print everybody who is there let's run this.
So one happens one hundred sixty six thousand two happens so on and so forth.
So as you can see we started from a more complicated approach with so many if else statements then we
simplify it a little bit by adding a switch then we simplify it a little bit by removing all the separate
counters and putting them all in array.
And then we finally simplify it by not counting them based on if else's statements are rather based
on their index in the array.
So I'm going to keep this here for a second if you want to have another final look you know fix your
code by any chance.
And then I will see you in the next lesson.