Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -5,7 +5,7 @@ F play_random(n)
L 0 .< n
random:shuffle(&in_drawer)
V found = 0B
L(prisoner) 100
L(prisoner) 0.<100
found = 0B
L(reveal) random:sample(sampler, 50)
V card = in_drawer[reveal]
@ -24,7 +24,7 @@ F play_optimal(n)
L 0 .< n
random:shuffle(&in_drawer)
V found = 0B
L(prisoner) 100
L(prisoner) 0.<100
V reveal = prisoner
found = 0B
L 50

View file

@ -1,23 +0,0 @@
package Prisoners is
type Win_Percentage is digits 2 range 0.0 .. 100.0;
type Drawers is array (1 .. 100) of Positive;
function Play_Game
(Repetitions : in Positive;
Strategy : not null access function
(Cupboard : in Drawers; Max_Prisoners : Integer;
Max_Attempts : Integer; Prisoner_Number : Integer) return Boolean)
return Win_Percentage;
-- Play the game with a specified number of repetitions, the chosen strategy
-- is passed to this function
function Optimal_Strategy
(Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
Prisoner_Number : Integer) return Boolean;
function Random_Strategy
(Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
Prisoner_Number : Integer) return Boolean;
end Prisoners;

View file

@ -1,128 +0,0 @@
pragma Ada_2012;
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;
package body Prisoners is
subtype Drawer_Range is Positive range 1 .. 100;
package Random_Drawer is new Ada.Numerics.Discrete_Random (Drawer_Range);
use Random_Drawer;
-- Helper procedures to initialise and shuffle the drawers
procedure Swap (A, B : Positive; Cupboard : in out Drawers) is
Temp : Positive;
begin
Temp := Cupboard (B);
Cupboard (B) := Cupboard (A);
Cupboard (A) := Temp;
end Swap;
procedure Shuffle (Cupboard : in out Drawers) is
G : Generator;
begin
Reset (G);
for I in Cupboard'Range loop
Swap (I, Random (G), Cupboard);
end loop;
end Shuffle;
procedure Initialise_Drawers (Cupboard : in out Drawers) is
begin
for I in Cupboard'Range loop
Cupboard (I) := I;
end loop;
Shuffle (Cupboard);
end Initialise_Drawers;
-- The two strategies for playing the game
function Optimal_Strategy
(Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
Prisoner_Number : Integer) return Boolean
is
Current_Card : Positive;
begin
Current_Card := Cupboard (Prisoner_Number);
if Current_Card = Prisoner_Number then
return True;
else
for I in Integer range 1 .. Max_Attempts loop
Current_Card := Cupboard (Current_Card);
if Current_Card = Prisoner_Number then
return True;
end if;
end loop;
end if;
return False;
end Optimal_Strategy;
function Random_Strategy
(Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
Prisoner_Number : Integer) return Boolean
is
Current_Card : Positive;
G : Generator;
begin
Reset (G);
Current_Card := Cupboard (Prisoner_Number);
if Current_Card = Prisoner_Number then
return True;
else
for I in Integer range 1 .. Max_Attempts loop
Current_Card := Cupboard (Random (G));
if Current_Card = Prisoner_Number then
return True;
end if;
end loop;
end if;
return False;
end Random_Strategy;
function Prisoners_Attempts
(Cupboard : in Drawers; Max_Prisoners : Integer; Max_Attempts : Integer;
Strategy : not null access function
(Cupboard : in Drawers; Max_Prisoners : Integer;
Max_Attempts : Integer; Prisoner_Number : Integer) return Boolean)
return Boolean
is
begin
for Prisoner_Number in Integer range 1 .. Max_Prisoners loop
if not Strategy
(Cupboard, Max_Prisoners, Max_Attempts, Prisoner_Number)
then
return False;
end if;
end loop;
return True;
end Prisoners_Attempts;
-- The function to play the game itself
function Play_Game
(Repetitions : in Positive;
Strategy : not null access function
(Cupboard : in Drawers; Max_Prisoners : Integer;
Max_Attempts : Integer; Prisoner_Number : Integer) return Boolean)
return Win_Percentage
is
Cupboard : Drawers;
Win, Game_Count : Natural := 0;
Number_Of_Prisoners : constant Integer := 100;
Max_Attempts : constant Integer := 50;
begin
loop
Initialise_Drawers (Cupboard);
if Prisoners_Attempts
(Cupboard => Cupboard, Max_Prisoners => Number_Of_Prisoners,
Max_Attempts => Max_Attempts, Strategy => Strategy)
then
Win := Win + 1;
end if;
Game_Count := Game_Count + 1;
exit when Game_Count = Repetitions;
end loop;
return Win_Percentage ((Float (Win) / Float (Repetitions)) * 100.0);
end Play_Game;
end Prisoners;

View file

