<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width" />
<title>Hello, world!</title>
</head>
<body>
<h1>6*x throws of the dice result in small error ("no six")</h1>
<label for="var1">How many times we want to throw 6 dices? (e.g. 4):</label>
<input type="number" id="var1" required>
<br>
<br>
<button id="run">Calculate</button>
<br>
<p id="debug"></p>
<p id="result"></p>
<script>
function sixExists() {
const target_end = 1000;
// Get user inputs from HTML elements
const xElement = document.getElementById("var1");
const x = parseFloat(xElement.value);
const debugElement = document.getElementById("debug");
const resultElement = document.getElementById("result");
debugElement.textContent = `x: ${x}.`;
resultElement.textContent = ``;
for (let j = 0; j < 10; j++) {
let attempts = 0;
let counter = 0;
let total = 0;
for (attempts = 0; attempts < target_end; attempts++) {
for (let i = 0; i < 6 * x; i++) {
const diceRoll = Math.floor(Math.random() * 6) + 1;
if (diceRoll === 6) {
counter++;
break;
}
}
if (counter > 0) {
total++;
counter = 0;
}
}
let label = document.createElement('label');
label.textContent = `Total 6s: ${total} in Attempts: ${attempts}. Error: ${(100 - 100.0 * total / attempts).toFixed(2)}%.`;
let br = document.createElement('br');
resultElement.appendChild(label);
resultElement.appendChild(br);
}
}
// Attach an event listener to the calculate button
const calculateButton = document.getElementById("run");
calculateButton.addEventListener("click", sixExists);
</script>
</body>
</html>