This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

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,27 @@
-module(monty_hall).
-export([main/0]).
main() ->
random:seed(now()),
{WinStay, WinSwitch} = experiment(100000, 0, 0),
io:format("Switching wins ~p times.\n", [WinSwitch]),
io:format("Staying wins ~p times.\n", [WinStay]).
experiment(0, WinStay, WinSwitch) ->
{WinStay, WinSwitch};
experiment(N, WinStay, WinSwitch) ->
Doors = setelement(random:uniform(3), {0,0,0}, 1),
SelectedDoor = random:uniform(3),
OpenDoor = open_door(Doors, SelectedDoor),
experiment(
N - 1,
WinStay + element(SelectedDoor, Doors),
WinSwitch + element(6 - (SelectedDoor + OpenDoor), Doors) ).
open_door(Doors,SelectedDoor) ->
OpenDoor = random:uniform(3),
case (element(OpenDoor, Doors) =:= 1) or (OpenDoor =:= SelectedDoor) of
true -> open_door(Doors, SelectedDoor);
false -> OpenDoor
end.

View file

@ -1,63 +1,27 @@
package main
import (
"fmt"
"math/rand"
"fmt"
"math/rand"
"time"
)
func main() {
games := 1000000
var switchWinsCar, keepWinsCar int
for i := 0; i < games; i++ {
// simulate game
carDoor := rand.Intn(3)
firstChoice := rand.Intn(3)
var hostOpens int
if carDoor == firstChoice {
hostOpens = rand.Intn(2)
if hostOpens >= carDoor {
hostOpens++
}
} else {
hostOpens = 3 - carDoor - firstChoice
}
remainingDoor := 3 - hostOpens - firstChoice
games := 100000
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// some assertions that above code produced a valid game state
if carDoor < 0 || carDoor > 2 {
panic("car behind invalid door")
}
if firstChoice < 0 || firstChoice > 2 {
panic("contestant chose invalid door")
}
if hostOpens < 0 || hostOpens > 2 {
panic("host opened invalid door")
}
if hostOpens == carDoor {
panic("host opened door with car")
}
if hostOpens == firstChoice {
panic("host opened contestant's first choice")
}
if remainingDoor < 0 || remainingDoor > 2 {
panic("remaining door invalid")
}
if remainingDoor == firstChoice {
panic("remaining door same as contestant's first choice")
}
if remainingDoor == hostOpens {
panic("remaining door same as one host opened")
}
// tally results
if firstChoice == carDoor {
keepWinsCar++
}
if remainingDoor == carDoor {
switchWinsCar++
}
}
fmt.Println("In", games, "games,")
fmt.Println("switching doors won the car", switchWinsCar, "times,")
fmt.Println("keeping same door won the car", keepWinsCar, "times.")
var switcherWins, keeperWins, shown int
for i := 0; i < games; i++ {
doors := []int{0, 0, 0}
doors[r.Intn(3)] = 1 // Set which one has the car
choice := r.Intn(3) // Choose a door
for shown = r.Intn(3); shown == choice || doors[shown] == 1; shown = r.Intn(3) {}
switcherWins += doors[3 - choice - shown]
keeperWins += doors[choice]
}
floatGames := float32(games)
fmt.Printf("Switcher Wins: %d (%3.2f%%)\n",
switcherWins, (float32(switcherWins) / floatGames * 100))
fmt.Printf("Keeper Wins: %d (%3.2f%%)",
keeperWins, (float32(keeperWins) / floatGames * 100))
}

View file

@ -0,0 +1,35 @@
/* NetRexx ************************************************************
* 30.08.2013 Walter Pachl translated from Java/REXX/PL/I
**********************************************************************/
options replace format comments java crossref savelog symbols nobinary
doors = create_doors
switchWins = 0
stayWins = 0
shown=0
Loop plays=1 To 1000000
doors=0
r=r3()
doors[r]=1
choice = r3()
loop Until shown<>choice & doors[shown]=0
shown = r3()
End
If doors[choice]=1 Then
stayWins=stayWins+1
Else
switchWins=switchWins+1
End
Say "Switching wins " switchWins " times."
Say "Staying wins " stayWins " times."
method create_doors static returns Rexx
doors = ''
doors[0] = 0
doors[1] = 0
doors[2] = 0
return doors
method r3 static
rand=random()
return rand.nextInt(3) + 1

View file

@ -0,0 +1,38 @@
*process source attributes xref;
ziegen: Proc Options(main);
/* REXX ***************************************************************
* 30.08.2013 Walter Pachl derived from Java
**********************************************************************/
Dcl (switchWins,stayWins) Bin Fixed(31) Init(0);
Dcl doors(3) Bin Fixed(31);
Dcl (plays,r,choice) Bin Fixed(31) Init(0);
Dcl c17 Char(17) Init((datetime()));
Dcl p9 Pic'(9)9' def(c17) pos(5);
i=random(p9);
Do plays=1 To 1000000;
doors=0;
r=r3();
doors(r)=1;
choice=r3();
Do Until(shown^=choice & doors(shown)=0);
shown=r3();
End;
If doors(choice)=1 Then
stayWins+=1;
Else
switchWins+=1;
End;
Put Edit("Switching wins ",switchWins," times.")(Skip,a,f(6),a);
Put Edit("Staying wins ",stayWins ," times.")(Skip,a,f(6),a);
r3: Procedure Returns(Bin Fixed(31));
/*********************************************************************
* Return a random integer: 1, 2, or 3
*********************************************************************/
Dcl r Bin Float(53);
Dcl res Bin Fixed(31);
r=random();
res=(r*3)+1;
Return(res);
End;
End;

View file

@ -0,0 +1,31 @@
/* REXX ***************************************************************
* 30.08.2013 Walter Pachl derived from Java
**********************************************************************/
Call time 'R'
switchWins = 0;
stayWins = 0
Do plays = 1 To 1000000
doors.=0
r=r3()
doors.r=1
choice = r3()
Do Until shown<>choice & doors.shown=0
shown = r3()
End
If doors.choice=1 Then
stayWins=stayWins+1
Else
switchWins=switchWins+1
End
Say "Switching wins " switchWins " times."
Say "Staying wins " stayWins " times."
Say 'REXX:' time('E') 'seconds'
Call time 'R'
'ziegen'
Say 'PL/I:' time('E') 'seconds'
Say ' '
Call time 'R'
'java ziegen'
Say 'NetRexx:' time('E') 'seconds'
Exit
r3: Return random(2)+1