Last active
July 18, 2019 03:10
-
-
Save leotada/fa909e517f18c3c9f9d4e8f63d3b8089 to your computer and use it in GitHub Desktop.
Monty Hall problem simulation - written in D (Dlang)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.random; | |
import std.algorithm; | |
bool make_game(bool change) | |
{ | |
static auto list = [1, 2, 3]; | |
int car = list.choice(); | |
int guest = list.choice(); | |
if (change) | |
{ | |
if(guest == car) | |
return true; | |
else | |
{ | |
list = list.remove!(a => a == guest); | |
guest = list.choice(); | |
return guest == car; | |
} | |
} | |
else | |
return guest == car; | |
} | |
void main() | |
{ | |
immutable bool change = true; | |
immutable int times = 100; | |
int count = 0; | |
foreach(i; 0 .. times) | |
{ | |
if (make_game(change)) | |
count++; | |
} | |
writeln(count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment