<!DOCTYPE html>
<html>
<head>
<title>Task 1.1</title>
<script>
function init() {
// Generates a random integer from -10 to 10
var d = Math.floor(Math.random() * 21) - 10;
document.getElementById("result").innerHTML = "The random integer is: " + d;
}
</script>
</head>
<body onload="init();">
<div id="result"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Task 1.2</title>
<script>
function init() {
var count = 0, d = 0, s = "";
s = "The dice throw outcomes are listed below: <br/>";
while (d !== 6) {
d = Math.floor(Math.random() * 6) + 1;
count++;
s += count + ". " + d + "<br/>";
}
s += "The total number of dice throws = " + count;
document.getElementById("result").innerHTML = s;
}
</script>
</head>
<body onload="init();">
<div id="result"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Task 1.3</title>
<script>
function randomUppercaseCharacter() {
var d = Math.floor(Math.random() * 26);
return String.fromCharCode(65 + d);
}
function init() {
var s1 = randomUppercaseCharacter();
var s2 = randomUppercaseCharacter();
document.getElementById("result").innerHTML = s1 + s2;
}
</script>
</head>
<body onload="init();">
<div id="result"><br/></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Task 2.1</title>
<script>
function init() {
var s = "";
// 1. Generate x either -1 or 1
var x1_no_cond = Math.floor(Math.random() * 2) * 2 - 1;
var x1_cond = (Math.random() < 0.5) ? -1 : 1;
s += "<b>Case 1 {-1, 1}:</b> " + x1_no_cond + " (without cond), " + x1_cond + " (with cond)<br/><br/>";
// 2. Generate x in {0, 1, 2} with probs 0.25, 0.5, 0.25
var x2_no_cond = Math.floor(Math.random() * 2) + Math.floor(Math.random() * 2);
var r = Math.random();
var x2_cond = (r < 0.25) ? 0 : ((r < 0.75) ? 1 : 2);
s += "<b>Case 2 {0, 1, 2}:</b> " + x2_no_cond + " (without cond), " + x2_cond + " (with cond)<br/><br/>";
// 3. Generate random float (-3, -1] U [1, 3)
var val3 = Math.random() * 2 + 1;
var x3_no_cond = val3 * (Math.floor(Math.random() * 2) * 2 - 1);
var x3_cond = (Math.random() < 0.5) ? val3 : -val3;
s += "<b>Case 3 (-3, -1] U [1, 3):</b> " + x3_no_cond.toFixed(4) + " (without cond), " + x3_cond.toFixed(4) + " (with cond)<br/>";
document.getElementById("result").innerHTML = s;
}
</script>
</head>
<body onload="init();">
<div id="result"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Task 2.2</title>
<script>
function init() {
var count = 0;
var d1 = 0, d2 = 0, sum = 0;
var s = "The dice throw outcomes are listed below: <br/>";
while (sum !== 7) {
d1 = Math.floor(Math.random() * 6) + 1;
d2 = Math.floor(Math.random() * 6) + 1;
sum = d1 + d2;
count++;
s += count + ". " + d1 + " + " + d2 + " = " + sum + "<br/>";
}
s += "The total number of dice throws = " + count;
document.getElementById("result").innerHTML = s;
}
</script>
</head>
<body onload="init();">
<div id="result"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Task 2.3</title>
<script>
function randomLowercaseCharacter() {
var d = Math.floor(Math.random() * 26);
return String.fromCharCode(97 + d);
}
function randomUppercaseCharacter() {
var d = Math.floor(Math.random() * 26);
return String.fromCharCode(65 + d);
}
function init() {
var s = "<b>Part 1: Random Lowercase Alphabet</b><br/>";
s += randomLowercaseCharacter() + "<br/><br/>";
s += "<b>Part 2 & 3: Generate until 'Z' is displayed</b><br/>";
var countTotal = 0;
var countA = 0;
var char = "";
while (char !== 'Z') {
char = randomUppercaseCharacter();
countTotal++;
s += char + " ";
if (char === 'A') {
countA++;
}
}
s += "<br/><br/>The total number of generated characters = " + countTotal + "<br/>";
s += "The total number of 'A' generated = " + countA;
document.getElementById("result").innerHTML = s;
}
</script>
</head>
<body onload="init();">
<div id="result"></div>
</body>
</html>