Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,76 @@
-- Monty Hall Game
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;
procedure Monty_Stats is
Num_Iterations : Positive := 100000;
type Action_Type is (Stay, Switch);
type Prize_Type is (Goat, Pig, Car);
type Door_Index is range 1..3;
package Random_Prize is new Ada.Numerics.Discrete_Random(Door_Index);
use Random_Prize;
Seed : Generator;
Doors : array(Door_Index) of Prize_Type;
procedure Set_Prizes is
Prize_Index : Door_Index;
Booby_Prize : Prize_Type := Goat;
begin
Reset(Seed);
Prize_Index := Random(Seed);
Doors(Prize_Index) := Car;
for I in Doors'range loop
if I /= Prize_Index then
Doors(I) := Booby_Prize;
Booby_Prize := Prize_Type'Succ(Booby_Prize);
end if;
end loop;
end Set_Prizes;
function Play(Action : Action_Type) return Prize_Type is
Chosen : Door_Index := Random(Seed);
Monty : Door_Index;
begin
Set_Prizes;
for I in Doors'range loop
if I /= Chosen and Doors(I) /= Car then
Monty := I;
end if;
end loop;
if Action = Switch then
for I in Doors'range loop
if I /= Monty and I /= Chosen then
Chosen := I;
exit;
end if;
end loop;
end if;
return Doors(Chosen);
end Play;
Winners : Natural;
Pct : Float;
begin
Winners := 0;
for I in 1..Num_Iterations loop
if Play(Stay) = Car then
Winners := Winners + 1;
end if;
end loop;
Put("Stay : count" & Natural'Image(Winners) & " = ");
Pct := Float(Winners * 100) / Float(Num_Iterations);
Put(Item => Pct, Aft => 2, Exp => 0);
Put_Line("%");
Winners := 0;
for I in 1..Num_Iterations loop
if Play(Switch) = Car then
Winners := Winners + 1;
end if;
end loop;
Put("Switch : count" & Natural'Image(Winners) & " = ");
Pct := Float(Winners * 100) / Float(Num_Iterations);
Put(Item => Pct, Aft => 2, Exp => 0);
Put_Line("%");
end Monty_Stats;

View file

@ -0,0 +1,36 @@
# Monty Hall problem
# Play one game.
# sw is true if and only if Player switches to remaining door.
proc is_game_won(sw :: boolean) :: boolean is
car := entier(3 * math.random()); # Randomly place car behind a door.
player0 := entier(3 * math.random()); # Player randomly chooses a door.
do
monty := entier(3 * math.random()); # Monty opens door revealing a goat.
as monty = car or monty = player0;
if sw then
do
player := entier(3 * math.random());
as player = player0 or player = monty
else
player := player0
fi;
return (player = car);
end;
scope
local constant ngames := 10000; # number of games simulated
math.randomseed();
nwins := 0;
for game to ngames do
if is_game_won(false) then inc nwins fi
od;
printf("NOT switching doors wins car in ");
printf("%4.1f%% of games.\n", nwins / ngames * 100.0);
nwins := 0;
for game to ngames do
if is_game_won(true) then inc nwins fi
od;
printf("But switching doors wins car in ");
printf("%4.1f%% of games.\n", nwins / ngames * 100.0);
epocs

View file

@ -0,0 +1,89 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. monty-hall.
DATA DIVISION.
WORKING-STORAGE SECTION.
78 Num-Games VALUE 1000000.
*> These are needed so the values are passed to
*> get-rand-int correctly.
01 One PIC 9 VALUE 1.
01 Three PIC 9 VALUE 3.
01 doors-area.
03 doors PIC 9 OCCURS 3 TIMES.
01 choice PIC 9.
01 shown PIC 9.
01 winner PIC 9.
01 switch-wins PIC 9(7).
01 stay-wins PIC 9(7).
01 stay-wins-percent PIC Z9.99.
01 switch-wins-percent PIC Z9.99.
PROCEDURE DIVISION.
PERFORM Num-Games TIMES
MOVE 0 TO doors (winner)
CALL "get-rand-int" USING CONTENT One, Three,
REFERENCE winner
MOVE 1 TO doors (winner)
CALL "get-rand-int" USING CONTENT One, Three,
REFERENCE choice
PERFORM WITH TEST AFTER
UNTIL NOT(shown = winner OR choice)
CALL "get-rand-int" USING CONTENT One, Three,
REFERENCE shown
END-PERFORM
ADD doors (choice) TO stay-wins
ADD doors (6 - choice - shown) TO switch-wins
END-PERFORM
COMPUTE stay-wins-percent ROUNDED =
stay-wins / Num-Games * 100
COMPUTE switch-wins-percent ROUNDED =
switch-wins / Num-Games * 100
DISPLAY "Staying wins " stay-wins " times ("
stay-wins-percent "%)."
DISPLAY "Switching wins " switch-wins " times ("
switch-wins-percent "%)."
.
IDENTIFICATION DIVISION.
PROGRAM-ID. get-rand-int.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 call-flag PIC X VALUE "Y".
88 first-call VALUE "Y", FALSE "N".
01 num-range PIC 9.
LINKAGE SECTION.
01 min-num PIC 9.
01 max-num PIC 9.
01 ret PIC 9.
PROCEDURE DIVISION USING min-num, max-num, ret.
*> Seed RANDOM once.
IF first-call
MOVE FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:8))
TO num-range
SET first-call TO FALSE
END-IF
COMPUTE num-range = max-num - min-num + 1
COMPUTE ret =
FUNCTION MOD(FUNCTION RANDOM * 100000, num-range)
+ min-num
.
END PROGRAM get-rand-int.
END PROGRAM monty-hall.

