
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Generating a random number that is divisible by n in JavaScript
We are required to write a JavaScript function that takes in a number as the only argument. The function should then return a random generated number which is always divisible by the number provided by the argument.
Example
The code for this will be −
const num = 21; // function that generates random numbers divisible by n with a default upper limit of 1000000 const specialRandom = (num = 1, limit = 1000000) => { // getting a random number const random = Math.random() * limit; // rounding it off to be divisible by num const res = Math.round( random / num ) * num; return res; }; console.log(specialRandom(num));
Output
And the output in the console will be −
6006
This output is likely to differ on each run.
- Related Articles
- Smallest number that is divisible by first n numbers in JavaScript
- Generating Random Prime Number in JavaScript
- Generating n random numbers between a range - JavaScript
- Finding a pair that is divisible by some number in JavaScript
- Greatest number divisible by n within a bound in JavaScript
- Generating random number in a range in C
- Generating random number list in Python
- Generating random hex color in JavaScript
- A number is divisible by $12$. By what other numbers will that number be divisible?
- Sum which is divisible by n in JavaScript
- Generating random string with a specific length in JavaScript
- A number is divisible by both 5 and 12. By which other number will that number be always divisible?
- Generating random string of specified length in JavaScript
- Activity : Ask all the students in your class to write a 3-digit number. Choose any student from the room at random. What is the probability that the number written by her/him is divisible by 3 ? Remember that a number is divisible by 3 , if the sum of its digits is divisible by 3 .
- Smallest possible number divisible by all numbers from 1 to n in JavaScript

Advertisements