Class 12 — Monday, October 5

A die you can check: a seed makes a Random roll the same way every run.

Wednesday’s builder could be changed in place. Today’s object changes every time you ask it for something: a die.

[CU] 3.2 — Creating Objects and [CU] 3.3 — Random Numbers were due today, and Quiz 4 is on them. A5 is due Tuesday, October 6.

Outline

  1. Two dice: predict, then run — one you can check, one you cannot
  2. A die you can check — pairs, at the keyboard
  3. One generator or two — a stop in the middle
  4. More dice — pairs again
  5. Keep going — two more
  6. Before Wednesday

1. Two dice: predict, then run

Write your prediction in the Journal before anyone runs anything.

Coles 3.3: nextInt(1, 7) is a die — the first number is included, the second left out. new Random(42) makes a generator with a seed, 42; new Random(), with nothing between the parentheses, makes one without.

import java.util.Random;

void main() {
    Random r = new Random(42);
    IO.println(r.nextInt(1, 7));
    IO.println(r.nextInt(1, 7));
    IO.println(new Random().nextInt(1, 7));
}

Run it three times. Which of the three lines change from run to run? Do the first two print the same number?

The first two lines print 3, then 4, on every run. The third is anyone’s guess.

directory          heap
r  →  1010         1010: generator, seed 42 --- next up: 3

new Random(42) makes a thing in the heap that holds where it is in its sequence, and the seed is where it starts. Each nextInt gives the next number and moves the generator on — the thing at 1010 changes, the way the builder at 1007 did on Wednesday:

r  →  1010         1010: generator, seed 42 --- next up: 4

new Random() picks a seed of its own each time it is made, so nobody can say what it rolls next.

2. A die you can check

Pairs. One keyboard. The navigator, who is not typing, says the examples first — each Check.is line, its answer worked out before any of the function exists — and then the steps the function takes. The driver types both, and decides how each step becomes Java. Swap at every function. If either of you is stuck, put away the roles and the keyboard for a moment and discuss.

What you write with your partner in class may go in both your files.

In IDEA, next to Builders.java: a new file Dice.java, starting with import java.util.Random;, and a main that ends in Check.done(). Check.java is already there. No project? Clone the starter again with the steps from Class 9, or use the OneCompiler page with Stripe.java emptied out: https://onecompiler.com/java/4544z33u6.

Each function gets a // line above it with a purpose statement and at least two Check.is lines in main.

Where a check’s number comes from. Not from your function: from the generator itself. This prints the first roll of seed 7, and it is what your check on seed 7 expects:

IO.println(new Random(7).nextInt(1, 7));

roll

int roll(Random r) — one roll of a six-sided die, from the generator it is given. roll(new Random(42)) is 3.

Done when two checks pass: seed 42, and a seed of your own.

rollTwo

int rollTwo(Random r) — two rolls on the same generator, added. rollTwo(new Random(42)) is 7. Two calls to roll on one r.

Done when two checks pass, and the second is a seed of your own.

report

String report(Random r) — rolls twice, and gives back both rolls and their sum as one line of text: report(new Random(42)) is 3 + 4 = 7. rollTwo cannot help here: it gives back only the sum, and calling it rolls two new dice. Keep each roll in a name of its own — int a = roll(r); — and build the line from the names.

Done when two checks pass.

The real die

After Check.done(), print report(new Random()), a die with no seed. Run the program three times.

Done when the checks pass on every run, and the three last lines are not all the same.

3. One generator or two

Keyboards stop. Predict first: which of the four checks pass?

Random r = new Random(42);
Check.is(roll(r), 3);
Check.is(roll(r), 3);

Check.is(roll(new Random(42)), 3);
Check.is(roll(new Random(42)), 3);

The second check fails — got 4, expected 3 — and the other three pass.

directory          heap
r  →  1011         1011: generator, seed 42      3, then 4
                   1012: generator, seed 42      3
                   1013: generator, seed 42      3

The first two checks share one generator, so the second roll is the next number in its sequence. Each of the last two makes a generator of its own, and two generators from the same seed start in the same place.

So each check gets its own new Random(42). That is also why rollTwo is roll(r) + roll(r) and not 2 * roll(r): the generator at r moves on between the two calls.

4. More dice

Pairs, the same way: the navigator’s examples and steps first, then the driver’s Java.

between

int between(Random r, int lo, int hi) — a whole number from lo to hi, both included. between(new Random(42), 10, 20) is 17.

Done when that check passes, and a second one compares between(new Random(42), 1, 6) with roll(new Random(42)) — two of your own functions agreeing.

pick

String pick(Random r, String s) — one character of s, as a text, every position equally likely. pick(new Random(42), "quokka") is "o". Positions count from 0, like substring, and the last one is length() - 1.

Done when two checks pass, and the second is on another word. Its number comes from the generator, its character from counting by hand.

doubles

boolean doubles(Random r) — whether two rolls on the same generator come up the same. doubles(new Random(42)) is false.

Done when a second check is true: find a seed that rolls doubles, and say in a // comment how you knew before running doubles.

5. Keep going

code

String code(Random r) — two capital letters and a digit, from "ABCDEFGHJKLMNPQRSTUVWXYZ" and "23456789": code(new Random(42)) is "CD3". One generator through three picks.

Done when a check on seed 7 passes, with its expected value worked out first: which three numbers does the generator give, and from which ranges?

same

boolean same(int seed) — whether two generators made from seed give the same first roll.

Done when checks on three seeds pass, and a // comment says why no seed makes it false.

6. Before Wednesday

A5 is due Tuesday, October 6.