View file

@ -0,0 +1,51 @@
use Random;
param doors: int = 3;
config const games: int = 1000;
config const maxTasks = 32;
var numTasks = 1;
while( games / numTasks > 1000000 && numTasks < maxTasks ) do numTasks += 1;
const tasks = 1..#numTasks;
const games_per_task = games / numTasks ;
const remaining_games = games % numTasks ;
var wins_by_stay: [tasks] int;
coforall task in tasks {
var rand = new RandomStream();
for game in 1..#games_per_task {
var player_door = (rand.getNext() * 1000): int % doors ;
var winning_door = (rand.getNext() * 1000): int % doors ;
if player_door == winning_door then
wins_by_stay[ task ] += 1;
}
if task == tasks.last then {
for game in 1..#remaining_games {
var player_door = (rand.getNext() * 1000): int % doors ;
var winning_door = (rand.getNext() * 1000): int % doors ;
if player_door == winning_door then
wins_by_stay[ task ] += 1;
}
}
}
var total_by_stay = + reduce wins_by_stay;
var total_by_switch = games - total_by_stay;
var percent_by_stay = ((total_by_stay: real) / games) * 100;
var percent_by_switch = ((total_by_switch: real) / games) * 100;
writeln( "Wins by staying: ", total_by_stay, " or ", percent_by_stay, "%" );
writeln( "Wins by switching: ", total_by_switch, " or ", percent_by_switch, "%" );
if ( total_by_stay > total_by_switch ){
writeln( "Staying is the superior method." );
} else if( total_by_stay < total_by_switch ){
writeln( "Switching is the superior method." );
} else {
writeln( "Both methods are equal." );
}

View file

@ -0,0 +1,21 @@
use Random;
config const numGames = 100_000_000;
var switch, stick: uint;
// have a separate RNG for each task; add together the results at the end
forall i in 1..numGames
with (var rand = new RandomStream(uint, parSafe = false), + reduce stick)
{
var chosen_door = rand.getNext() % 3;
var winner_door = rand.getNext() % 3;
if chosen_door == winner_door then
stick += 1;
}
// if you lost by sticking it means you would have won by switching
switch = numGames - stick;
writeln("Over ", numGames, " games:\n - switching wins ",
100.0*switch / numGames, "% of the time and\n - sticking wins ",
100.0*stick / numGames, "% of the time");

View file

@ -4,39 +4,39 @@ type Door
model
int id
Prize prize
new by int =id, Prize =prize do end
fun asText = text by block do return "(id:" + me.id + ", prize:" + me.prize.value + ")" end
new by int ←id, Prize ←prize do end
fun asText text by block do return "(id:" + me.id + ", prize:" + me.prize.value + ")" end
end
type Player
model
Door choice
fun choose = void by List doors
me.choice = doors[random(3)]
fun choose void by List doors
me.choice doors[random(3)]
end
end
type Monty
model
fun setPrize = void by List doors, Prize prize
doors[random(3)].prize = prize
fun setPrize void by List doors, Prize prize
doors[random(3)].prize prize
end
end
type MontyHallProblem
int ITERATIONS = 1000000
Map counter = text%int[ "keep" => 0, "switch" => 0 ]
int ITERATIONS 1000000
Map counter ← text%int[ "keep" ⇒ 0, "switch" ⇒ 0 ]
writeLine("Simulating " + ITERATIONS + " games:")
for int i = 0; i < ITERATIONS; i++
if i % 100000 == 0 do write(".") end
for int i 0; i < ITERATIONS; i++
if i % 100000 æ 0 do write(".") end
^|three numbered doors with no cars for now|^
List doors = Door[Door(1, Prize.GOAT), Door(2, Prize.GOAT), Door(3, Prize.GOAT)]
Monty monty = Monty() # set up Monty
List doors Door[Door(1, Prize.GOAT), Door(2, Prize.GOAT), Door(3, Prize.GOAT)]
Monty monty Monty() # set up Monty
monty.setPrize(doors, Prize.CAR) # Monty randomly sets the car behind one door
Player player = Player() # set up the player
Player player Player() # set up the player
player.choose(doors) # the player makes a choice
^|here Monty opens a door with a goat;
|behind the ones that are still closed there is a car and a goat,
|so that the player *always* wins by keeping or switching.
|^
counter[when(player.choice.prize == Prize.CAR, "keep", "switch")]++
counter[when(player.choice.prize æ Prize.CAR, "keep", "switch")]++
end
writeLine()
writeLine(counter)

