我試圖用下面的代碼來模擬硬幣翻轉。math.Random不能正常工作
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]);
}
}
出於某種原因,Eclipse中說,
導入了java.util.Random;
從來沒有使用,即使我明確使用它。我沒有把我的for循環放到上面的代碼中,但它循環n次,然後輸出結果。無論它循環多少次,它總是返回結果大於0.0,這是不正確的。我是不是正確地調用Math.random?
數學。Random和java.util.Random不是一回事! – gtgaxiola
他們是不同的東西,Eclipse是正確的。 – Arran
'result == 0.0'比我想象的要少得多。 – Flexo