title: Problems Generating A Math.random Number, Either 0 or 1
body: <p>I want a random number, either 0 or 1 and then that will be returned to main() as in my code below.</p>
  <pre><code>import java.util.Scanner;
  public class Exercise8Lab7 {
      public static void main(String[] args) {
  
          Scanner input = new Scanner(System.in);
  
          int numFlips = 0;
          int heads = 0;
          int tails = 0;
          String answer;
  
  
          System.out.print(&quot;Please Enter The Number Of Coin Tosses You Want: &quot;);
          numFlips = input.nextInt();
  
          for(int x = 1;x &lt;= numFlips; x++){
              if(coinToss() == 1){
                  answer = &quot;Tails&quot;;
                  tails++;
              }
              else{
                  answer = &quot;Heads&quot;;
                  heads++;
              }
              System.out.print(&quot;\nCoin Toss &quot; + x + &quot;: &quot; + answer);
          }
          System.out.println(&quot;\n\n====== Overall Results ======&quot; +
          &quot;\nPercentage Of Heads: &quot; + (heads/numFlips)*100 + &quot;\nPercentage Of Tails: &quot; + (tails/numFlips)*100);
      }
  
      public static int coinToss(){
          double rAsFloat = 1 * (2 + Math.random( ) );
          int r = (int)rAsFloat;
          return r;
      }
  }
  </code></pre>
  <p>Many solutions had been suggested to use the util.Random option which I have done and works perfectly but I want to sort out why I can't get this to work. Obviously I want the number to be an int myself so I convert it to an int after the random number has been generated. But no matter what I add or multiply the Math.random() by, it will always all either be Heads or all either be Tails. Never mixed.</p>