langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
29
Task/Monty-Hall-problem/OCaml/monty-hall-problem.ocaml
Normal file
29
Task/Monty-Hall-problem/OCaml/monty-hall-problem.ocaml
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
let trials = 10000
|
||||
|
||||
type door = Car | Goat
|
||||
|
||||
let play switch =
|
||||
let n = Random.int 3 in
|
||||
let d1 = [|Car; Goat; Goat|].(n) in
|
||||
if not switch then d1
|
||||
else match d1 with
|
||||
Car -> Goat
|
||||
| Goat -> Car
|
||||
|
||||
let cars n switch =
|
||||
let total = ref 0 in
|
||||
for i = 1 to n do
|
||||
let prize = play switch in
|
||||
if prize = Car then
|
||||
incr total
|
||||
done;
|
||||
!total
|
||||
|
||||
let () =
|
||||
let switch = cars trials true
|
||||
and stay = cars trials false in
|
||||
let msg strat n =
|
||||
Printf.printf "The %s strategy succeeds %f%% of the time.\n"
|
||||
strat (100. *. (float n /. float trials)) in
|
||||
msg "switch" switch;
|
||||
msg "stay" stay
|
||||
13
Task/Monty-Hall-problem/PARI-GP/monty-hall-problem.pari
Normal file
13
Task/Monty-Hall-problem/PARI-GP/monty-hall-problem.pari
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
test(trials)={
|
||||
my(stay=0,change=0);
|
||||
for(i=1,trials,
|
||||
my(prize=random(3),initial=random(3),opened);
|
||||
while((opened=random(3))==prize | opened==initial,);
|
||||
if(prize == initial, stay++, change++)
|
||||
);
|
||||
print("Wins when staying: "stay);
|
||||
print("Wins when changing: "change);
|
||||
[stay, change]
|
||||
};
|
||||
|
||||
test(1e4)
|
||||
38
Task/Monty-Hall-problem/Perl-6/monty-hall-problem.pl6
Normal file
38
Task/Monty-Hall-problem/Perl-6/monty-hall-problem.pl6
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
sub remove (@a is rw, Int $i) {
|
||||
my $temp = @a[$i];
|
||||
@a = @a[0 ..^ $i], @a[$i ^..^ @a];
|
||||
return $temp;
|
||||
}
|
||||
|
||||
enum Prize <Car Goat>;
|
||||
enum Strategy <Stay Switch>;
|
||||
|
||||
sub play (Strategy $strategy, Int :$doors = 3) returns Prize {
|
||||
# Call the door with a car behind it door 0. Number the
|
||||
# remaining doors starting from 1.
|
||||
my Prize @doors = Car, Goat xx $doors - 1;
|
||||
# The player chooses a door.
|
||||
my Prize $initial_pick = remove @doors, @doors.keys().pick;
|
||||
# Of the n doors remaining, the host chooses n - 1 that have
|
||||
# goats behind them and opens them, removing them from play.
|
||||
remove @doors, $_
|
||||
for pick @doors.elems - 1, grep { @doors[$_] == Goat }, keys @doors;
|
||||
# If the player stays, they get their initial pick. Otherwise,
|
||||
# they get whatever's behind the remaining door.
|
||||
return $strategy == Stay ?? $initial_pick !! @doors[0];
|
||||
}
|
||||
|
||||
constant TRIALS = 100;
|
||||
|
||||
for 3, 10 -> $doors {
|
||||
my %wins;
|
||||
say "With $doors doors: ";
|
||||
for Stay, 'Staying', Switch, 'Switching' -> $s, $name {
|
||||
for ^TRIALS {
|
||||
++%wins{$s} if play($s, doors => $doors) == Car;
|
||||
}
|
||||
say " $name wins ",
|
||||
round(100*%wins{$s} / TRIALS),
|
||||
'% of the time.'
|
||||
}
|
||||
}
|
||||
54
Task/Monty-Hall-problem/PowerShell/monty-hall-problem.psh
Normal file
54
Task/Monty-Hall-problem/PowerShell/monty-hall-problem.psh
Normal 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 ""
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
Structure wins
|
||||
stay.i
|
||||
redecide.i
|
||||
EndStructure
|
||||
|
||||
#goat = 0
|
||||
#car = 1
|
||||
Procedure MontyHall(*results.wins)
|
||||
Dim Doors(2)
|
||||
Doors(Random(2)) = #car
|
||||
|
||||
player = Random(2)
|
||||
Select Doors(player)
|
||||
Case #car
|
||||
*results\redecide + #goat
|
||||
*results\stay + #car
|
||||
Case #goat
|
||||
*results\redecide + #car
|
||||
*results\stay + #goat
|
||||
EndSelect
|
||||
EndProcedure
|
||||
|
||||
OpenConsole()
|
||||
#Tries = 1000000
|
||||
|
||||
Define results.wins
|
||||
|
||||
For i = 1 To #Tries
|
||||
MontyHall(@results)
|
||||
Next
|
||||
|
||||
PrintN("Trial runs for each option: " + Str(#Tries))
|
||||
PrintN("Wins when redeciding: " + Str(results\redecide) + " (" + StrD(results\redecide / #Tries * 100, 2) + "% chance)")
|
||||
PrintN("Wins when sticking: " + Str(results\stay) + " (" + StrD(results\stay / #Tries * 100, 2) + "% chance)")
|
||||
Input()
|
||||
22
Task/Monty-Hall-problem/Run-BASIC/monty-hall-problem.run
Normal file
22
Task/Monty-Hall-problem/Run-BASIC/monty-hall-problem.run
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
' 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
|
||||
doors(winner) = 1 'put a winner in a random door
|
||||
choice = INT(RND(1) * 3) + 1 'pick a door please
|
||||
[DO] shown = INT(RND(1) * 3) + 1
|
||||
' ------------------------------------------
|
||||
' don't show the winner or the choice
|
||||
if doors(shown) = 1 then goto [DO]
|
||||
if shown = choice then goto [DO]
|
||||
if doors(choice) = 1 then
|
||||
stayWins = stayWins + 1 ' if you won by staying, count it
|
||||
else
|
||||
switchWins = switchWins + 1 ' could have switched to win
|
||||
end if
|
||||
doors(winner) = 0 'clear the doors for the next test
|
||||
NEXT
|
||||
PRINT " Result for ";tries;" games."
|
||||
PRINT "Switching wins ";switchWins; " times."
|
||||
PRINT " Staying wins ";stayWins; " times."
|
||||
23
Task/Monty-Hall-problem/Seed7/monty-hall-problem.seed7
Normal file
23
Task/Monty-Hall-problem/Seed7/monty-hall-problem.seed7
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: switchWins is 0;
|
||||
var integer: stayWins is 0;
|
||||
var integer: winner is 0;
|
||||
var integer: choice is 0;
|
||||
var integer: shown is 0;
|
||||
var integer: plays is 0;
|
||||
begin
|
||||
for plays range 1 to 10000 do
|
||||
winner := rand(1, 3);
|
||||
choice := rand(1, 3);
|
||||
repeat
|
||||
shown := rand(1, 3)
|
||||
until shown <> winner and shown <> choice;
|
||||
stayWins +:= ord(choice = winner);
|
||||
switchWins +:= ord(6 - choice - shown = winner);
|
||||
end for;
|
||||
writeln("Switching wins " <& switchWins <& " times");
|
||||
writeln("Staying wins " <& stayWins <& " times");
|
||||
end func;
|
||||
72
Task/Monty-Hall-problem/UNIX-Shell/monty-hall-problem.sh
Normal file
72
Task/Monty-Hall-problem/UNIX-Shell/monty-hall-problem.sh
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#!/bin/bash
|
||||
# Simulates the "monty hall" probability paradox and shows results.
|
||||
# http://en.wikipedia.org/wiki/Monty_Hall_problem
|
||||
# (should rewrite this in C for faster calculating of huge number of rounds)
|
||||
# (Hacked up by Éric Tremblay, 07.dec.2010)
|
||||
|
||||
num_rounds=10 #default number of rounds
|
||||
num_doors=3 # default number of doors
|
||||
[ "$1" = "" ] || num_rounds=$[$1+0]
|
||||
[ "$2" = "" ] || num_doors=$[$2+0]
|
||||
|
||||
nbase=1 # or 0 if we want to see door numbers zero-based
|
||||
num_win=0; num_lose=0
|
||||
|
||||
echo "Playing $num_rounds times, with $num_doors doors."
|
||||
[ "$num_doors" -lt 3 ] && {
|
||||
echo "Hey, there has to be at least 3 doors!!"
|
||||
exit 1
|
||||
}
|
||||
echo
|
||||
|
||||
function one_round() {
|
||||
winning_door=$[$RANDOM % $num_doors ]
|
||||
player_picks_door=$[$RANDOM % $num_doors ]
|
||||
|
||||
# Host leaves this door AND the player's first choice closed, opens all others
|
||||
# (this WILL loop forever if there is only 1 door)
|
||||
host_skips_door=$winning_door
|
||||
while [ "$host_skips_door" = "$player_picks_door" ]; do
|
||||
#echo -n "(Host looks at door $host_skips_door...) "
|
||||
host_skips_door=$[$RANDOM % $num_doors]
|
||||
done
|
||||
|
||||
# Output the result of this round
|
||||
#echo "Round $[$nbase+current_round]: "
|
||||
echo -n "Player chooses #$[$nbase+$player_picks_door]. "
|
||||
[ "$num_doors" -ge 10 ] &&
|
||||
# listing too many door numbers (10 or more) will just clutter the output
|
||||
echo -n "Host opens all except #$[$nbase+$host_skips_door] and #$[$nbase+$player_picks_door]. " \
|
||||
|| {
|
||||
# less than 10 doors, we list them one by one instead of "all except ?? and ??"
|
||||
echo -n "Host opens"
|
||||
host_opens=0
|
||||
while [ "$host_opens" -lt "$num_doors" ]; do
|
||||
[ "$host_opens" != "$host_skips_door" ] && [ "$host_opens" != "$player_picks_door" ] && \
|
||||
echo -n " #$[$nbase+$host_opens]"
|
||||
host_opens=$[$host_opens+1]
|
||||
done
|
||||
echo -n " "
|
||||
}
|
||||
echo -n "(prize is behind #$[$nbase+$winning_door]) "
|
||||
echo -n "Switch from $[$nbase+$player_picks_door] to $[$nbase+$host_skips_door]: "
|
||||
[ "$winning_door" = "$host_skips_door" ] && {
|
||||
echo "WIN."
|
||||
num_win=$[num_win+1]
|
||||
} || {
|
||||
echo "LOSE."
|
||||
num_lose=$[num_lose+1]
|
||||
}
|
||||
} # end of function one_round
|
||||
|
||||
# ok, let's go
|
||||
current_round=0
|
||||
while [ "$num_rounds" -gt "$current_round" ]; do
|
||||
one_round
|
||||
current_round=$[$current_round+1]
|
||||
done
|
||||
|
||||
echo
|
||||
echo "Wins (switch to remaining door): $num_win"
|
||||
echo "Losses (first guess was correct): $num_lose"
|
||||
exit 0
|
||||
17
Task/Monty-Hall-problem/Ursala/monty-hall-problem.ursala
Normal file
17
Task/Monty-Hall-problem/Ursala/monty-hall-problem.ursala
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#import std
|
||||
#import nat
|
||||
#import flo
|
||||
|
||||
rounds = 10000
|
||||
|
||||
car_locations = arc{1,2,3}* iota rounds
|
||||
initial_choices = arc{1,2,3}* iota rounds
|
||||
|
||||
staying_wins = length (filter ==) zip(car_locations,initial_choices)
|
||||
switching_wins = length (filter ~=) zip(car_locations,initial_choices)
|
||||
|
||||
format = printf/'%0.2f'+ (times\100.+ div+ float~~)\rounds
|
||||
|
||||
#show+
|
||||
|
||||
main = ~&plrTS/<'stay: ','switch: '> format* <staying_wins,switching_wins>
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#90 = Time_Tick // seed for random number generator
|
||||
#91 = 3 // random numbers in range 0 to 2
|
||||
#1 = 0 // wins for "always stay" strategy
|
||||
#2 = 0 // wins for "always switch" strategy
|
||||
for (#10 = 0; #10 < 10000; #10++) { // 10,000 iterations
|
||||
Call("RANDOM")
|
||||
#3 = Return_Value // #3 = winning door
|
||||
Call("RANDOM")
|
||||
#4 = Return_Value // #4 = players choice
|
||||
do {
|
||||
Call("RANDOM")
|
||||
#5 = Return_Value // #5 = door to open
|
||||
} while (#5 == #3 || #5 == #4)
|
||||
if (#3 == #4) { // original choice was correct
|
||||
#1++
|
||||
}
|
||||
if (#3 == 3 - #4 - #5) { // switched choice was correct
|
||||
#2++
|
||||
}
|
||||
}
|
||||
Ins_Text("Staying wins: ") Num_Ins(#1)
|
||||
Ins_Text("Switching wins: ") Num_Ins(#2)
|
||||
return
|
||||
|
||||
//--------------------------------------------------------------
|
||||
// Generate random numbers in range 0 <= Return_Value < #91
|
||||
// #90 = Seed (0 to 0x7fffffff)
|
||||
// #91 = Scaling (0 to 0xffff)
|
||||
|
||||
:RANDOM:
|
||||
#92 = 0x7fffffff / 48271
|
||||
#93 = 0x7fffffff % 48271
|
||||
#90 = (48271 * (#90 % #92) - #93 * (#90 / #92)) & 0x7fffffff
|
||||
return ((#90 & 0xffff) * #91 / 0x10000)
|
||||
31
Task/Monty-Hall-problem/XPL0/monty-hall-problem.xpl0
Normal file
31
Task/Monty-Hall-problem/XPL0/monty-hall-problem.xpl0
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
def Games = 10000; \number of games simulated
|
||||
int Game, Wins;
|
||||
include c:\cxpl\codes;
|
||||
|
||||
proc Play(Switch); \Play one game
|
||||
int Switch;
|
||||
int Car, Player, Player0, Monty;
|
||||
[Car:= Ran(3); \randomly place car behind a door
|
||||
Player0:= Ran(3); \player randomly chooses a door
|
||||
repeat Monty:= Ran(3); \Monty opens door revealing a goat
|
||||
until Monty # Car and Monty # Player0;
|
||||
if Switch then \player switches to remaining door
|
||||
repeat Player:= Ran(3);
|
||||
until Player # Player0 and Player # Monty
|
||||
else Player:= Player0; \player sticks with original door
|
||||
if Player = Car then Wins:= Wins+1;
|
||||
];
|
||||
|
||||
[Format(2,1);
|
||||
Text(0, "Not switching doors wins car in ");
|
||||
Wins:= 0;
|
||||
for Game:= 0 to Games-1 do Play(false);
|
||||
RlOut(0, float(Wins)/float(Games)*100.0);
|
||||
Text(0, "% of games.^M^J");
|
||||
|
||||
Text(0, "But switching doors wins car in ");
|
||||
Wins:= 0;
|
||||
for Game:= 0 to Games-1 do Play(true);
|
||||
RlOut(0, float(Wins)/float(Games)*100.0);
|
||||
Text(0, "% of games.^M^J");
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue