math.Random isn't working right
Posted
by
RandomlyKnighted
on Stack Overflow
See other posts from Stack Overflow
or by RandomlyKnighted
Published on 2012-09-21T15:34:41Z
Indexed on
2012/09/21
15:37 UTC
Read the original article
Hit count: 126
I'm trying to simulate a coin flip using the code below.
public class Coin
{
public static double result;
int[] count = new count[2];
public static void flip()
{
result = Math.random();
}
public static boolean isHeads()
{
if (result == 0.0)
{
count[0]++;
return false;
}
else
{
count[1]++;
return true;
}
}
public static void main(String[] args)
{
flip();
isHeads();
System.out.println(count[0]);
System.out.println(count[1]);
}
}
For some reason Eclipse says that the
import java.util.Random;
is never used even though I'm clearly using it. I didn't put my for loop into the code above but it loops n number of times and then outputs the result. No matter how many times it loops it always returns that the result is greater than 0.0 which can't be right. Am I calling Math.random incorrectly?
© Stack Overflow or respective owner