Coin toss simulation/game in Java

coin toss simulator - java
java program that simulates flipping a coin
flip a coin 1000 times java
coin flip java assignment
flip a coin n times java
flip coin and print percentage of heads and tails in java
tossing coins for a dollar java
coin java

I'm currently doing a course in computer science which is technically for beginners, but I feel all the assignments they are giving me are more complicated than I can handle.

This assignment asks me to make a coin toss simulation. Three people play the game and each of them has specific requirements to win:

  • One wins if there are 2 results head and the total number of tosses is even
  • One wins if there are 2 results tails and the total number of tosses is even
  • One wins if there are 2 results tails OR heads and the total number of tosses is odd

What I should do is run an experiment 300 times. Each time I should determine who of the three won and how many times. And I'm really stuck. I barely have any code but I do have an very basic idea of what the code should be like, but I can't put it into Java language.

For my assignment, I need to display the binary sequence that caused a person to win.

My idea:

  1. Initialize a counter (1, 2, 3) for every person so that I can keep track of how many times they win
  2. Initialize a head count and a tails count to keep track of the randomly generated sequence
  3. Use a for loop for practically the whole experiment. There should be an outer loop that defines the experiment should be run 300 times and the inner loop should contain the if statements that check who has won. There should be a counter in every if statement, so that I can update it for every person. And I need a System.out.println(); to print every single result of the experiment.
  4. Print the final outcome of the experiment, using the previously determined counters

EDIT: I've tried to change what everyone said and it does look better now, thanks! But it's still not doing what it's supposed to do and I wonder why. I run the code and I only get one output; not the 300 times I want it to run. Furthermore, the counters at the end don't hold the information about who has won, they reset every single time. And sometimes two people win, while that should not be possible. Could anyone clarify this?

EDIT: This is another update of the code. I believe it does run 300 times now! A problem now is the output. Sometimes it says; 1 Bernie wins, while you obviously need at least 2 results for Bernie to win. Also, it says 1 Penny wins, while that also shouldn't be possible. Did I mess something up in my if statements?

import java.util.Random;

public class Assignment3e
public static void main(String[] args)
    int counter1 = 0;
    int counter2 = 0;
    int counter3 = 0;

    Random coin = new Random();

    for(int i = 0; i <= 300; i++){
        int headCount = 0;
        int tailsCount = 0;
        for(int coinToss = 0; coinToss <= 3; coinToss++){
            int random = (int) (Math.random() * 6) + 1;
            String binary = Integer.toBinaryString(random);

          boolean result = coin.nextBoolean();
         if(headCount == 2 && binary.length() % 2 ==0){
                //Amy wins
                counter1 = counter1 + 1;
                System.out.println(binary + " Amy wins.");
            else if(tailsCount == 2 && binary.length() % 2 == 0){
                //Penny wins
                counter2 = counter2 + 1;
                System.out.println(binary + " Penny wins.");
            else if(headCount == 2 || tailsCount == 2 && binary.length() % 2 != 0){
                //Bernie wins
                counter3 = counter3 + 1;
                System.out.println(binary + " Bernie wins.");
        System.out.println("Amy wins " + counter1 + " times.");
        System.out.println("Penny wins " + counter2 + " times.");
        System.out.println("Bernie wins " + counter3 + " times.");


You almost got it right.

This part is not relevant anymore since OP add a requirement in comments.

Your second for loop only need to run a maximum of 3 times as there will be at least 2 heads or 2 tails, not 10.

Edit: you can do it with a maximun of 2 tosses: if there is 1 head and 1 tail, the winner is the 3rd player (Bernie)

Now, on each iteration, you can draw a random decimal number, between 0 and 1 and assume if it's over 0.5 it's a head so you increment the headCount, else you increment the tailCount. Then you can perform the test on the counters (and the number of tosses).

It's easier than using a binary String.

Edit: To answer your comment: Look where you initialized the headCountand tailCount. Remember that once one has won, those counters must be reset.

As it's homework, I won't post code :)

Edit: OP jsut add a comment stating he need to print the sequence that lead to the result of each game.

We assume a binary 1 is a head, and a 0 is a tail. You need to toss the coin 3 times to know the sequence if Bernie wins. So the integer value random which is converted in binary string only need to be made of 3 bits (1 bit = 1 toss).

random can only take values between 0 and 7.

As you get all values in one shot, you don't need the coinToss for loop anymore. All you need to do is to check if the beginning of the binary string matches any win pattern (11 for 2 heads, 00 for 2 tails, else Bernie wins)

6.16. (Part 1) Coin Toss Simulator - Java, Check out the following source code for a simple coin toss game written in Java. This game program illustrates the use of Random class and enumerators in  This game program illustrates the use of Random class and enumerators in Java. The program asks the user to guess the coin toss and then compares the value with the actual coin toss result. Coin toss program runs the game in an infinite loop until the user decides to quit by entering q.

How to Write a Coin Toss Game in Java, public class CoinToss { public static void main(String[] args) { // Create a Random Random rand = new Random(); // Simulate the coin tosses. for (int count = 0;  A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up (“heads” or “tails”) and sets the sideUp field accordingly. A method named getSideUp that returns the value of the sideUp field. Write a program that demonstrates the Coin class.

If you're using a Random() object anyways for the coin nextBoolean(), why not replace the int random = (int) (Math.random() * 6) + 1; with int random = coin.nextInt([range here])?, Let's say we have a coin and 10 chances. Here, we will first initialize the values for head, tail and chances −int heads = 0; int tails = 0;  This Java program is used to toss a coin using Java random class. Java Math.random () returns a random value between 0.0 and 1.0 each time. If value is below 0.5 then it's Heads or otherwise Tails.

Coin toss simulator, Coin toss simulation/game in Java. After moving the object six times, print its final location. Each time the coin is tossed, display the side that is facing up. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Coin toss simulation/game in Java. 0. Coin toss using

Java Program to Toss a Coin, If the balance is exactly one dollar, you win the game. You lose if your balance exceeds one dollar. I wrote the java code but cannot get it to work, I am stuck on  Coin Flipper. This form allows you to flip virtual coins. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.

Toss a coin program in java, The Coin class should have the following member variable: A string named sideUp. During each round of the game, the program will toss the simulated coins. Online virtual coin toss simulation app. Simulate a random coin flip or coin toss to make those hard 50/50 decisions from your mobile Android, iPhone, or Blackberry phone or desktop web browser.