SlideShare a Scribd company logo
1 of 8
Download to read offline
JavaScript Random number
generation
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College
Perundurai
Dr.T.Abirami/KEC
Math.random()
• This function is used to return a floating-point
pseudo-random number between range [0,1) ,
0 (inclusive) and 1 (exclusive).
• This random number can then be scaled
according to the desired range.
Dr.T.Abirami/KEC
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML
= Math.random();
</script>
Math.random() returns a random number
between 0 (inclusive) and 1 (exclusive):
Generating a random floating point number
between 0 and 1
Dr.T.Abirami/KEC
Generating a random floating point number
between 0 and a specified max
• var x = Math.random()*10;
<html>
<body>
<h1>example</h1>
<h1>
<script>
var x = Math.random()*10;
document.write(x);
</script>
</h1>
</body>
</html>
Dr.T.Abirami/KEC
<html>
<body>
<h1> example</h1>
<h1>
<script>
var x = Math.floor(Math.random()*10);
document.write(x);
</script>
</h1>
</body>
</html>
The Math.floor() method rounds a number
DOWNWARDS to the nearest integer, and
returns the result. Dr.T.Abirami/KEC
Generating a random floating point
number within a range
var min = 83.1;
var max = 193.36;
var x = Math.random()*(max - min)+min;
console.log(x); // 126.94014012699063
Dr.T.Abirami/KEC
Example 1: This example generate an integer
random number between 1(min) and 6(max).
<script>
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}
document.write("Random Number between 1 and 6: ")
document.write( randomNumber(1, 6) );
</script>
Dr.T.Abirami/KEC
Displaying random images
<html>
<body>
<h1>example</h1>
<h1>
<script>
var images = [], index = 0;
images[0] = "<img src='D:/images/1.jpg' alt='1'>";
images[1] = "<img src='D:/images/2.jpg' alt='2'>";
images[2] = "<img src='D:/images/3.jpg' alt='3'>";
images[3] = "<img src='D:/images/4.jpg' alt='3'>";
images[4] = "<img src='D:/images/5.jpg' alt='3'>";
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
</script>
</h1>
</body>
</html> Dr.T.Abirami/KEC

More Related Content

Similar to Random number generation_upload.pdf

Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Pytho-Chapter-4-Functions.pptx
Pytho-Chapter-4-Functions.pptxPytho-Chapter-4-Functions.pptx
Pytho-Chapter-4-Functions.pptx
RaviAr5
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
Paulo Morgado
 

Similar to Random number generation_upload.pdf (20)

Price of an Error
Price of an ErrorPrice of an Error
Price of an Error
 
FYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III JavascriptFYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III Javascript
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdf
 
Lecture17 ie321 dr_atifshahzad_js
Lecture17 ie321 dr_atifshahzad_jsLecture17 ie321 dr_atifshahzad_js
Lecture17 ie321 dr_atifshahzad_js
 
Javascript math boolean string date
Javascript math boolean string dateJavascript math boolean string date
Javascript math boolean string date
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittest
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
Maharashtra state board Hsc IT Chap 3.pdf
Maharashtra state board Hsc IT Chap 3.pdfMaharashtra state board Hsc IT Chap 3.pdf
Maharashtra state board Hsc IT Chap 3.pdf
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 
Pytho-Chapter-4-Functions.pptx
Pytho-Chapter-4-Functions.pptxPytho-Chapter-4-Functions.pptx
Pytho-Chapter-4-Functions.pptx
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
 
淺談高效撰寫單元測試
淺談高效撰寫單元測試淺談高效撰寫單元測試
淺談高效撰寫單元測試
 
Using xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing ToolkitUsing xUnit as a Swiss-Aarmy Testing Toolkit
Using xUnit as a Swiss-Aarmy Testing Toolkit
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
Headache from using mathematical software
Headache from using mathematical softwareHeadache from using mathematical software
Headache from using mathematical software
 
Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#
 

More from Kongu Engineering College, Perundurai, Erode

More from Kongu Engineering College, Perundurai, Erode (20)

Introduction to Spring & Spring BootFramework
Introduction to Spring  & Spring BootFrameworkIntroduction to Spring  & Spring BootFramework
Introduction to Spring & Spring BootFramework
 
A REST API (also called a RESTful API or RESTful web API) is an application p...
A REST API (also called a RESTful API or RESTful web API) is an application p...A REST API (also called a RESTful API or RESTful web API) is an application p...
A REST API (also called a RESTful API or RESTful web API) is an application p...
 
SOA and Monolith Architecture - Micro Services.pptx
SOA and Monolith Architecture - Micro Services.pptxSOA and Monolith Architecture - Micro Services.pptx
SOA and Monolith Architecture - Micro Services.pptx
 
