1 00:00:00,210 --> 00:00:03,810 Before we start, did you try solving the workbook yourself? 2 00:00:03,840 --> 00:00:09,600 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,890 --> 00:00:13,190 Welcome to Workbook 5.10. 4 00:00:13,200 --> 00:00:17,670 In this workbook, the user must keep rolling dice until they roll doubles. 5 00:00:17,700 --> 00:00:22,320 Task one is to make a function that returns a random number between one and six. 6 00:00:22,320 --> 00:00:25,320 Let's copy the description over here. 7 00:00:26,930 --> 00:00:28,750 So public static. 8 00:00:28,760 --> 00:00:33,980 The return type is int because this function will return a random number. 9 00:00:34,070 --> 00:00:37,040 It does not expect any values to be passed in. 10 00:00:37,040 --> 00:00:40,700 All it's going to do is roll a dice between one and six. 11 00:00:40,730 --> 00:00:45,590 You'll remember from the functions section, the familiar math random function. 12 00:00:45,590 --> 00:00:51,380 What it does is it returns a random decimal greater than or equal to zero and less than one. 13 00:00:51,380 --> 00:00:57,050 So a value in the range of 0 to 0.9999999. 14 00:00:57,050 --> 00:01:03,800 If we multiply the result by six, the minimum value would still be zero, but the maximum decimal that 15 00:01:03,800 --> 00:01:09,830 could be returned from that random will be now scaled to 5.9999. 16 00:01:09,830 --> 00:01:14,540 So this current code will return a random decimal. 17 00:01:17,290 --> 00:01:19,510 Inside this range. 18 00:01:22,040 --> 00:01:28,280 But we want to roll a dice between one and six so we know our minimum needs to be scaled up to one. 19 00:01:28,310 --> 00:01:31,340 Here we'll say a random decimal plus equals one. 20 00:01:31,670 --> 00:01:38,180 By virtue of doing that, assuming a random decimal ended up being the minimum, here it becomes one. 21 00:01:38,360 --> 00:01:44,180 And now assuming that random decimal ended up being the maximum possible value that this code could 22 00:01:44,180 --> 00:01:48,950 return, now it would be 6.99999. 23 00:01:50,560 --> 00:01:54,250 And now if we typecast this decimal before returning it. 24 00:01:56,790 --> 00:02:03,240 Given that this can be any value between one and 6.999, assuming that it was the minimum, it would 25 00:02:03,240 --> 00:02:04,140 still be one. 26 00:02:04,140 --> 00:02:10,280 But if it's the maximum, the decimal would just get cut off and you would get a dice roll between 1 27 00:02:10,280 --> 00:02:11,100 to 6. 28 00:02:11,430 --> 00:02:12,690 All right. 29 00:02:13,630 --> 00:02:16,420 So that was task number one. 30 00:02:17,340 --> 00:02:24,000 Task number two is to call the roll dice function twice and store the values and variables Dice one 31 00:02:24,000 --> 00:02:27,930 and dice two, then print each dice as follows. 32 00:02:28,240 --> 00:02:35,430 So dice one is equal to random number, and dice two is equal to random number. 33 00:02:35,670 --> 00:02:38,340 We seem to already have the print statements over here. 34 00:02:38,340 --> 00:02:43,070 That is perfect and we're off to a great start. 35 00:02:43,080 --> 00:02:47,910 Task number three is to set up a loop that keeps running while the two dice aren't the same. 36 00:02:47,910 --> 00:02:52,560 And during each run we roll the dice and print both values. 37 00:02:52,560 --> 00:03:01,470 So here we'll say while dice one is not equal to dice two, So while the two dice are not the same, 38 00:03:01,470 --> 00:03:04,440 we need to keep running the loop until they are. 39 00:03:04,440 --> 00:03:05,140 And you know what? 40 00:03:05,160 --> 00:03:09,000 Before I continue, I want to call this roll dice instead. 41 00:03:09,030 --> 00:03:11,730 That seems more intuitive to me. 42 00:03:12,430 --> 00:03:17,170 And so whenever this wild loop runs, we need to update our dice rolls. 43 00:03:17,170 --> 00:03:21,850 So here we'll say dice one is equal to a new roll of the dice. 44 00:03:22,240 --> 00:03:31,000 Dice two is equal to a new roll of the dice, and then we can print the updated dice values. 45 00:03:34,030 --> 00:03:39,270 So this wild loop is going to keep running until the dice eventually equal each other. 46 00:03:39,280 --> 00:03:45,730 Eventually it will break and once the while loop breaks, we know that the dice ended up equaling each 47 00:03:45,730 --> 00:03:46,060 other. 48 00:03:46,060 --> 00:03:52,810 They rolled doubles so we can print out of no success or something, or we'll just print you rolled 49 00:03:52,810 --> 00:03:53,680 doubles. 50 00:03:57,250 --> 00:03:59,210 All right, Let us visualize the runtime. 51 00:03:59,230 --> 00:04:00,880 I will put. 52 00:04:01,850 --> 00:04:05,030 Two breakpoints over here and one over here. 53 00:04:05,030 --> 00:04:06,920 And we'll start visualizing. 54 00:04:13,240 --> 00:04:20,709 So when you call a roll dice, this code can return any decimal in the range of 0 to 5.999. 55 00:04:20,740 --> 00:04:27,340 In this case, it returns a decimal of 1.8, add one to it, it becomes 2.8. 56 00:04:27,550 --> 00:04:29,530 Convert the decimal to an integer. 57 00:04:29,530 --> 00:04:33,370 We get a value of two The second dice roll. 58 00:04:33,370 --> 00:04:34,810 Let's step into it. 59 00:04:35,650 --> 00:04:41,170 So this once again can return any number between zero and less than six. 60 00:04:41,920 --> 00:04:44,950 That returns a value of 3.9 and one. 61 00:04:44,950 --> 00:04:50,140 It becomes 4.9 typecast to an end and we have a dice roll of four. 62 00:04:52,910 --> 00:05:00,230 Because dice one does not equal dice two this while loop is going to run, prompting them to roll dice 63 00:05:00,230 --> 00:05:00,920 again. 64 00:05:00,920 --> 00:05:07,430 So entering roll dice again, This can return anything between zero and less than six. 65 00:05:07,610 --> 00:05:10,580 It returns 3.21. 66 00:05:10,610 --> 00:05:11,600 That's four. 67 00:05:11,630 --> 00:05:13,610 Convert that to an integer. 68 00:05:14,640 --> 00:05:17,280 So here we have a dice roll of four. 69 00:05:17,970 --> 00:05:20,040 Let's hope we get four again. 70 00:05:22,630 --> 00:05:24,250 And that is just perfect. 71 00:05:24,250 --> 00:05:26,820 Converting 4.9 to an integer. 72 00:05:26,830 --> 00:05:29,470 We get another dice roll of four. 73 00:05:31,860 --> 00:05:32,190 Here. 74 00:05:32,190 --> 00:05:33,420 We print the first dice. 75 00:05:33,420 --> 00:05:38,470 Here we print the second dice because dice one equals dice two. 76 00:05:38,490 --> 00:05:39,960 This evaluates the false. 77 00:05:39,960 --> 00:05:42,420 So the while loop is not going to run anymore. 78 00:05:42,450 --> 00:05:45,450 It will break and then it's going to print. 79 00:05:45,450 --> 00:05:47,130 You rolled doubles.