-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
Math.random()
– Generating Random NumbersThe Math.random()
method returns a pseudo-random floating-point number in the range:
0 ≤ Math.random() < 1
📌 It never returns exactly 1
, only up to 0.999...
.
Math.random();
Returns a floating-point number between 0 (inclusive) and 1 (exclusive).
Task | Example |
---|---|
Random decimal 0–1 | Math.random() |
Random decimal 0–10 | Math.random() * 10 |
Random integer 0–9 | Math.floor(Math.random() * 10) |
Random integer 1–10 | Math.floor(Math.random() * 10) + 1 |
Random integer between min & max |
Math.floor(Math.random() * (max - min + 1)) + min |
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(1, 100)); // Random int between 1 and 100
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}
console.log(getRandomFloat(5.5, 10.5)); // e.g., 7.2341
Math.random()
returns a decimal, not an integer.
Always use Math.floor()
or Math.round()
to get whole numbers.
Always add +min
to shift the range correctly.
Q1. How do you generate a random floating-point number between 0 and 1 using Math?
Q2. How do you create a random integer between 0 and 9 using Math.random()
and Math.floor()
?
Q3. How do you generate a random number between 1 and 100 using a custom function?
Q4. What is the purpose of Math.floor()
in the expression Math.floor(Math.random() * 10)
?
Q5. How do you generate a random floating-point number between 5.5 and 10.5?
Q6. Write a function that returns a random number between any two given integers, inclusive.
Q7. What happens if you forget to add +min
when generating a random number in a custom range?
Q8. How do you create a random number generator for a dice roll (1 to 6)?
Q9. How do you ensure that the result of Math.random()
never equals 1?
Q10. How can you generate a random number that simulates a coin toss (Head or Tail)?
Q1: What is the range of values returned by Math.random()?
Q2: What is the output type of Math.random()?
Q3: Which expression gives a random integer between 1 and 10 (inclusive)?
Q4: What does Math.floor() do in a random number formula?
Q5: What would Math.floor(Math.random() * 100) + 1 return?
Q6: How many values are possible in Math.floor(Math.random() * 6) + 1?
Q7: Which method generates a decimal between two float values?
Q8: What is a correct way to simulate a coin toss using Math.random()?
Q9: Which one is not a valid usage of Math.random()?
Q10: What will Math.floor(Math.random() * 1) always return?