jeudi 8 octobre 2015

Monty Hall Lets Make A Deal Java Simulation 10000 iterations

Im to create a program that runs Monty Hall's lets make a deal 10000 times and outputs the following statistics:

  • the number of wins versus losses
  • the number of times the player switched and won versus staying and winning.
  • display the percentage of wins versus losses when switching and staying.

the desired output for the switching and winning vs staying and winning should be around 2/3 when switching. Which im getting, but only half of it; 33% when switching and 16% when staying. I don't know why the other 50% isn't showing up. Pretty sure it has something to do with my 2nd switch statement but can't figure it out (probably due to lack of sleep).

What am I doing wrong? thanks in advance.

                int iterations; 
                for (iterations = 0; iterations < 10000; iterations++)
                {
                    int prizeIs = (int) ((Math.random() * 3) + 1);  
                    int compChoice = (int) ((Math.random() * 3) + 1);
                    int zonkIs = 0;
                    if ( prizeIs == compChoice )
                    {
                            boolean chooseFirstZonk = Math.random() < 0.5; // 50% chance
                            switch ( prizeIs ) 
                            {
                                case 1: if ( chooseFirstZonk ) 
                                        {
                                            zonkIs = 2;
                                        }    
                                        else 
                                        {
                                            zonkIs = 3;
                                        }
                                        break;
                                case 2: if ( chooseFirstZonk ) 
                                        {
                                            zonkIs = 1;
                                        }    
                                        else 
                                        {
                                            zonkIs = 3;
                                        }
                                        break;
                                case 3: if ( chooseFirstZonk ) 
                                        {
                                            zonkIs = 1;
                                        } 
                                        else 
                                        {
                                            zonkIs = 2;
                                        }
                                        break;
                                }
                        }
                        else if (prizeIs == 1 && compChoice == 2)
                        {
                            zonkIs = 3; 
                        }
                        else if (prizeIs == 1 && compChoice == 3 )
                        {
                            zonkIs = 2; 
                        }
                        else if (prizeIs == 2 && compChoice == 1 )
                        {
                            zonkIs = 3;
                        }
                        else if (prizeIs == 2 && compChoice == 3 )
                        {
                            zonkIs = 1;
                        }
                        else if (prizeIs == 3 && compChoice == 1 )
                        {
                            zonkIs = 2;
                        }
                        else if (prizeIs == 3 && compChoice == 2 )
                        {
                            zonkIs = 1;
                        }

                        int switchDoor = (int) (1 +  (Math.random() * 2));

                        switch ( switchDoor ) 
                        {
                            case 1:  
                                    {
                                        if (compChoice == prizeIs)
                                        {
                                            noSwitch++;
                                            wins++;
                                            games++;
                                        }
                                        else
                                        {
                                            games++;
                                        }
                                    }    
                                    break;
                            //switch door
                            case 2:
                                    {
                                        if (compChoice == prizeIs)
                                        {
                                            games++;
                                        }
                                        else if(compChoice != prizeIs)
                                        {

                                            switches++;
                                            wins++;
                                            games++;
                                        }

                                    }
                        }

                }
                if (iterations == 10000)
                {

                    double percentage = 100.0 * (switches/games);
                    double noswitchpercentage = 100.0 *(noSwitch/games);
                    System.out.println( "Switch percentage : " + percentage);
                    System.out.println( "No Switch percentage : " + noswitchpercentage);
                    System.out.println( "wins : "+ wins);
                    System.out.println("losses : " + (games - wins));
                    break;
                }

Aucun commentaire:

Enregistrer un commentaire