【問題】請問有關亂數造成的機率問題


Recommended Posts

public class CrapsTest

{

public static void main( String args[] )

{

int times=0;

Craps game = new Craps();

for(int i=1;i<=1000;i++)

{

game.play(); // play one game of craps

times+=game.rollNum;

}

game.result();

System.out.printf("The average length is %f\n", (double)times/1000.0);

} // end main

} // end class CrapsTest

import java.util.Random;

public class Craps

{

// create random number generator for use in method rollDice

private Random randomNumbers = new Random();

// enumeration with constants that represent the game status

private enum Status { CONTINUE, WON, LOST };

// constants that represent common rolls of the dice

private final static int SNAKE_EYES = 2;

private final static int TREY = 3;

private final static int SEVEN = 7;

private final static int YO_LEVEN = 11;

private final static int BOX_CARS = 12;

private int roll;

public int rollNum;

private int w[]=new int[1001];

private int l[]=new int[1001];

private int winchances;

private int wtotal;

private int wsum,lsum;

private int sumOfWin,sumOfAll;

// plays one game of craps

public void play()

{

int myPoint = 0; // point if no win or loss on first roll

Status gameStatus; // can contain CONTINUE, WON or LOST

int sumOfDice = rollDice(); // first roll of the dice

// determine game status and point based on first roll

switch ( sumOfDice )

{

case SEVEN: // win with 7 on first roll

case YO_LEVEN: // win with 11 on first roll

gameStatus = Status.WON;

++w[roll];

break;

case SNAKE_EYES: // lose with 2 on first roll

case TREY: // lose with 3 on first roll

case BOX_CARS: // lose with 12 on first roll

gameStatus = Status.LOST;

++l[roll];

break;

default: // did not win or lose, so remember point

gameStatus = Status.CONTINUE; // game is not over

myPoint = sumOfDice; // remember the point

System.out.printf( "Point is %d\n", myPoint );

break; // optional at end of switch

} // end switch

// while game is not complete

while ( gameStatus == Status.CONTINUE ) // not WON or LOST

{

sumOfDice = rollDice(); // roll dice again

// determine game status

if ( sumOfDice == myPoint ) // win by making point

{

gameStatus = Status.WON;

w[roll]++;

}

else

if ( sumOfDice == SEVEN ) // lose by rolling 7 before point

{

gameStatus = Status.LOST;

l[roll]++;

}

} // end while

// display won or lost message

if ( gameStatus == Status.WON )

{

w[roll]++;

System.out.println( "Player wins" );

}

else

{

l[roll]++;

System.out.println( "Player loses" );

}

rollNum=roll;

roll=0;

} // end method play

public void result()

{

System.out.printf("win\n");

for(int g=1;g<21;g++)

{

System.out.printf("%3d%10d\n",g,w[g]);

}

for(int c = 21;c<=1000;c++)

{

wsum += w[c];

}System.out.printf("else:%10d\n",wsum);

System.out.println();

System.out.printf("lose\n");

for(int m=1;m<21;m++)

{

System.out.printf("%3d%10d\n",m,l[m]);

}

for(int c = 21;c<=1000;c++)

{lsum += l[c];

}System.out.printf("else:%10d\n",lsum);

System.out.println();

for(int c = 1;c<=1000;c++)

sumOfWin +=l[c];

for(int c = 1;c<=1000;c++)

sumOfAll += (w[c] + l[c]);

double winchance = (double) sumOfWin / (double)sumOfAll;

System.out.printf("The chance to win is %f\n",winchance);

}

// roll dice, calculate sum and display results

public int rollDice()

{

// pick random die values

int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll

int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll

int sum = die1 + die2; // sum of die values

roll++;

// display results of this roll

System.out.printf( "Player rolled %d + %d = %d\n",

die1, die2, sum );

return sum; // return sum of dice

} // end method rollDice

} // end class Craps

有沒有比較簡短的寫法

The chance to win 好像是0.4~但是我CONPILE後常常出現0.5~,出現0.4~的機會很少耶

這樣寫有錯嗎?

鏈接文章
分享到其他網站

請登入後來留意見

在登入之後,您才能留意見



立即登入