Application Layer.pptx
Application Layer.pptxApplication Layer.pptx
Application Layer.pptx
 
Connect to NoSQL Database using Node JS.pptx
Connect to NoSQL Database using Node JS.pptxConnect to NoSQL Database using Node JS.pptx
Connect to NoSQL Database using Node JS.pptx
 
Node_basics.pptx
Node_basics.pptxNode_basics.pptx
Node_basics.pptx
 
Navigation Bar.pptx
Navigation Bar.pptxNavigation Bar.pptx
Navigation Bar.pptx
 
Bootstarp installation.pptx
Bootstarp installation.pptxBootstarp installation.pptx
Bootstarp installation.pptx
 
nested_Object as Parameter & Recursion_Later_commamd.pptx
nested_Object as Parameter  & Recursion_Later_commamd.pptxnested_Object as Parameter  & Recursion_Later_commamd.pptx
nested_Object as Parameter & Recursion_Later_commamd.pptx
 
Chapter 3.pdf
Chapter 3.pdfChapter 3.pdf
Chapter 3.pdf
 
Introduction to Social Media and Social Networks.pdf
Introduction to Social Media and Social Networks.pdfIntroduction to Social Media and Social Networks.pdf
Introduction to Social Media and Social Networks.pdf
 
Dropdown Menu or Combo List.pdf
Dropdown Menu or Combo List.pdfDropdown Menu or Combo List.pdf
Dropdown Menu or Combo List.pdf
 
div tag.pdf
div tag.pdfdiv tag.pdf
div tag.pdf
 
Dimensions of elements.pdf
Dimensions of elements.pdfDimensions of elements.pdf
Dimensions of elements.pdf
 
CSS Positioning Elements.pdf
CSS Positioning Elements.pdfCSS Positioning Elements.pdf
CSS Positioning Elements.pdf
 
JavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdfJavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdf
 
Computer Networks: Quality of service
Computer Networks: Quality of serviceComputer Networks: Quality of service
Computer Networks: Quality of service
 
Transport layer protocols : Simple Protocol , Stop and Wait Protocol , Go-Bac...
Transport layer protocols : Simple Protocol , Stop and Wait Protocol , Go-Bac...Transport layer protocols : Simple Protocol , Stop and Wait Protocol , Go-Bac...
Transport layer protocols : Simple Protocol , Stop and Wait Protocol , Go-Bac...
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer services
 
Transport layer protocols : TCP and UDP
Transport layer protocols  : TCP and UDPTransport layer protocols  : TCP and UDP
Transport layer protocols : TCP and UDP
 

Recently uploaded

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 

Random number generation_upload.pdf

  • 1. JavaScript Random number generation Dr.T.Abirami Associate Professor Department of IT Kongu Engineering College Perundurai Dr.T.Abirami/KEC
  • 2. Math.random() • This function is used to return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive). • This random number can then be scaled according to the desired range. Dr.T.Abirami/KEC
  • 3. <p id="demo"></p> <script> document.getElementById("demo").innerHTML = Math.random(); </script> Math.random() returns a random number between 0 (inclusive) and 1 (exclusive): Generating a random floating point number between 0 and 1 Dr.T.Abirami/KEC
  • 4. Generating a random floating point number between 0 and a specified max • var x = Math.random()*10; <html> <body> <h1>example</h1> <h1> <script> var x = Math.random()*10; document.write(x); </script> </h1> </body> </html> Dr.T.Abirami/KEC
  • 5. <html> <body> <h1> example</h1> <h1> <script> var x = Math.floor(Math.random()*10); document.write(x); </script> </h1> </body> </html> The Math.floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result. Dr.T.Abirami/KEC
  • 6. Generating a random floating point number within a range var min = 83.1; var max = 193.36; var x = Math.random()*(max - min)+min; console.log(x); // 126.94014012699063 Dr.T.Abirami/KEC
  • 7. Example 1: This example generate an integer random number between 1(min) and 6(max). <script> function randomNumber(min, max) { return Math.random() * (max - min) + min; } document.write("Random Number between 1 and 6: ") document.write( randomNumber(1, 6) ); </script> Dr.T.Abirami/KEC
  • 8. Displaying random images <html> <body> <h1>example</h1> <h1> <script> var images = [], index = 0; images[0] = "<img src='D:/images/1.jpg' alt='1'>"; images[1] = "<img src='D:/images/2.jpg' alt='2'>"; images[2] = "<img src='D:/images/3.jpg' alt='3'>"; images[3] = "<img src='D:/images/4.jpg' alt='3'>"; images[4] = "<img src='D:/images/5.jpg' alt='3'>"; index = Math.floor(Math.random() * images.length); document.write(images[index]); </script> </h1> </body> </html> Dr.T.Abirami/KEC