A coin toss is usually fair when a coin is tossed it has a

A coin toss is usually fair – when a coin is tossed, it has a 50% chance of landing on heads, and a 50% chance of landing on tails. In this problem, you will design an unfair coin using the Random class and implement it as a method.

Utilize an instance of the random class, Random r = new Random(), to design an unfair coin.

Write a valid Java method (including a method header) that takes no arguments and returns the string “heads” 60% of the time and returns the string “tails” 40% of the time. Your code may use any valid methods in the Random class

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Random;

public class UnfairCoin {

  

   /*

   * This method return head ot tail with probability of 60:40

   */

   public static String toss(){

       Random random = new Random();

       int rand = random.nextInt(5); //0,1,2,3,4 ( 5value)

       if(rand <=2)

           return \"head\"; // returning head for three value out of 5: 0, 1,2

       else

           return \"tail\"; // returning tail for 2 value out of 5 : 3,4

   }

  

   public static void main(String[] args) {

      

      

       System.out.println(\"Toss: \"+toss());

      

   }

}

A coin toss is usually fair – when a coin is tossed, it has a 50% chance of landing on heads, and a 50% chance of landing on tails. In this problem, you will de

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site