L10. Math - Random Method in JS, CSE 202, BN11
L10. Math - Random Method in JS, CSE 202, BN11
2
Math.random()
Syntax:
Math.random()
3
Getting a random number between two values
Syntax:
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
4
Math.floor() method
Example:
console.log(Math.floor(5.95));
// Expected output: 5
5
Math.ceil() method
Example:
console.log(Math.ceil(7.004));
// Expected output: 8
6
Math.round() method
Example:
console.log(Math.round(7.62));
// Expected output: 8
7
toFixed() method
Example:
let num = 5.56789;
let n = num.toFixed(2);
// Expected output: 5.57
8
Worked out project
10.0 Project name: Program to generate random integer number
within a range and display in the browser. (Generate random spot
speed reading of traffic within 20-100 km/hr from a speed gun in a
road segment.)
Github link:
https://github.com/sauravbarua02/randomSpotSpeed
9
Interface
10
html codes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>mathRandom</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<p id="display"></p>
</div>
<script src="app.js"></script>
</body>
</html>
11
css codes
body{
margin: 0px;
background: rgba(169, 182, 134, 0.7); display: flex;
} flex-direction: column;
justify-content: center;
.container{ align-items: center;
background-color: rgba(169, 182, 134, border-radius: 20px;
0.4); box-shadow: 0 0 5px 1px rgba(0,0,0,0.5);
width: 300px;
height: 300px; }
margin: 10px auto;
padding: 5px;
12
JS codes
//random integer number generation
const displayEl =
document.getElementById("display"); const speedVal = randomGenerator(100,20);
displayEl.innerText = "The recorded random
function spot speed = "+ speedVal +" km/hr";
randomGenerator(upperLimit,lowerLimit){
const speed =
Math.floor(Math.random()*(upperLimit-
lowerLimit)+ lowerLimit);
return speed;
}
13
Class tasks
Task 10.1: Program to generate random CBR value of a subgrade soil sample obtained from
the CBR test within 70 to 98 and display in the browser.
Task 10.2: Program to generate random 28 days’ crushing strength of a concrete cylinder
specimen from the concrete compressive strength test within 2500 psi to 3500 psi and display
in the browser.
Task 10.3: Program to generate random BOD5 value obtained from a dissolved oxygen (DO)
meter within 2 mg/L to 8 mg/L and display in the browser. (hints: Do not use Math.floor, use
toFixed(2) at the end of the calculated expression)
Task 10.4: Program to generate random deflection reading obtained from a strain gauge in the
beam deflection test within 0.0001 mm to 0.001 mm and display in the browser. (hints: Do not
use Math.floor, use toFixed(4) at the end of the calculated expression)
Task 10.5: Program to generate random hardness value obtained using 1200 diamond cone
indenter from the Rockwell hardness test within 20.0 to 65.0 and display in the browser. (hints:
Do not use Math.floor, use toFixed(2) at the end of the calculated expression)
14
End of the Lecture
15