Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
57
Task/Monty-Hall-problem/AWK/monty-hall-problem-1.awk
Normal file
57
Task/Monty-Hall-problem/AWK/monty-hall-problem-1.awk
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#!/bin/gawk -f
|
||||
|
||||
# Monty Hall problem
|
||||
|
||||
BEGIN {
|
||||
srand()
|
||||
doors = 3
|
||||
iterations = 10000
|
||||
# Behind a door:
|
||||
EMPTY = "empty"; PRIZE = "prize"
|
||||
# Algorithm used
|
||||
KEEP = "keep"; SWITCH="switch"; RAND="random";
|
||||
#
|
||||
}
|
||||
function monty_hall( choice, algorithm ) {
|
||||
# Set up doors
|
||||
for ( i=0; i<doors; i++ ) {
|
||||
door[i] = EMPTY
|
||||
}
|
||||
# One door with prize
|
||||
door[int(rand()*doors)] = PRIZE
|
||||
|
||||
chosen = door[choice]
|
||||
del door[choice]
|
||||
|
||||
#if you didn't choose the prize first time around then
|
||||
# that will be the alternative
|
||||
alternative = (chosen == PRIZE) ? EMPTY : PRIZE
|
||||
|
||||
if( algorithm == KEEP) {
|
||||
return chosen
|
||||
}
|
||||
if( algorithm == SWITCH) {
|
||||
return alternative
|
||||
}
|
||||
return rand() <0.5 ? chosen : alternative
|
||||
}
|
||||
|
||||
function simulate(algo){
|
||||
prizecount = 0
|
||||
for(j=0; j< iterations; j++){
|
||||
if( monty_hall( int(rand()*doors), algo) == PRIZE) {
|
||||
prizecount ++
|
||||
}
|
||||
}
|
||||
printf " Algorithm %7s: prize count = %i, = %6.2f%%\n", \
|
||||
algo, prizecount,prizecount*100/iterations
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
print "\nMonty Hall problem simulation:"
|
||||
print doors, "doors,", iterations, "iterations.\n"
|
||||
simulate(KEEP)
|
||||
simulate(SWITCH)
|
||||
simulate(RAND)
|
||||
|
||||
}
|
||||
9
Task/Monty-Hall-problem/AWK/monty-hall-problem-2.awk
Normal file
9
Task/Monty-Hall-problem/AWK/monty-hall-problem-2.awk
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
bash$ ./monty_hall.awk
|
||||
|
||||
Monty Hall problem simulation:
|
||||
3 doors, 10000 iterations.
|
||||
|
||||
Algorithm keep: prize count = 3411, = 34.11%
|
||||
Algorithm switch: prize count = 6655, = 66.55%
|
||||
Algorithm random: prize count = 4991, = 49.91%
|
||||
bash$
|
||||
Loading…
Add table
Add a link
Reference in a new issue