How flipping a coin and display tails and heads counting?

Fred882401

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

JB Nizet

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)); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Flipping a coin and stopping when it lands heads 4 times in a row

From Dev

python - infinite coin flip that stops when number of heads = number of tails

From Dev

python - infinite coin flip that stops when number of heads = number of tails

From Dev

Biased coin flipping?

From Dev

solution for the flipping coin issue

From Dev

Python Heads and Tails

From Dev

How to find the most frequent length of Heads in a Coin Toss?

From Dev

Javascript Image Replace Coin Flipping

From Dev

Coin flipping - returning random outcome

From Dev

How to calculate what is probability of getting same result 8 times in a row, when flipping coin 1000 times?

From Dev

How do I traverse through a linked list after adding tails rather than heads

From Dev

3D Coin Flipping Animation on SKSpriteNode

From Dev

Coin Flipping/Card picking program not working

From Dev

Javascript - Rigged coin flip (heads first)

From Dev

Better than brute force algorithms for a coin-flipping game

From Dev

Better than brute force algorithms for a coin-flipping game

From Dev

How to display a counting of an item from ListView

From Dev

Homework: Simulating coin tosses until consecutive heads using R

From Dev

Coin flip simulation never exceeding a streak of 15 heads

From Dev

How about flipping a jcombobox?

From Dev

How do I make my flipping image display a different image on either side with javascript?

From Dev

Coin-counting game for making change

From Dev

Count substrings with a minority of tails from a string of coin tosses

From Dev

Tails: How to connect Tails to a Samba network share?

From Dev

How do I display a running total for averages and for counting each entry?

From Dev

can someone explain this short segment of C++ code, I can't make heads or tails of it

From Dev

can someone explain this short segment of C++ code, I can't make heads or tails of it

From Dev

Game: nine heads and tails. I am having trouble with binary stuff

From Dev

Using heads or tails to choose a between to sprites which will spawn and to choose the speed that the sprite will move

Related Related

  1. 1

    Flipping a coin and stopping when it lands heads 4 times in a row

  2. 2

    python - infinite coin flip that stops when number of heads = number of tails

  3. 3

    python - infinite coin flip that stops when number of heads = number of tails

  4. 4

    Biased coin flipping?

  5. 5

    solution for the flipping coin issue

  6. 6

    Python Heads and Tails

  7. 7

    How to find the most frequent length of Heads in a Coin Toss?

  8. 8

    Javascript Image Replace Coin Flipping

  9. 9

    Coin flipping - returning random outcome

  10. 10

    How to calculate what is probability of getting same result 8 times in a row, when flipping coin 1000 times?

  11. 11

    How do I traverse through a linked list after adding tails rather than heads

  12. 12

    3D Coin Flipping Animation on SKSpriteNode

  13. 13

    Coin Flipping/Card picking program not working

  14. 14

    Javascript - Rigged coin flip (heads first)

  15. 15

    Better than brute force algorithms for a coin-flipping game

  16. 16

    Better than brute force algorithms for a coin-flipping game

  17. 17

    How to display a counting of an item from ListView

  18. 18

    Homework: Simulating coin tosses until consecutive heads using R

  19. 19

    Coin flip simulation never exceeding a streak of 15 heads

  20. 20

    How about flipping a jcombobox?

  21. 21

    How do I make my flipping image display a different image on either side with javascript?

  22. 22

    Coin-counting game for making change

  23. 23

    Count substrings with a minority of tails from a string of coin tosses

  24. 24

    Tails: How to connect Tails to a Samba network share?

  25. 25

    How do I display a running total for averages and for counting each entry?

  26. 26

    can someone explain this short segment of C++ code, I can't make heads or tails of it

  27. 27

    can someone explain this short segment of C++ code, I can't make heads or tails of it

  28. 28

    Game: nine heads and tails. I am having trouble with binary stuff

  29. 29

    Using heads or tails to choose a between to sprites which will spawn and to choose the speed that the sprite will move

HotTag

Archive