HP Forums
The Monty Hall Problem - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: The Monty Hall Problem (/thread-9350.html)



The Monty Hall Problem - ArturDonda - 10-22-2017 11:43 PM

This program simulates the famous Monty Hall program. You get to choose how many times the problem should run, the number of doors and if you want to change doors or not.

Please note that it only shows the results (statistics) and you won't accually choose doors, win a car or goats.

PrintScreen:
[Image: q64HEfjBwi6y6APvvtdQ_rDAhlnPtuIcNymVaAYd...ize_mode=3]


Code:
EXPORT Monty_Hall()
BEGIN

LOCAL num1:=0,num2:=0,times:=1000,change,car,aux;
LOCAL chosen,doors:=3,victory:=0,defeat:=0,opendoors:={};

   REPEAT INPUT({doors,times,{change,0}},"Configurations",{"Doors = ","Times = ","Change?"},{"How many doors? (Must be >= 3)","How many times would you like to play?","Would you like to change doors?"});
   UNTIL doors >= 3;

   FOR num1 FROM 1 TO times DO
      car:=RANDINT(1,doors);
      chosen:=RANDINT(1,doors);

      IF change THEN
         FOR num2 FROM 1 to (doors-2) DO
            REPEAT aux:=RANDINT(1,doors)
            UNTIL aux <> chosen AND aux <> car AND NOT(contains(opendoors,aux));
            opendoors(num2):=aux;

            REPEAT aux:=RANDINT(1,doors)
            UNTIL aux <> chosen AND NOT(contains(opendoors,aux));
            chosen:=aux;
         END;
      END;
      opendoors:={};

      IF chosen==car THEN
         victory:=victory+1;
      ELSE
         defeat:=defeat+1;
      END;

   RECT();
   TEXTOUT_P("Monty Hall Problem",60,15,7);
   TEXTOUT_P("Number of doors: " + doors,15,80,2);
   TEXTOUT_P("Times Played:  " + num1 + "  out of " + times,15,95,2);
   TEXTOUT_P("Victories:",15,130,2);
   TEXTOUT_P(victory,85,130,2);
   TEXTOUT_P("(" + victory*100/num1 +"%)",150,130,2);
   TEXTOUT_P("Defeats:",15,145,2);
   TEXTOUT_P(defeat,85,145,2);
   TEXTOUT_P("(" + defeat*100/num1 +"%)",150,145,2);

   END;

WAIT(-1);
END;