@ -1,17 +0,0 @@
with Prisoners; use Prisoners;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
Wins : Win_Percentage;
package Win_Percentage_IO is new Float_IO (Win_Percentage);
begin
Wins := Play_Game (100_000, Optimal_Strategy'Access);
Put ("Optimal Strategy = ");
Win_Percentage_IO.Put (Wins, 2, 2, 0);
Put ("%");
New_Line;
Wins := Play_Game (100_000, Random_Strategy'Access);
Put ("Random Strategy = ");
Win_Percentage_IO.Put (Wins, 2, 2, 0);
Put ("%");
end Main;

View file

@ -37,7 +37,7 @@ bool playRandom() {
double exec(const size_t n, bool function() play) {
size_t success = 0;
for (int i = n; i > 0; i--) {
for (size_t i = n; i > 0; i--) {
if (play()) {
success++;
}

View file

@ -1,121 +0,0 @@
### Clear Screen from old Output
Clear-Host
Function RandomOpening ()
{
$Prisoners = 1..100 | Sort-Object {Get-Random}
$Cupboard = 1..100 | Sort-Object {Get-Random}
## Loop for the Prisoners
$Survived = $true
for ($I=1;$I -le 100;$i++)
{
$OpeningListe = 1..100 | Sort-Object {Get-Random}
$Gefunden = $false
## Loop for the trys of every prisoner
for ($X=1;$X -le 50;$X++)
{
$OpenNumber = $OpeningListe[$X]
IF ($Cupboard[$OpenNumber] -eq $Prisoners[$I])
{
$Gefunden = $true
}
## Cancel loop if prisoner found his number (yeah i know, dirty way ^^ )
IF ($Gefunden)
{
$X = 55
}
}
IF ($Gefunden -eq $false)
{
$I = 120
$Survived = $false
}
}
Return $Survived
}
Function StrategyOpening ()
{
$Prisoners = 1..100 | Sort-Object {Get-Random}
$Cupboard = 1..100 | Sort-Object {Get-Random}
$Survived = $true
for ($I=1;$I -le 100;$i++)
{
$Gefunden = $false
$OpeningNumber = $Prisoners[$I-1]
for ($X=1;$X -le 50;$X++)
{
IF ($Cupboard[$OpeningNumber-1] -eq $Prisoners[$I-1])
{
$Gefunden = $true
}
else
{
$OpeningNumber = $Cupboard[$OpeningNumber-1]
}
IF ($Gefunden)
{
$X = 55
}
}
IF ($Gefunden -eq $false)
{
$I = 120
$Survived = $false
}
}
Return $Survived
}
$MaxRounds = 10000
Function TestRandom
{
$WinnerRandom = 0
for ($Round = 1; $Round -le $MaxRounds;$Round++)
{
IF (($Round%1000) -eq 0)
{
$Time = Get-Date
Write-Host "Currently we are at rount $Round at $Time"
}
$Rueckgabewert = RandomOpening
IF ($Rueckgabewert)
{
$WinnerRandom++
}
}
$Prozent = (100/$MaxRounds)*$WinnerRandom
Write-Host "There are $WinnerRandom survivors whit random opening. This is $Prozent percent"
}
Function TestStrategy
{
$WinnersStrategy = 0
for ($Round = 1; $Round -le $MaxRounds;$Round++)
{
IF (($Round%1000) -eq 0)
{
$Time = Get-Date
Write-Host "Currently we are at $Round at $Time"
}
$Rueckgabewert = StrategyOpening
IF ($Rueckgabewert)
{
$WinnersStrategy++
}
}
$Prozent = (100/$MaxRounds)*$WinnersStrategy
Write-Host "There are $WinnersStrategy survivors whit strategic opening. This is $Prozent percent"
}
Function Main ()
{
Clear-Host
TestRandom
TestStrategy
}
Main

View file

@ -1,62 +0,0 @@
option explicit
const npris=100
const ntries=50
const ntests=1000.
dim drawer(100),opened(100),i
for i=1 to npris: drawer(i)=i:next
shuffle drawer
wscript.echo rf(tests(false)/ntests*100,10," ") &" % success for random"
wscript.echo rf(tests(true) /ntests*100,10," ") &" % success for optimal strategy"
function rf(v,n,s) rf=right(string(n,s)& v,n):end function
sub shuffle(d) 'knut's shuffle
dim i,j,t
randomize timer
for i=1 to npris
j=int(rnd()*i+1)
t=d(i):d(i)=d(j):d(j)=t
next
end sub
function tests(strat)
dim cntp,i,j
tests=0
for i=1 to ntests
shuffle drawer
cntp=0
if strat then
for j=1 to npris
if not trystrat(j) then exit for
next
else
for j=1 to npris
if not tryrand(j) then exit for
next
end if
if j>=npris then tests=tests+1
next
end function
function tryrand(pris)
dim i,r
erase opened
for i=1 to ntries
do
r=int(rnd*npris+1)
loop until opened(r)=false
opened(r)=true
if drawer(r)= pris then tryrand=true : exit function
next
tryrand=false
end function
function trystrat(pris)
dim i,r
r=pris
for i=1 to ntries
if drawer(r)= pris then trystrat=true :exit function
r=drawer(r)
next
trystrat=false
end function