• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Monty Hall Simulator for 3 items

Byers Dec 23rd, 2011 160 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /**
2. * Monty Hall Simulator
3. * Shows the probabilty of getting the price if you do not change your choice or if you do
4. * The Program starts with a random "door" array, with two fails (0's) and one price (1)
5. * The "user" choices random position, then the program deletes one fail (turning it into -1)
6. * and the user changes his choice (successIfChange) and test whether he gets the price or not
7. * same test is done simultaneosly choicing not to change his choice ()
8. * then the success rate for both cases are shown for a  big amount of tries
9. * For customizing the numbr of tries, edit the NUMBER_OF_TRIES constant
10. * @author E.A [agua15@eresmas.com]
11. */
12.
13. int NUMBER_OF_TRIES=1000000;
14.
15. /**
16. * Returns a random array of 3 positions with two fails(0's) and one price (1)
17. */
18. int[] fillArray()
19. {
20. int[] myarray=new int[3];
21. int candidate= (int) ((Math.random ()*100) % 3);
22. myarray[candidate]=1;
23. for (int i=0;i<3;i++){
24.   if(myarray[i]!=1) myarray[i]=0;
25. }
26. return myarray;
27. }
28.
29. /**
30. * Gets the position of the price ('1' value) for the current array
31. */
32. int getPricePosition(int[] array)
33. {
34. for (int i=0;i<3;i++){
35.   if (array[i]==1) return i;
36. }
37. return -1;
38. }
39.
40. /**
41. * Change user choice amongst the another unique valid choice
42. * @param guess user original guess
43. * @discarded forbidden choice as it was "revealed" by the program
44. * @myarray array with one price(1), one fail(0), and one discarded item (-1)
45. */
46. int changePos(int guess,int discarded,int[] myarray)
47. {
48. for (int i=0;i<3;i++){
49.   if (i!=guess && i!=discarded) return i;
50. }
51. return -1;
52. }
53.
54. /**
55. *  Trim door array with two valid positions,
56. *  one must be the price
57. *  and return the position of the discarded door
58. *  @param myarray original door array
59. *  @param myguess user guess of price
60. *  @one real position of price
61. */
62. int getChoicedArray(int[] myarray,int myGuess,int one){
63.   int[] small= new int[3];
64.   boolean flag=true;
65.  for (int k=0;k<3;k++){
66.    small[k]=0; //default fill
67.   if (k==one) { // compulsory item, must not be discared
68.      small[k]=myarray[k]; //will be 1 ,otherwise algorithm is wrong
69.      if (small[k]!=1) return -1;// error, should never happen
70.    }else if(flag && k!=myGuess){ //discarded option
71.      small[k]=-1;
72.      flag=false; // do not enter this condition again
73.    }
74.
75.   }
76.   // return position of discarded door
77.  for  (int i=0;i<3;i++){
78.    if (small[i]==-1)return i;
79.  }
80.  return -1; // error, should never happen
81. }
82.
83. /**
84. *  Main flow of the program:
85. *  get the original door array . Example: [0,1,0] two fails and one price
86. *  get user's choice: (possible values: 0,1,2). For Example: 1
87. *  Discard a fail : Example: [-1,1,0]
88. *  Change user's mind: Choice position 2 (NO SUCCESS)
89. *  If user does not change his mind: Position 1 (SUCCESS)
90. *  Register this success rate for NUMBER_OF_TRIES times
91. *
92. */
93. int successIfNotChange=0;
94. int successIfChange=0;
95. for (int i=0;i <NUMBER_OF_TRIES; i++){
96.  def myarray =fillArray(); //get original door array
97.  int one=getPricePosition(myarray); //get position of the price (1)
98.  int myGuess= (int) ((Math.random ()*100) % 3); //user choices a position
99.   //trim door array with two remaining positions, and get the position of the discarded door
101.  if(myarray[myGuess]==1){ //keep original  user choice and see if user was right
102.     successIfNotChange++;
103.   }
104.   //ExChange  user choice  with the alternative remaining door and see if success