Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
41
Task/Monty-Hall-problem/ANSI-BASIC/monty-hall-problem.basic
Normal file
41
Task/Monty-Hall-problem/ANSI-BASIC/monty-hall-problem.basic
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
100 PROGRAM MontyHallProblem
|
||||
110 DEF NGames = 10000
|
||||
120 RANDOMIZE
|
||||
130 LET NWins = 0
|
||||
140 FOR Game = 0 TO NGames
|
||||
150 IF IsGameWon(0) <> 0 THEN LET NWins = NWins + 1
|
||||
160 NEXT Game
|
||||
170 PRINT "NOT switching doors wins car in ";
|
||||
180 PRINT USING "##.#": NWins / NGames * 100;
|
||||
190 PRINT "% of games."
|
||||
200 LET NWins = 0
|
||||
210 FOR Game = 0 TO NGames
|
||||
220 IF IsGameWon(1) <> 0 THEN LET NWins = NWins + 1
|
||||
230 NEXT Game
|
||||
240 PRINT "But switching doors wins car in ";
|
||||
250 PRINT USING "##.#": NWins / NGames * 100;
|
||||
260 PRINT "% of games."
|
||||
270 END
|
||||
280 REM ***
|
||||
290 EXTERNAL FUNCTION IsGameWon(Sw)
|
||||
300 REM Play one game.
|
||||
310 REM Switching if and only if Sw <> 0.
|
||||
320 REM Returns 1 if the game is won, 0 otherwise.
|
||||
330 LET Car = INT(RND * 3) ! Randomly place car behind a door.
|
||||
340 LET Player0 = INT(RND * 3) ! Player randomly chooses a door.
|
||||
350 DO
|
||||
360 LET Monty = INT(RND * 3) ! Monty opens door revealing a goat.
|
||||
370 LOOP UNTIL (Monty <> Car) AND (Monty <> Player0)
|
||||
380 IF Sw <> 0 THEN ! Player switches TO remaining door.
|
||||
390 DO
|
||||
400 LET Player = INT(RND * 3)
|
||||
410 LOOP UNTIL (Player <> Player0) AND (Player <> Monty)
|
||||
420 ELSE
|
||||
430 LET Player = Player0 ! Player sticks with original door.
|
||||
440 END IF
|
||||
450 IF Player = Car THEN
|
||||
460 LET IsGameWon = 1
|
||||
470 ELSE
|
||||
480 LET IsGameWon = 0
|
||||
490 END IF
|
||||
500 END FUNCTION
|
||||
|
|
@ -16,5 +16,5 @@ for plays = 1 to 10000 do
|
|||
stayWins += choice = winner
|
||||
switchWins += 6-choice-shown = winner
|
||||
end for
|
||||
printf(1, "Switching wins %d times\n", switchWins)
|
||||
printf(1, "Staying wins %d times\n", stayWins)
|
||||
printf(1, "Switching wins %d times.\n", switchWins)
|
||||
printf(1, "Staying wins %d times.\n", stayWins)
|
||||
|
|
|
|||
60
Task/Monty-Hall-problem/FutureBasic/monty-hall-problem.basic
Normal file
60
Task/Monty-Hall-problem/FutureBasic/monty-hall-problem.basic
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// Monty Hall problem
|
||||
// May 2024 - Rich Love
|
||||
|
||||
local fn IsGameWon(Sw as int) as bool
|
||||
REM Play one game.
|
||||
REM Switching if and only if Sw <> 0.
|
||||
REM Returns 1 if the game is won, 0 otherwise.
|
||||
|
||||
short Car = INT(RND(3)) //Randomly place car behind a door.
|
||||
short Player0 = INT(RND(3)) // Player randomly chooses a door.
|
||||
short Monty
|
||||
short IsGameWon
|
||||
short Player
|
||||
|
||||
DO
|
||||
Monty = INT(RND(3)) // Monty opens door revealing a goat.
|
||||
UNTIL (Monty <> Car) AND (Monty <> Player0)
|
||||
380
|
||||
|
||||
IF Sw <> 0 // Player switches TO remaining door.
|
||||
|
||||
DO
|
||||
Player = INT(RND(3))
|
||||
UNTIL (Player <> Player0) AND (Player <> Monty)
|
||||
Monty = INT(RND(3))
|
||||
|
||||
ELSE
|
||||
Player = Player0 // Player sticks with original door.
|
||||
END IF
|
||||
IF Player = Car
|
||||
IsGameWon = 1
|
||||
ELSE
|
||||
IsGameWon = 0
|
||||
END IF
|
||||
END fn = IsgameWon
|
||||
|
||||
|
||||
_NGames = 10000
|
||||
|
||||
float NWins = 0
|
||||
short Game
|
||||
|
||||
FOR Game = 0 TO _NGames
|
||||
IF fn IsGameWon(0) <> 0 THEN NWins = NWins + 1
|
||||
NEXT Game
|
||||
|
||||
|
||||
PRINT "NOT switching doors wins car in ";
|
||||
PRINT USING "##.##" ; NWins / 100 ;
|
||||
PRINT " % of games."
|
||||
|
||||
NWins = 0
|
||||
FOR Game = 0 TO _NGames
|
||||
IF fn IsGameWon(1) <> 0 THEN NWins = NWins + 1
|
||||
NEXT Game
|
||||
PRINT "But switching doors wins car in ";
|
||||
PRINT USING "##.##" ; NWins / _NGames * 100;
|
||||
PRINT " % of games."
|
||||
|
||||
handleevents
|
||||
3
Task/Monty-Hall-problem/K/monty-hall-problem.k
Normal file
3
Task/Monty-Hall-problem/K/monty-hall-problem.k
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
montyhall:{t:,/ 1_ x {`prng@`t[];ch:1?3;pr:1?3;sw:1?2;$[sw;a:ch=pr;a:~(ch=pr)];a}\0N;("KEEP %";(+/t)%x;"SWAP %";(+/~t)%x)}
|
||||
|
||||
montyhall 100000
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
'adapted from BASIC solution
|
||||
DIM doors(3) '0 is a goat, 1 is a car
|
||||
|
||||
total = 10000 'set desired number of iterations
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
10 REM Monty Hall problem
|
||||
20 LET N = 10000
|
||||
30 RANDOMIZE
|
||||
40 LET W = 0
|
||||
50 FOR G = 0 TO N
|
||||
60 LET S = 0
|
||||
70 GOSUB 230
|
||||
80 IF V = 0 THEN 100
|
||||
90 LET W = W+1
|
||||
100 NEXT G
|
||||
110 PRINT "NOT switching doors wins car in";
|
||||
120 PRINT W/N*100; "per cent of games."
|
||||
130 LET W = 0
|
||||
140 FOR G = 0 TO N
|
||||
150 LET S = 1
|
||||
160 GOSUB 230
|
||||
170 IF V = 0 THEN 190
|
||||
180 LET W = W+1
|
||||
190 NEXT G
|
||||
200 PRINT "But switching doors wins car in";
|
||||
210 PRINT W/N*100; "per cent of games."
|
||||
220 END
|
||||
230 REM ** Is game won?
|
||||
240 REM Play one game.
|
||||
250 REM Switching if and only if S <> 0.
|
||||
260 REM Randomly place car behind a door.
|
||||
270 LET C = INT(RND*3)
|
||||
280 REM Player randomly chooses a door.
|
||||
290 LET P0 = INT(RND*3)
|
||||
300 REM Monty opens door revealing a goat.
|
||||
310 LET M = INT(RND*3)
|
||||
320 IF M = C THEN 310
|
||||
330 IF M = P0 THEN 310
|
||||
340 IF S = 0 THEN 410
|
||||
350 REM Player switches to remaining door.
|
||||
360 LET P = INT(RND*3)
|
||||
370 IF P = P0 THEN 360
|
||||
380 IF P = M THEN 360
|
||||
390 GOTO 430
|
||||
400 REM Player sticks with original door.
|
||||
410 LET P = P0
|
||||
420 REM Victory?
|
||||
430 IF P <> C THEN 460
|
||||
440 LET V = 1
|
||||
450 RETURN
|
||||
460 LET V = 0
|
||||
470 RETURN
|
||||
58
Task/Monty-Hall-problem/Modula-2/monty-hall-problem.mod2
Normal file
58
Task/Monty-Hall-problem/Modula-2/monty-hall-problem.mod2
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
MODULE MontyHallProblem;
|
||||
|
||||
FROM STextIO IMPORT
|
||||
WriteLn, WriteString;
|
||||
FROM RandomNumbers IMPORT
|
||||
Randomize, Rnd;
|
||||
FROM SRealIO IMPORT
|
||||
WriteFixed;
|
||||
|
||||
CONST
|
||||
NGames = 10000; (* number of games simulated *)
|
||||
VAR
|
||||
NWins, Game: CARDINAL;
|
||||
|
||||
PROCEDURE IsGameWon(Sw: BOOLEAN): BOOLEAN;
|
||||
(* Play one game. *)
|
||||
VAR
|
||||
Car, Player, Player0, Monty: CARDINAL;
|
||||
BEGIN
|
||||
Car := Rnd(3); (* Randomly place car behind a door. *)
|
||||
Player0 := Rnd(3); (* Player randomly chooses a door. *)
|
||||
REPEAT
|
||||
Monty := Rnd(3); (* Monty opens door revealing a goat. *)
|
||||
UNTIL (Monty <> Car) AND (Monty <> Player0);
|
||||
IF Sw THEN
|
||||
(* Player switches to remaining door. *)
|
||||
REPEAT
|
||||
Player := Rnd(3);
|
||||
UNTIL (Player <> Player0) AND (Player <> Monty)
|
||||
ELSE
|
||||
Player := Player0 (* Player sticks with original door. *)
|
||||
END;
|
||||
RETURN (Player = Car);
|
||||
END IsGameWon;
|
||||
|
||||
BEGIN
|
||||
Randomize(0);
|
||||
NWins := 0;
|
||||
FOR Game := 0 TO NGames DO
|
||||
IF IsGameWon(FALSE) THEN
|
||||
NWins := NWins + 1
|
||||
END
|
||||
END;
|
||||
WriteString('NOT switching doors wins car in ');
|
||||
WriteFixed(FLOAT(NWins) / FLOAT(NGames) * 100.0, 1, 4);
|
||||
WriteString('% of games.');
|
||||
WriteLn;
|
||||
NWins := 0;
|
||||
FOR Game := 0 TO NGames DO
|
||||
IF IsGameWon(TRUE) THEN
|
||||
NWins := NWins + 1
|
||||
END
|
||||
END;
|
||||
WriteString('But switching doors wins car in ');
|
||||
WriteFixed(FLOAT(NWins) / FLOAT(NGames) * 100.0, 1, 4);
|
||||
WriteString('% of games.');
|
||||
WriteLn;
|
||||
END MontyHallProblem.
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
' adapted from BASIC solution
|
||||
|
||||
input "Number of tries;";tries ' gimme the number of iterations
|
||||
FOR plays = 1 TO tries
|
||||
winner = INT(RND(1) * 3) + 1
|
||||
|
|
|
|||
29
Task/Monty-Hall-problem/SETL/monty-hall-problem.setl
Normal file
29
Task/Monty-Hall-problem/SETL/monty-hall-problem.setl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
program monty_hall;
|
||||
setrandom(0);
|
||||
|
||||
n_simulations := 100000;
|
||||
print('Chance to win:');
|
||||
print('When switching doors:', win_chance(true, n_simulations) * 100, '%');
|
||||
print('When not switching doors:', win_chance(false, n_simulations) * 100, '%');
|
||||
|
||||
proc win_chance(switch, n_simulations);
|
||||
wins := 0;
|
||||
loop for i in [1..n_simulations] do
|
||||
wins +:= if simulate(switch) then 1 else 0 end;
|
||||
end loop;
|
||||
return wins / n_simulations;
|
||||
end proc;
|
||||
|
||||
proc simulate(switch);
|
||||
doors := {1, 2, 3};
|
||||
car := random doors;
|
||||
goats := doors less car;
|
||||
choice := random doors;
|
||||
opened := random (goats less choice);
|
||||
|
||||
if switch then
|
||||
choice := arb (doors less choice less opened);
|
||||
end if;
|
||||
return choice = car;
|
||||
end proc;
|
||||
end program;
|
||||
|
|
@ -1,19 +1,21 @@
|
|||
OPTION BASE 0
|
||||
DIM puertas(3)
|
||||
|
||||
LET numTiradas = 1000000
|
||||
|
||||
FOR i = 1 TO numTiradas
|
||||
FOR i = 0 TO numTiradas
|
||||
LET pta_coche = INT(RND * 3) + 1
|
||||
LET puertas(pta_coche) = 1
|
||||
LET pta_elegida = INT(RND * 3) + 1
|
||||
IF pta_coche <> pta_elegida THEN
|
||||
LET pta_montys = 6 - pta_coche - pta_elegida
|
||||
DO
|
||||
LET pta_montys = INT(RND * 3) + 1
|
||||
LOOP WHILE puertas(pta_montys) = 1 OR pta_montys = pta_elegida
|
||||
IF puertas(pta_elegida) = 1 THEN
|
||||
LET cambia = cambia + 1
|
||||
ELSE
|
||||
DO
|
||||
LET pta_montys = INT(RND * 3) + 1
|
||||
LOOP UNTIL pta_montys <> pta_coche
|
||||
LET permanece = permanece + 1
|
||||
END IF
|
||||
! mantener elección
|
||||
IF pta_coche = pta_elegida THEN LET permanece = permanece + 1
|
||||
! cambiar elección
|
||||
IF pta_coche = 6 - pta_montys - pta_elegida THEN LET cambia = cambia + 1
|
||||
LET puertas(pta_coche) = 0
|
||||
NEXT i
|
||||
|
||||
PRINT "Cambiar gana el"; permanece / numTiradas * 100; "% de las veces."
|
||||
|
|
|
|||
|
|
@ -17,17 +17,17 @@ return Player = Car;
|
|||
];
|
||||
|
||||
[Format(2,1);
|
||||
Text(0, "NOT switching doors wins car in ");
|
||||
NWins:= 0;
|
||||
for Game:= 0 to NGames-1 do
|
||||
if IsGameWon(false) then NWins:= NWins+1;
|
||||
Text(0, "NOT switching doors wins car in ");
|
||||
RlOut(0, float(NWins)/float(NGames)*100.0);
|
||||
Text(0, "% of games.^M^J");
|
||||
|
||||
Text(0, "But switching doors wins car in ");
|
||||
NWins:= 0;
|
||||
for Game:= 0 to NGames-1 do
|
||||
if IsGameWon(true) then NWins:= NWins+1;
|
||||
Text(0, "But switching doors wins car in ");
|
||||
RlOut(0, float(NWins)/float(NGames)*100.0);
|
||||
Text(0, "% of games.^M^J");
|
||||
]
|
||||
|
|
|
|||
29
Task/Monty-Hall-problem/Zig/monty-hall-problem.zig
Normal file
29
Task/Monty-Hall-problem/Zig/monty-hall-problem.zig
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
const std = @import("std");
|
||||
|
||||
const number_of_simulations: u32 = 10_000_000;
|
||||
|
||||
pub fn main() !void {
|
||||
var stick_wins: u32 = 0;
|
||||
var switch_wins: u32 = 0;
|
||||
var doors = [3]bool{ true, false, false };
|
||||
|
||||
var t = std.rand.DefaultPrng.init(42);
|
||||
const r = t.random();
|
||||
|
||||
var guess: u8 = undefined;
|
||||
var door_shown: u8 = undefined;
|
||||
|
||||
for (0..number_of_simulations) |_| {
|
||||
std.rand.Random.shuffle(r, bool, &doors);
|
||||
guess = r.intRangeAtMost(u8, 0, 2);
|
||||
door_shown = r.intRangeAtMost(u8, 0, 2);
|
||||
while (!doors[door_shown] and door_shown != guess) door_shown = r.intRangeAtMost(u8, 0, 2);
|
||||
if (doors[guess]) {
|
||||
stick_wins += 1;
|
||||
} else {
|
||||
switch_wins += 1;
|
||||
}
|
||||
}
|
||||
|
||||
std.debug.print("After {} simulations:\nStick wins: {}\nSwitch wins: {}\n", .{ number_of_simulations, stick_wins, switch_wins });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue