RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

loop practice, cont.
rollsTilGet

In class, we wrote a function to compute how many rolls it takes for 2d6 to roll a given target-number.

  1. Write a function which, named avgRollsTilGet, which calls rollsTilGet a given number of times, and returns the average result.
    That is, if I call this function 3 and 1000, it will call rollsTilGet(3) 1000 times, and return the average of the numbers returned (probably around 18.0).
    math: You can say that we're sampling rollsTilGet 1000 times (or however many times). And, math people would say that rollsTilGet(3) is a random variable — not the same sort of variable we mean in programming.
  2. Write a function avgRolls which, given a desired number of samples (like 1000 or 75), calls avgRollsTilGet with each number from 2..12 (and the desired #samples). This function won't return anything, but will print: 75 tests: rolling 2 took, on average, 36.2738 rolls. 75 tests: rolling 3 took, on average, 17.924917 rolls. 75 tests: rolling 12 took, on average, 35.982 rolls.
  3. Write an overall function — let's call it main1 — which calls avgRolls with Math.pow(2,0), Math.pow(2,1), Math.pow(2,2), … Math.pow(2,10).
    To observe: Each time, as we double the number of samples we take, how quickly do the results seems to converge? Do they converge more quickly for difficult targets like 2, or for targets easy targets like 7?
  4. Submit this last function on D2L as lab05b.
    (It's fine if you have previous functions in the file you submit.)

  5. If you want further things to do, read on…
  6. For cool-style-points, you might read and use java.lang.String#format to pad the #tests, roll, and average-result so that each line is prints with the same width.
  7. Challenge/optional: We've been rolling 2d6. But what if we want to roll 2d20 (two 20-sided dice), or 93d7 (ninety-three 7-sided dice)?

    Generalize these functions to take additional parameters, so they can use any number-of-faces of the die, and also how many copies of the die to roll. (For example, rollsTilGet(5,2,20) would be how many times 2d20 were rolled to get a 5.

    We can do this via overloading:

    1. Make a version of rollsTilGet which takes in three parameters.
      Java pro tip: You now have two functions, both named rollsTilGet. But Java can tell which one you are calling, by whether you are passing it three things, or just one. We say we have overloaded rollsTilGet. If we are talking/writing about them, we can distinguish their names by providing their full signature: rollsTilGet(int) vs. rollsTilGet(int,int,int).
    2. Go and re-write your original rollsTilGet(int) so that it simply calls the three-argument rollsTilGet(int,int,int) with that same parameter, plus 2 and 6. (So it gives the same answer as before, but you don't have any repeated code!)
    3. Generalize avgRollsTilGet similarly.
    4. Inside your class, before any of your methods, declare and initialize two named-constants DEFAULT_NUM_DICE and DEFAULT_NUM_SIDES. Use final so Java knows you intend them as constants (and so it won't let you re-assign them after you initialize them). Also use static, because … well, just because.

      Now, go and change all the 2 and 6's in the following places: The default values used when rollsTilGet(int) calls rollsTilGet(int,int,int), and in main. (You can change these values and re-compile, and see what you get.)

      unknown territory: Although these two named-constants look just like local-variables, they're not. They are fields, and are shared by all methods(functions) inside the class (acting like global-variables, in this case). (And moreover, they're static-fields, which means they are shared by all objects. But we haven't made any objects yet, so ignore that.)

Official Misgiving: As an instructor, I like how random-numbers can make for fun loops, and how we can reason a bit about random variables. But, we can't do any unit-tests on them! That is unfortunate that I seem to not be emphasizing that, because it's a critical skill for programming at all levels.

1 We'll call it main, but you do not need to make it public, nor have it take as any parameters — not even an array-of-strings which you never refer to.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.