Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
54
Task/Monty-Hall-problem/PowerShell/monty-hall-problem-1.ps1
Normal file
54
Task/Monty-Hall-problem/PowerShell/monty-hall-problem-1.ps1
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#Declaring variables
|
||||
$intIterations = 10000
|
||||
$intKept = 0
|
||||
$intSwitched = 0
|
||||
|
||||
#Creating a function
|
||||
Function Play-MontyHall()
|
||||
{
|
||||
#Using a .NET object for randomization
|
||||
$objRandom = New-Object -TypeName System.Random
|
||||
|
||||
#Generating the winning door number
|
||||
$intWin = $objRandom.Next(1,4)
|
||||
|
||||
#Generating the chosen door
|
||||
$intChoice = $objRandom.Next(1,4)
|
||||
|
||||
#Generating the excluded number
|
||||
#Because there is no method to exclude a number from a range,
|
||||
#I let it re-generate in case it equals the winning number or
|
||||
#in case it equals the chosen door.
|
||||
$intLose = $objRandom.Next(1,4)
|
||||
While (($intLose -EQ $intWin) -OR ($intLose -EQ $intChoice))
|
||||
{$intLose = $objRandom.Next(1,4)}
|
||||
|
||||
#Generating the 'other' door
|
||||
#Same logic applies as for the chosen door: it cannot be equal
|
||||
#to the winning door nor to the chosen door.
|
||||
$intSwitch = $objRandom.Next(1,4)
|
||||
While (($intSwitch -EQ $intLose) -OR ($intSwitch -EQ $intChoice))
|
||||
{$intSwitch = $objRandom.Next(1,4)}
|
||||
|
||||
#Simple counters per win for both categories
|
||||
#Because a child scope cannot change variables in the parent
|
||||
#scope, the scope of the counters is expanded script-wide.
|
||||
If ($intChoice -EQ $intWin)
|
||||
{$script:intKept++}
|
||||
If ($intSwitch -EQ $intWin)
|
||||
{$script:intSwitched++}
|
||||
|
||||
}
|
||||
|
||||
#Looping the Monty Hall function for $intIterations times
|
||||
While ($intIterationCount -LT $intIterations)
|
||||
{
|
||||
Play-MontyHall
|
||||
$intIterationCount++
|
||||
}
|
||||
|
||||
#Output
|
||||
Write-Host "Results through $intIterations iterations:"
|
||||
Write-Host "Keep : $intKept ($($intKept/$intIterations*100)%)"
|
||||
Write-Host "Switch: $intSwitched ($($intSwitched/$intIterations*100)%)"
|
||||
Write-Host ""
|
||||
39
Task/Monty-Hall-problem/PowerShell/monty-hall-problem-2.ps1
Normal file
39
Task/Monty-Hall-problem/PowerShell/monty-hall-problem-2.ps1
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Monty Hall Problem
|
||||
$script:NGames = 10000
|
||||
|
||||
function Is-Game-Won($Sw) {
|
||||
# Play one game.
|
||||
# Switching if and only if $Sw -ne 0.
|
||||
$car = Get-Random -maximum 3 # Randomly place car behind a door.
|
||||
$player0 = Get-Random -maximum 3 # Player randomly chooses a door.
|
||||
do {
|
||||
$monty = Get-Random -maximum 3 # Monty opens door revealing a goat.
|
||||
} while (($monty -eq $car) -or ($monty -eq $player0))
|
||||
if ($Sw -ne 0) { # Player switches to remaining door.
|
||||
do {
|
||||
$player = Get-Random -maximum 3
|
||||
} while (($player -eq $player0) -or ($player -eq $monty))
|
||||
} else {
|
||||
$player = $player0 # Player sticks with original door.
|
||||
}
|
||||
return [bool]($player -eq $car)
|
||||
}
|
||||
|
||||
$nWins = 0
|
||||
foreach ($game in 1..$script:NGames) {
|
||||
if (Is-Game-Won(0)) {
|
||||
$nWins++
|
||||
}
|
||||
}
|
||||
$row = "NOT switching doors wins car in "
|
||||
$row += "$(($nWins / $script:NGames * 100).ToString('##.#'))% of games."
|
||||
Write-Output $row
|
||||
$nWins = 0
|
||||
foreach ($game in 1..$script:NGames) {
|
||||
if (Is-Game-Won(1) -ne 0) {
|
||||
$nWins++
|
||||
}
|
||||
}
|
||||
$row = "But switching doors wins car in "
|
||||
$row += "$(($nWins / $script:NGames * 100).ToString('##.#'))% of games."
|
||||
Write-Output $row
|
||||
Loading…
Add table
Add a link
Reference in a new issue