View file

@ -1,21 +1,17 @@
max = 1000000
for i = 1 to max
car_door = random 3
chosen_door = random 3
car_door = random 1 3
chosen_door = random 1 3
if car_door <> chosen_door
montys_door = 6 - car_door - chosen_door
else
repeat
montys_door = random 3
montys_door = random 1 3
until montys_door <> car_door
.
.
if car_door = chosen_door
stay += 1
.
if car_door = 6 - montys_door - chosen_door
switch += 1
.
if car_door = chosen_door : stay += 1
if car_door = 6 - montys_door - chosen_door : switch += 1
.
print "If you stick to your choice, you have a " & stay / max * 100 & " percent chance to win"
print "If you switched, you have a " & switch / max * 100 & " percent chance to win"

View file

@ -0,0 +1,15 @@
(defun montyhall (keep)
(let ((prize (random 3))
(choice (random 3)))
(if keep (= prize choice)
(/= prize choice))))
(let ((cnt 0))
(dotimes (i 10000)
(and (montyhall t) (setq cnt (1+ cnt))))
(message "Strategy keep: %.3f%%" (/ cnt 100.0)))
(let ((cnt 0))
(dotimes (i 10000)
(and (montyhall nil) (setq cnt (1+ cnt))))
(message "Strategy switch: %.3f%%" (/ cnt 100.0)))

View file

@ -0,0 +1,20 @@
integer switchWins, stayWins
switchWins = 0
stayWins = 0
integer winner, choice, shown
for plays = 1 to 10000 do
winner = rand(3)
choice = rand(3)
while 1 do
shown = rand(3)
if shown != winner and shown != choice then
exit
end if
end while
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)

View file

@ -0,0 +1,22 @@
var switchWins = 0
var stayWins = 0
for of 1000000 {
var doors = [0 ,0, 0]
val winner = random(3)
doors[winner] = 1
val choice = random(3)
var shown = random(3)
while doors[shown] or shown == choice {
shown = random(3)
}
stayWins += doors[choice]
switchWins += doors[2 - choice - shown]
}
writeln "Staying wins {{stayWins}} times."
writeln "Switching wins {{switchWins}} times."

View file

@ -0,0 +1,48 @@
program MontyHall;
(* Monty Hall problem *)
const
n_games = 10000;
unit is_game_won: function (sw: boolean): boolean;
var
car, player, player0, Monty: integer;
begin
car := entier(random * 3); (* Randomly place car behind a door. *)
player0 := entier(random * 3); (* Player randomly chooses a door. *)
Monty := entier(random * 3); (* Monty opens door revealing a goat. *)
while (Monty = car) or (Monty = player0)
do
Monty := entier(random * 3)
od;
if sw
then (* Player switches to remaining door. *)
player := entier(random * 3);
while (player = player0) or (player = Monty)
do
player := entier(random * 3)
od
else
player := player0 (* Player sticks with original door. *)
fi;
result := (player = car)
end;
var
n_wins, game: integer;
begin
n_wins := 0;
for game := 1 to n_games
do
if is_game_won(false) then n_wins := n_wins + 1 fi
od;
write("NOT switching doors wins car in ");
writeln(n_wins / n_games * 100: 4: 1, "% of games.");
n_wins := 0;
for game := 1 to n_games
do
if is_game_won(true) then n_wins := n_wins + 1 fi
od;
write("But switching doors wins car in ");
writeln(n_wins / n_games * 100: 4: 1, "% of games.");
end

View file

@ -0,0 +1,23 @@
local fmt = require "fmt"
local function monty_hall(games)
local switch_wins = 0
local stay_wins = 0
for i = 1, games do
local doors = {0, 0, 0} -- all zero (goats) by default
doors[math.random(3)] = 1 -- put car in a random door
local choice = math.random(3) -- choose a door at random
local shown = 0
while true do
shown = math.random(3) -- the shown door
if doors[shown] != 1 and shown != choice then break end
end
stay_wins += doors[choice]
switch_wins += doors[6 - choice - shown]
end
fmt.print("Simulating %,s games:", games)
fmt.print("Staying wins %,s times", stay_wins)
fmt.print("Switching wins %,s times", switch_wins)
end
monty_hall(1_000_000)

