September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
15
Task/Monty-Hall-problem/BASIC/monty-hall-problem-2.basic
Normal file
15
Task/Monty-Hall-problem/BASIC/monty-hall-problem-2.basic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
10 PRINT " WINS IF YOU"
|
||||
20 PRINT "STICK","SWITCH"
|
||||
30 LET STICK=0
|
||||
40 LET SWITCH=0
|
||||
50 FOR I=1 TO 1000
|
||||
60 LET CAR=INT (RND*3)
|
||||
70 LET GUESS=INT (RND*3)
|
||||
80 LET SHOW=INT (RND*3)
|
||||
90 IF SHOW=GUESS OR SHOW=CAR THEN GOTO 80
|
||||
100 LET NEWGUESS=INT (RND*3)
|
||||
110 IF NEWGUESS=GUESS OR NEWGUESS=SHOW THEN GOTO 100
|
||||
120 IF GUESS=CAR THEN LET STICK=STICK+1
|
||||
130 IF NEWGUESS=CAR THEN LET SWITCH=SWITCH+1
|
||||
140 PRINT AT 2,0;STICK,SWITCH
|
||||
150 NEXT I
|
||||
28
Task/Monty-Hall-problem/Kotlin/monty-hall-problem.kotlin
Normal file
28
Task/Monty-Hall-problem/Kotlin/monty-hall-problem.kotlin
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.util.Random
|
||||
|
||||
fun montyHall(games: Int) {
|
||||
var switchWins = 0
|
||||
var stayWins = 0
|
||||
val rnd = Random()
|
||||
(1..games).forEach {
|
||||
val doors = IntArray(3) // all zero (goats) by default
|
||||
doors[rnd.nextInt(3)] = 1 // put car in a random door
|
||||
val choice = rnd.nextInt(3) // choose a door at random
|
||||
var shown: Int
|
||||
do {
|
||||
shown = rnd.nextInt(3) // the shown door
|
||||
}
|
||||
while (doors[shown] == 1 || shown == choice)
|
||||
stayWins += doors[choice]
|
||||
switchWins += doors[3 - choice - shown]
|
||||
}
|
||||
println("Simulating $games games:")
|
||||
println("Staying wins $stayWins times")
|
||||
println("Switching wins $switchWins times")
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
montyHall(1_000_000)
|
||||
}
|
||||
22
Task/Monty-Hall-problem/Ring/monty-hall-problem.ring
Normal file
22
Task/Monty-Hall-problem/Ring/monty-hall-problem.ring
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
total = 10000
|
||||
swapper = 0
|
||||
sticker = 0
|
||||
revealdoor = 0
|
||||
for trial = 1 to total
|
||||
prizedoor = random(3) + 1
|
||||
guessdoor = random(3) + 1
|
||||
if prizedoor = guessdoor
|
||||
revealdoor = random(2) + 1
|
||||
if prizedoor = 1 revealdoor += 1 ok
|
||||
if (prizedoor = 2 and revealdoor = 2) revealdoor = 3 ok
|
||||
else
|
||||
revealdoor = (prizedoor ^ guessdoor)
|
||||
ok
|
||||
stickdoor = guessdoor
|
||||
swapdoor = (guessdoor ^ revealdoor)
|
||||
if stickdoor = prizedoor sticker += 1 ok
|
||||
if swapdoor = prizedoor swapper += 1 ok
|
||||
next
|
||||
see "after a total of " + total + " trials," + nl
|
||||
see "the 'sticker' won " + sticker + " times (" + floor(sticker/total*100) + "%)" + nl
|
||||
see "the 'swapper' won " + swapper + " times (" + floor(swapper/total*100) + "%)" + nl
|
||||
26
Task/Monty-Hall-problem/Rust/monty-hall-problem.rust
Normal file
26
Task/Monty-Hall-problem/Rust/monty-hall-problem.rust
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
extern crate rand;
|
||||
use rand::Rng;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Prize {Goat , Car}
|
||||
|
||||
const GAMES: usize = 3_000_000;
|
||||
fn main() {
|
||||
let mut switch_wins = 0;
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
for _ in 0..GAMES {
|
||||
let mut doors = [Prize::Goat; 3];
|
||||
*rng.choose_mut(&mut doors).unwrap() = Prize::Car;
|
||||
|
||||
// You only lose by switching if you pick the car the first time
|
||||
if rng.choose(&doors).unwrap() != &Prize::Car {
|
||||
switch_wins += 1;
|
||||
}
|
||||
}
|
||||
println!("I played the game {total} times and won {wins} times ({percent}%).",
|
||||
total = GAMES,
|
||||
wins = switch_wins,
|
||||
percent = switch_wins as f64 / GAMES as f64 * 100.0
|
||||
);
|
||||
}
|
||||
|
|
@ -1,21 +1,21 @@
|
|||
var n = 1000; # number of times to play
|
||||
var switchWins = (var stayWins = 0); # sum of each strategy's wins
|
||||
|
||||
var n = 1000 # number of times to play
|
||||
var switchWins = (var stayWins = 0) # sum of each strategy's wins
|
||||
|
||||
n.times { # play the game n times
|
||||
var prize = 3.rand.int;
|
||||
var chosen = 3.rand.int;
|
||||
|
||||
var prize = pick(^3)
|
||||
var chosen = pick(^3)
|
||||
|
||||
var show;
|
||||
do {
|
||||
show = 3.rand.int
|
||||
} while (show ~~ [chosen, prize]);
|
||||
|
||||
show = pick(^3)
|
||||
} while (show ~~ [chosen, prize])
|
||||
|
||||
given(chosen) {
|
||||
when (prize) { stayWins += 1 }
|
||||
when ([3 - show - prize]) { switchWins += 1 }
|
||||
default { die "~ error ~" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
say ("Staying wins %.2f%% of the time." % (100.0 * stayWins / n));
|
||||
say ("Switching wins %.2f%% of the time." % (100.0 * switchWins / n));
|
||||
|
||||
say ("Staying wins %.2f%% of the time." % (100.0 * stayWins / n))
|
||||
say ("Switching wins %.2f%% of the time." % (100.0 * switchWins / n))
|
||||
|
|
|
|||
17
Task/Monty-Hall-problem/Zkl/monty-hall-problem.zkl
Normal file
17
Task/Monty-Hall-problem/Zkl/monty-hall-problem.zkl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
const games=0d100_000;
|
||||
|
||||
reg switcherWins=0, keeperWins=0, shown=0;
|
||||
do(games){
|
||||
doors := L(0,0,0);
|
||||
doors[(0).random(3)] = 1; // Set which one has the car
|
||||
choice := (0).random(3); // Choose a door
|
||||
while(1){ shown = (0).random(3);
|
||||
if (not (shown == choice or doors[shown] == 1)) break; }
|
||||
switcherWins += doors[3 - choice - shown];
|
||||
keeperWins += doors[choice];
|
||||
}
|
||||
|
||||
"Switcher Wins: %,d (%3.2f%%)".fmt(
|
||||
switcherWins, switcherWins.toFloat() / games * 100).println();
|
||||
"Keeper Wins: %,d (%3.2f%%)".fmt(
|
||||
keeperWins, keeperWins.toFloat() / games * 100).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue