3

Briefly, I have textbook lab assignments for this class and I'm currently doing a module on objects. This lab has me using a random number generator but due to auto grading it needs repeatability. It does this by seeding with 2 but I can't seem to get what the lab wants even though my program functions exactly the way it should under normal circumstances. Here's the directions and my code:

Write a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the output is either heads or tails. Assume the input is a value greater than 0.

Ex: If the input is 3, the output is:

tails 
heads
heads

For reproducibility needed for auto-grading, seed the program with a value of 2. In a real program, you would seed with the current time. In that case, every program's output would be different, which is what is desired but can't be auto-graded.

Note: A common student mistake is to create an instance of Random before each call to rand.nextInt(). But seeding should only be done once, at the start of the program, after which rand.nextInt() can be called any number of times.

Your program must define and call the following method that returns "heads" or "tails".

public static String HeadsOrTails(Random rand)

And here's what I have so far:

import java.util.Scanner;
import java.util.Random;

public class LabProgram {

   public static String HeadsOrTails(Random rand) {
      String coinFlipVal = "tails";

      if (rand.nextInt(2) == 0) {
         coinFlipVal = "heads";
         }

      return coinFlipVal;
   }

   public static void main(String[] args) {
      Scanner key = new Scanner(System.in);
      Random randGen = new Random(2); // Unique seed
      int iDecisions = key.nextInt();

      for (int i = 0; i < iDecisions; i++) {
         System.out.println(HeadsOrTails(randGen));
      }
   }
}

This does exactly what it's supposed in that it yields a random set of heads or tails results for however many iterations I want but not in the order that the program is looking for. I've played with my if statement, setting it to 1 instead of zero, using an else if statement for tails and declaring coinFlipVal as "", etc. I just don't know how to get what they're looking for. Any help as to what I'm overlooking is greatly appreciated. And here is the submission results (which gives what the program expects from the output):

1: Compare output 0 / 2 Output differs. See highlights below. Input:

3

Your output

tails
heads
tails

Expected output

tails
heads
heads

2: Unit test 2 / 2 HeadsOrTails() input 1

Your output:

HeadsOrTails() with input 1 correctly returned:
tails

3: Unit test 0 / 3 HeadsOrTails() input 5

Your output:

HeadsOrTails() with input 5 incorrectly returned:
tails
heads
tails
heads
heads

4: Unit test 0 / 3 HeadsOrTails() input 10

Your output:

HeadsOrTails() with input 10 incorrectly returned:
tails
heads
tails
heads
heads
tails
tails
heads
tails
tails
Boog
  • 31
  • 1
  • 3
  • 3
    Try using the nextboolean method in Random. – QuyNguyen2013 Mar 13 '19 at 23:21
  • @QuyNguyen2013 suggestion works – StvnBrkdll Mar 13 '19 at 23:53
  • That does work in the sense that it generates a series of random heads or tails, however my issue is that this program is autograded meaning the system needs a specific random sequence (which is just ridiculous to me). My program did what it should in the first place, just not the exact sequence the grading program wants. As the directions said, seeding with 2 should solve this but I'm still getting a different sequence. – Boog Mar 13 '19 at 23:59
  • ?? Keep looping until expected output equals actual output – Scary Wombat Mar 14 '19 at 00:28
  • 1
    Your instructor hinted to use `rand.nextInt()`, and didn't specify any arguments, so I don't think `nextBoolean()` is the proper approach. Try changing your test from `if (rand.nextInt(2) == 0)` to one of `if ((rand.nextInt() % 2) == 0)` or `if ((rand.nextInt() & 1) == 0)`. Both worked for me for your test case of 3. – pjs Mar 14 '19 at 05:11

2 Answers2

1

I had the same answer as you and got 10/10, the only difference is that I added an else Statement after the if;

public static String headsOrTails(Random rand){
      String coinFlip; // no need to "initialize" as it is definitely assigned just below

      if (rand.nextInt(2) == 0)
         coinFlip = "heads";
      else  // Added code
         coinFlip = "tails";

   return coinFlip;
   }
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
0

This should work

import java.util.Scanner;
import java.util.Random;

public class LabProgram {

public static String headsOrTails(Random rand) {
   String coinflip = "tails";
  if (rand.nextInt(2) == 0) {
     coinflip = "heads";
     }
  return coinflip;
}
public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  Random rand = new Random(2);
  int choice = scnr.nextInt();
  for (int i = 0; i < choice; i++) {
     System.out.println(headsOrTails(rand));
  }
}
}