A simulation the game of craps java -


this code supposed simulating game of craps: getting right win , losses turn out can't probability correct. suggestions?

please instructions are: in game of craps, pass line bet proceeds follows. 2 six-sided dice rolled; first roll of dice in craps round called “come out roll.” come-out roll of 7 or 11 automatically wins, , come out roll of 2, 3, or 12 automatically loses. if 4, 5, 6, 8, 9, or 10 rolled on come out roll, number becomes point. player keeps rolling dice until either 7 or point rolled. if point rolled first, player wins bet. if 7 rolled first, player loses. write program simulates game of craps using these rules without human input. instead of asking wager, program should calculate whether player win or lose.

the program should simulate rolling 2 dice , calculate sum. add loop program plays 10,000 games.add counters count how many times player wins, , how many times player loses. @ end of 10,000 games, compute probability of winning [i.e., wins / (wins + losses)] , output value. on long run, going win games, or house? note: generate random number x, 0 x ≤< 1, use x = math.random(); . example, multiplying 6 , converting integer results in integer between 0 , 5.

public class dice  {     public static void main(string[]args)     {         //declaring variables         int comeoutroll1, comeoutroll2;         int roll1, roll2;         int numw, numl;         int sum, sum2 = 0;         int thepoint = 0;         double probability;            //initializing variables         comeoutroll1 = (int)(math.random()*5);         comeoutroll2 = (int)(math.random()*5);         sum = comeoutroll1 + comeoutroll2;         numw = 0;         numl = 0;           for(int timesplayed = 0; timesplayed <= 10000; timesplayed++)         {              switch(sum)             {             //adds how many wins , losses             case 2:                 numl = numl +1;                 break;             case 3:                 numl = numl + 1;                 break;             case 12:                 numl = numl + 1;             break;             case 7:                 numw = numw +1;                 break;             case 11:                 numw = numw +1;                 break;             case 4:                 thepoint = sum;                 break;             case 5:                 thepoint = sum;                 break;             case 6:                 thepoint = sum;                 break;             case 8:                 thepoint = sum;                 break;             case 9:                 thepoint = sum;                 break;             case 10:                 thepoint = sum;             break;             //if not of these cases roll again             default:                  roll1 = (int)(math.random()*5);                 roll2 = (int)(math.random()*5);                 sum2 = roll1 + roll2;                 break;             }              if(sum2 == thepoint)             {                 numw = numw +1;             }             else if(sum2 == 7)             {                 numl = numl +1;             }         }           probability = (numw/(numw+numl));          system.out.println("number of wins: " + numw);         system.out.println("number of losses: " + numl);         system.out.println("the probability of winning is: " + probability + " percent");          }  } 

2 problems...

  1. you analyse 1 (random) come out roll. need loop 10000 times on whole game, not secondary rolls, should looped until result.
  2. you're doing integer division (which truncates leave whole number). use floating point math instead casting 1 of numbers float or double. ie probability = (float)numw/(numw+numl);

in pseudo code, using helper methods:

// returns random integer between 1 , 6 inclusive method rolldie()  // returns sum of random roll of 2 dice method rolldice()     return rolldie() + rolldie()   // return true if player won given point method won(point)     roll = rolldice()     if roll == 7 return false     if roll == point return true     return won(point)  // main define wins variable (you don't need losses variable. losses = 10000 - wins  loop 10000 times {     comeout = rolldice()     if comeout in (7, 11) or (comeout not in (2, 3 or 12) , won(comeout))         wins++ } probability = (float)wins/10000 

convert above java , should go (and you'll learn - see dry).


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -