Реализации алгоритмов/Парадокс Монти Холла: различия между версиями

Содержимое удалено Содержимое добавлено
Пример на Дельфи удалил, так как код совсем не удачный, предлагаю хозяину его подправить
Строка 382:
Можно просто вставить в файл monty-hall.html и открыть в браузере
 
<source lang="javascript">
<pre>
<html>
<head>
<script type="text/javascript">
function getCarDoors(){
var doors = new Array(3);{
var doors = [0], =0, 0];
doors[1] var carIn = 0Math.floor(Math.random() * 3);
doors[2carIn] = 01;
var carIn = Math.floor(Math.random() * 3) return doors;
doors[carIn] = 1; }
 
return doors;
function game(tries)
}
function game(tries) {
var changedAndWonres = 0;
} {
var changedAndLost = 0;
changedAndWon: 0,
var keepAndWon = 0;
changedAndLost: 0,
var keepAndLost = 0;
for (j = 0; j < tries; j ++){ keepAndWon: 0,
var d = getCarDoors(); keepAndLost: 0
var myChoice = Math.floor(Math.random() * 3) };
 
var changeChoice = Math.floor(Math.random() * 2) == 1 ? true : false;
var otherOpen for (j = 0; j < tries; j++)
var otherClosed; {
var openedd = falsegetCarDoors();
for (i = 0; i < 3; i ++ var myChoice = Math.floor(Math.random(){ * 3);
if (i != myChoice && d[i] = var changeChoice = 0Math.floor(Math.random() &&* !opened2){ == 1;
otherOpen = i var otherOpen;
opened = true var otherClosed;
} else if (i ! var opened = myChoice){false;
otherClosed for (i = 0; i < 3; i++)
} {
if (i != myChoice && d[i] == 0 && !opened)
}
var selected = changeChoice ? otherClosed : myChoice; {
if (d[selected] = otherOpen = 1){i;
if (changeChoice){ opened = true;
changedAndWon ++; }
} else {if (i != myChoice)
keepAndWon++; {
} otherClosed = i;
} else { }
if (changeChoice){ }
changedAndLost ++ var selected = changeChoice ? otherClosed : myChoice;
} else { if (d[selected])
keepAndLost++; {
} if (changeChoice)
res.changedAndWon++;
}
} else
var res = new Array(4) res.keepAndWon++;
res["changeAndWon"] = changedAndWon; }
res["changedAndLost"] = changedAndLost; else
res["keepAndWon"] = keepAndWon; {
if (changeChoice)
res["keepAndLost"] = keepAndLost;
return res.changedAndLost++;
else
}
res.keepAndLost++;
function presentGame(count, divId){
var res = game(count); }
}
var changedAndWon = res["changeAndWon"];
return doorsres;
var changedAndLost = res["changedAndLost"];
}
var keepAndWon = res["keepAndWon"];
 
var keepAndLost = res["keepAndLost"];
function presentGame(count, divId){
var changeOkProb = changedAndWon / ((changedAndLost + changedAndWon) > 0 ? (changedAndLost + changedAndWon) : 1);
{
var keepOkProb = keepAndWon / ((keepAndWon + keepAndLost) > 0 ? (keepAndWon + keepAndLost) : 1);
var res = game(count);
var text = "<tr><td>Number of tries: " + "</td><td><b>" + count + "</b></td></tr>";
var changeOkProb = res.changedAndWon / ((res.changedAndLost + res.changedAndWon) > 0 ? (res.changedAndLost + res.changedAndWon) : 1);
text += "<tr><td>kept decision and won: " + "</td><td>" +keepAndWon + "</td></tr>";
text += "<tr><td>kept decision and lost: " + "<var keepOkProb = res.keepAndWon /td><td>" ((res.keepAndWon + res.keepAndLost) > 0 ? (res.keepAndWon + "</td></tr>"res.keepAndLost) : 1);
var text += "<tr><td>changedNumber decisionof and wontries: " + "</td><td><b>" +changedAndWon count + "</b></td></tr>";
text += "<tr><td>changedkept decision and lostwon: " + "</td><td>" +changedAndLost res.keepAndWon + "</td></tr>";
text += "<tr><td>Probabilitykept todecision winand if change decisionlost: " + "</td><td>" + "<b>" + (Mathres.round(changeOkProb * 10000) / 100) + "%</b>"keepAndLost + "</td></tr>";
text += "<tr><td>Probabilitychanged todecision winand if keep decisionwon: " + "</td><td>" + "<b>" + (Mathres.round(keepOkProb * 10000) / 100) + "%</b>"changedAndWon + "</td></tr>";
var text += "<tr><td>Numberchanged ofdecision triesand lost: " + "</td><td><b>" + countres.changedAndLost + "</b></td></tr>";
document.getElementById(divId).innerHTML = "<table>" + text + "</table>";
text += "<tr><td>Probability to win if change decision: " + "</td><td>" + "<b>" + (Math.round(changeOkProb * 10000) / 100) + "%</b>" + "</td></tr>";
}
text += "<tr><td>Probability to win if keep decision: " + "</td><td>" + "<b>" + (Math.round(keepOkProb * 10000) / 100) + "%</b>" + "</td></tr>";
</script>
document.getElementById(divId).innerHTML = "<table>" + text + "</table>";
</head>
}
<body>
</script>
<h1>Monty Hall problem</h1>
</head>
Number of tries:
<body>
<input type="text" value="100" id="tries" />
<h1>Monty Hall problem</h1>
<input type="button" value="Run simulation"
Number of tries:
onClick="presentGame(document.getElementById('tries').value, 'data'); return false;" />
<input type="text" value="100" id="tries" />
<br />
<input type="button" value="Run simulation" onClick="presentGame(document.getElementById('tries').value, 'data'); return false;" />
<div id="data"></div>
<br />
</body>
<div id="data"></div>
</body>
</html>
 
</pre>
</source>
 
== Программа на [[w:Turbo Pascal|Turbo Pascal]], моделирующая задачу ==