View file

@ -0,0 +1,54 @@
#Declaring variables
$intIterations = 10000
$intKept = 0
$intSwitched = 0
#Creating a function
Function Play-MontyHall()
{
#Using a .NET object for randomization
$objRandom = New-Object -TypeName System.Random
#Generating the winning door number
$intWin = $objRandom.Next(1,4)
#Generating the chosen door
$intChoice = $objRandom.Next(1,4)
#Generating the excluded number
#Because there is no method to exclude a number from a range,
#I let it re-generate in case it equals the winning number or
#in case it equals the chosen door.
$intLose = $objRandom.Next(1,4)
While (($intLose -EQ $intWin) -OR ($intLose -EQ $intChoice))
{$intLose = $objRandom.Next(1,4)}
#Generating the 'other' door
#Same logic applies as for the chosen door: it cannot be equal
#to the winning door nor to the chosen door.
$intSwitch = $objRandom.Next(1,4)
While (($intSwitch -EQ $intLose) -OR ($intSwitch -EQ $intChoice))
{$intSwitch = $objRandom.Next(1,4)}
#Simple counters per win for both categories
#Because a child scope cannot change variables in the parent
#scope, the scope of the counters is expanded script-wide.
If ($intChoice -EQ $intWin)
{$script:intKept++}
If ($intSwitch -EQ $intWin)
{$script:intSwitched++}
}
#Looping the Monty Hall function for $intIterations times
While ($intIterationCount -LT $intIterations)
{
Play-MontyHall
$intIterationCount++
}
#Output
Write-Host "Results through $intIterations iterations:"
Write-Host "Keep : $intKept ($($intKept/$intIterations*100)%)"
Write-Host "Switch: $intSwitched ($($intSwitched/$intIterations*100)%)"
Write-Host ""

View file

@ -0,0 +1,39 @@
# Monty Hall Problem
$script:NGames = 10000
function Is-Game-Won($Sw) {
# Play one game.
# Switching if and only if $Sw -ne 0.
$car = Get-Random -maximum 3 # Randomly place car behind a door.
$player0 = Get-Random -maximum 3 # Player randomly chooses a door.
do {
$monty = Get-Random -maximum 3 # Monty opens door revealing a goat.
} while (($monty -eq $car) -or ($monty -eq $player0))
if ($Sw -ne 0) { # Player switches to remaining door.
do {
$player = Get-Random -maximum 3
} while (($player -eq $player0) -or ($player -eq $monty))
} else {
$player = $player0 # Player sticks with original door.
}
return [bool]($player -eq $car)
}
$nWins = 0
foreach ($game in 1..$script:NGames) {
if (Is-Game-Won(0)) {
$nWins++
}
}
$row = "NOT switching doors wins car in "
$row += "$(($nWins / $script:NGames * 100).ToString('##.#'))% of games."
Write-Output $row
$nWins = 0
foreach ($game in 1..$script:NGames) {
if (Is-Game-Won(1) -ne 0) {
$nWins++
}
}
$row = "But switching doors wins car in "
$row += "$(($nWins / $script:NGames * 100).ToString('##.#'))% of games."
Write-Output $row

View file

@ -0,0 +1,38 @@
' Monty Hall problem
Public Sub MontyHallMain()
Const NGames As Integer = 10000
Dim NWins, Game As Integer
Randomize
NWins = 0
For Game = 1 To NGames
If IsGameWon(False) Then NWins = NWins + 1
Next Game
Debug.Print "NOT switching doors wins car in ";
Debug.Print FormatNumber(NWins / NGames * 100, 1);
Debug.Print "% of games."
NWins = 0
For Game = 1 To NGames
If IsGameWon(True) Then NWins = NWins + 1
Next Game
Debug.Print "But switching doors wins car in ";
Debug.Print FormatNumber(NWins / NGames * 100, 1);
Debug.Print "% of games."
End Sub
Private Function IsGameWon(Sw As Boolean) As Boolean
Dim Car, Player, Player0, Monty As Byte
Car = Int(Rnd * 3) ' Randomly place car behind a door.
Player0 = Int(Rnd * 3) ' Player randomly chooses a door.
Do
Monty = Int(Rnd * 3) ' Monty opens door revealing a goat.
Loop Until (Monty <> Car) And (Monty <> Player0)
If Sw <> 0 Then ' Player switches to remaining door.
Do
Player = Int(Rnd * 3)
Loop Until (Player <> Player0) And (Player <> Monty)
Else
Player = Player0 ' Player sticks with original door.
End If
IsGameWon = (Player = Car)
End Function