0% found this document useful (0 votes)
10 views15 pages

L10. Math - Random Method in JS, CSE 202, BN11

JavaScript

Uploaded by

Saurav Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

L10. Math - Random Method in JS, CSE 202, BN11

JavaScript

Uploaded by

Saurav Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Course Code: CSE 202

Course Title: Computer Programming Lab


Lecture 10: Various methods of Math object

Course Teacher: Saurav Barua (SB)


Assistant Professor, Dept. of CE, DIU
Phone: +88-01715334075
Email: [email protected]
Contents

Getting a random Math.floor(),


Math.random()
number between Math.ceil()
method. two values methods

Math.round() toFixed() Practice problems


method method on math object

2
Math.random()
Syntax:
Math.random()

✓ The Math.random() static method returns a


floating-point, pseudo-random number that's
greater than or equal to 0 and less than 1, with
approximately uniform distribution over that range
— which you can then scale to your desired range.

3
Getting a random number between two values
Syntax:
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}

✓ This example returns a random number between the


specified values.
✓ The returned value is no lower than (and may possibly
equal) min, and is less than (and not equal) max.

4
Math.floor() method
Example:
console.log(Math.floor(5.95));
// Expected output: 5

✓ The Math.floor() static method always rounds down and


returns the largest integer less than or equal to a given
number.

5
Math.ceil() method
Example:
console.log(Math.ceil(7.004));
// Expected output: 8

✓ The Math.ceil() static method always rounds up and


returns the smallest integer greater than or equal to a
given number.

6
Math.round() method
Example:
console.log(Math.round(7.62));
// Expected output: 8

✓ The Math.round() static method returns the value of a


number rounded to the nearest integer.

7
toFixed() method
Example:
let num = 5.56789;
let n = num.toFixed(2);
// Expected output: 5.57

✓ The toFixed() method rounds the string to a specified


number of decimals.

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.)

Google drive link:


https://drive.google.com/drive/folders/1P2aEpbT7Kq2cFJEeLSs2TWt
re-ZUlmQz?usp=drive_link

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

You might also like