Write a script to simulate the rolling of two dice. The script should use Math.r
ID: 3722070 • Letter: W
Question
Write a script to simulate the rolling of two dice. The script should use Math.random to roll the first die and again to roll the second die. The two die make an ordered pair with the first die representing the row number and the second die representing the column number. Since each die can show an integer value from 1 to 6, the resulting table should have row values from 1 to 6 and the column values from 1 to 6. There are 36 possible combinations. Your program should prompt the user for the number of times to roll the dice, but should be a minimum of 10,000 times. Use a two dimensional array to tally the number of times each combination appears. Display the results in an HTML5 table. (Die_Combination)
Explanation / Answer
Please find below the html page with java script to perform the dice permutation activity
<!DOCTYPE html>
<html>
<body>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
<p>No of times to roll the dice : <input type="text" id="permutations"></p>
<button>Submit</button>
<p id="validation"></p>
<table id = "statistics">
<tr>
<th></th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>4</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>5</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<script>
function myFunction() {
var repetitions = document.getElementById("permutations").value;
if(repetitions < 10000){
document.getElementById("validation").innerHTML = "Invalid iterations (min 10000 iterations expected) " + repetitions;
}else{
document.getElementById("validation").innerHTML = "Expected iterations " + repetitions;
var table = document.getElementById("statistics");
for(i=0;i<repetitions; i++)
{
var try1 = Math.floor((Math.random() * 6) + 1);
var try2 = Math.floor((Math.random() * 6) + 1);
var score = parseInt(table.rows[try1].cells[try2].innerHTML);
var updated_score = score+1;
table.rows[try1].cells[try2].innerHTML = updated_score;
}
}
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.