Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Monty Hall Simulator
- * Shows the probabilty of getting the price if you do not change your choice or if you do
- * The Program starts with a random "door" array, with two fails (0's) and one price (1)
- * The "user" choices random position, then the program deletes one fail (turning it into -1)
- * and the user changes his choice (successIfChange) and test whether he gets the price or not
- * same test is done simultaneosly choicing not to change his choice ()
- * then the success rate for both cases are shown for a big amount of tries
- * For customizing the numbr of tries, edit the NUMBER_OF_TRIES constant
- * @author E.A [[email protected]]
- */
- int NUMBER_OF_TRIES=1000000;
- /**
- * Returns a random array of 3 positions with two fails(0's) and one price (1)
- */
- int[] fillArray()
- {
- int[] myarray=new int[3];
- int candidate= (int) ((Math.random ()*100) % 3);
- myarray[candidate]=1;
- for (int i=0;i<3;i++){
- if(myarray[i]!=1) myarray[i]=0;
- }
- return myarray;
- }
- /**
- * Gets the position of the price ('1' value) for the current array
- */
- int getPricePosition(int[] array)
- {
- for (int i=0;i<3;i++){
- if (array[i]==1) return i;
- }
- return -1;
- }
- /**
- * Change user choice amongst the another unique valid choice
- * @param guess user original guess
- * @discarded forbidden choice as it was "revealed" by the program
- * @myarray array with one price(1), one fail(0), and one discarded item (-1)
- */
- int changePos(int guess,int discarded,int[] myarray)
- {
- for (int i=0;i<3;i++){
- if (i!=guess && i!=discarded) return i;
- }
- return -1;
- }
- /**
- * Trim door array with two valid positions,
- * one must be the price
- * and return the position of the discarded door
- * @param myarray original door array
- * @param myguess user guess of price
- * @one real position of price
- */
- int getChoicedArray(int[] myarray,int myGuess,int one){
- int[] small= new int[3];
- boolean flag=true;
- for (int k=0;k<3;k++){
- small[k]=0; //default fill
- if (k==one) { // compulsory item, must not be discared
- small[k]=myarray[k]; //will be 1 ,otherwise algorithm is wrong
- if (small[k]!=1) return -1;// error, should never happen
- }else if(flag && k!=myGuess){ //discarded option
- small[k]=-1;
- flag=false; // do not enter this condition again
- }
- }
- // return position of discarded door
- for (int i=0;i<3;i++){
- if (small[i]==-1)return i;
- }
- return -1; // error, should never happen
- }
- /**
- * Main flow of the program:
- * get the original door array . Example: [0,1,0] two fails and one price
- * get user's choice: (possible values: 0,1,2). For Example: 1
- * Discard a fail : Example: [-1,1,0]
- * Change user's mind: Choice position 2 (NO SUCCESS)
- * If user does not change his mind: Position 1 (SUCCESS)
- * Register this success rate for NUMBER_OF_TRIES times
- *
- */
- int successIfNotChange=0;
- int successIfChange=0;
- for (int i=0;i <NUMBER_OF_TRIES; i++){
- def myarray =fillArray(); //get original door array
- int one=getPricePosition(myarray); //get position of the price (1)
- int myGuess= (int) ((Math.random ()*100) % 3); //user choices a position
- //trim door array with two remaining positions, and get the position of the discarded door
- int discarded=getChoicedArray(myarray,myGuess,one);
- if(myarray[myGuess]==1){ //keep original user choice and see if user was right
- successIfNotChange++;
- }
- //ExChange user choice with the alternative remaining door and see if success
- if (myarray[changePos(myGuess,discarded,myarray)]==1){
- successIfChange++; //success counter
- }
- }
- //Print results, if the algorithm is right shoud be around 33% and 66%
- double total= (successIfNotChange/NUMBER_OF_TRIES) *100;
- println "Success rate if user do not change his mind "+total+" %";
- double total1= (successIfChange/NUMBER_OF_TRIES) *100;
- println "Success rate if user change his mind "+total1+" %";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement