Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 5.10.
In this workbook, the user must keep rolling dice until they roll doubles.
Task one is to make a function that returns a random number between one and six.
Let's copy the description over here.
So public static.
The return type is int because this function will return a random number.
It does not expect any values to be passed in.
All it's going to do is roll a dice between one and six.
You'll remember from the functions section, the familiar math random function.
What it does is it returns a random decimal greater than or equal to zero and less than one.
So a value in the range of 0 to 0.9999999.
If we multiply the result by six, the minimum value would still be zero, but the maximum decimal that
could be returned from that random will be now scaled to 5.9999.
So this current code will return a random decimal.
Inside this range.
But we want to roll a dice between one and six so we know our minimum needs to be scaled up to one.
Here we'll say a random decimal plus equals one.
By virtue of doing that, assuming a random decimal ended up being the minimum, here it becomes one.
And now assuming that random decimal ended up being the maximum possible value that this code could
return, now it would be 6.99999.
And now if we typecast this decimal before returning it.
Given that this can be any value between one and 6.999, assuming that it was the minimum, it would
still be one.
But if it's the maximum, the decimal would just get cut off and you would get a dice roll between 1
to 6.
All right.
So that was task number one.
Task number two is to call the roll dice function twice and store the values and variables Dice one
and dice two, then print each dice as follows.
So dice one is equal to random number, and dice two is equal to random number.
We seem to already have the print statements over here.
That is perfect and we're off to a great start.
Task number three is to set up a loop that keeps running while the two dice aren't the same.
And during each run we roll the dice and print both values.
So here we'll say while dice one is not equal to dice two, So while the two dice are not the same,
we need to keep running the loop until they are.
And you know what?
Before I continue, I want to call this roll dice instead.
That seems more intuitive to me.
And so whenever this wild loop runs, we need to update our dice rolls.
So here we'll say dice one is equal to a new roll of the dice.
Dice two is equal to a new roll of the dice, and then we can print the updated dice values.
So this wild loop is going to keep running until the dice eventually equal each other.
Eventually it will break and once the while loop breaks, we know that the dice ended up equaling each
other.
They rolled doubles so we can print out of no success or something, or we'll just print you rolled
doubles.
All right, Let us visualize the runtime.
I will put.
Two breakpoints over here and one over here.
And we'll start visualizing.
So when you call a roll dice, this code can return any decimal in the range of 0 to 5.999.
In this case, it returns a decimal of 1.8, add one to it, it becomes 2.8.
Convert the decimal to an integer.
We get a value of two The second dice roll.
Let's step into it.
So this once again can return any number between zero and less than six.
That returns a value of 3.9 and one.
It becomes 4.9 typecast to an end and we have a dice roll of four.
Because dice one does not equal dice two this while loop is going to run, prompting them to roll dice
again.
So entering roll dice again, This can return anything between zero and less than six.
It returns 3.21.
That's four.
Convert that to an integer.
So here we have a dice roll of four.
Let's hope we get four again.
And that is just perfect.
Converting 4.9 to an integer.
We get another dice roll of four.
Here.
We print the first dice.
Here we print the second dice because dice one equals dice two.
This evaluates the false.
So the while loop is not going to run anymore.
It will break and then it's going to print.
You rolled doubles.