Skip to content

Instantly share code, notes, and snippets.

@leotada
Last active July 18, 2019 03:10
Show Gist options
  • Save leotada/fa909e517f18c3c9f9d4e8f63d3b8089 to your computer and use it in GitHub Desktop.
Save leotada/fa909e517f18c3c9f9d4e8f63d3b8089 to your computer and use it in GitHub Desktop.
Monty Hall problem simulation - written in D (Dlang)
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