1

I am very beginner on Java, still getting passion about. I have been given this exercise: "Write a simulator program that flips a coin: One thousand times then prints out how many time you get tails and how many times you get heads" That is what i have tried to do so far.

import java.util.Random; 
import java.util.regex.Pattern; 

public class coin { 

    public static void main( String [] args ) { 

        Random r = new Random(); 
        Pattern tail = Pattern.compile("Tail+"); 
        Pattern head = Pattern.compile("Head+"); 
        String flips = ""; 

        for (int i = 0; i < 1000; i++) { 
            flips += r.nextInt(100) % 2 == 0 ? "Head" : "Tail"; 
        } 
        String[] heads = head.split( flips ); 
        String[] tails = tail.split( flips ); 
        //Display
        System.out.println("Times head was flipped:" + heads.length); 
        System.out.println("Times tail was flipped:" + tails.length); 
    }
}

The program seems to be working, but it is giving me always an almost pair amount of heads and tails, which the total exceed 1000, at least by 1 or more. Please, someone has any solution of this? Where am I wrong? Thanks

axiom
  • 7,725
  • 3
  • 29
  • 34
Fred882401
  • 13
  • 1
  • 1
  • 3

2 Answers2

4

Rather than appending the result in a String and then splitting the string and counting the occurence of "Head"/"Tail" you can just keep track of the count in separate variables :

int headCount = 0;
int tailCount = 0;

for (int i = 0; i < 1000; i++) {
    if(r.nextInt(100) %2 == 0)
    {
      headCount++;
    }
    else
    {
      tailCount ++;
    }

  System.out.println("Times head was flipped:" + headsCount); 
  System.out.println("Times tail was flipped:" + tailCount); 

}
Kakarot
  • 4,267
  • 2
  • 14
  • 18
1

A coin has two sides, so I really don't see why you would ask the random generator to generate a number between 0 and 100 (exclusive). Between 0 and 2 (exclusive) would be much more logical.

Also, you're being asked to count. Appending strings and then splitting to get the final value is quite a complex and inefficient way to count. You should use an integer instead. Each time you get a 1 from your random, increment a counter. In the end, you have the number of times 1 was returned, and the number of 0 is thus 1000 - this number.

    Random r = new Random(); 
    int heads = 0;
    for (int i = 0; i < 1000; i++) { 
        int side = random.nextInt(2);
        if (side == 1) {
            heads++;
        } 
    } 
    System.out.println("Times head was flipped:" + heads); 
    System.out.println("Times tail was flipped:" + (1000 - heads)); 

It could even be simplified to the follwoing (although this simplification makes the code a bit harder to understand):

    Random r = new Random(); 
    int heads = 0;
    for (int i = 0; i < 1000; i++) { 
        heads += random.nextInt(2);
    } 
    System.out.println("Times head was flipped:" + heads); 
    System.out.println("Times tail was flipped:" + (1000 - heads)); 
JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174