javascript generate a random integer number in a range of numbers
const min = 1;
const max = 4;
const intNumber = Math.floor(Math.random() * (max - min)) + min;
console.log(intNumber); //> 1, 2, 3
javascript generate a random integer number in a range of numbers
const min = 1;
const max = 4;
const intNumber = Math.floor(Math.random() * (max - min)) + min;
console.log(intNumber); //> 1, 2, 3
random number in range js
var min = 10, max = 25;
//inclusive random (can output 25)
var random = Math.round(Math.random() * (max - min) + min);
//exclusive random (max number that can be output is 24, in this case)
var random = Math.floor(Math.random() * (max - min) + min);
//floor takes the number beneath the generated random and round takes
//which ever is the closest to the decimal
Generate a number range js
const range = (options) => {
const { from = 0, step = 1, to } = options;
if (!to) {
throw Error('"to" must be specified');
}
if (to <= from) {
throw Error(`"to (${to})" is lesser than or equal to "from (${from})"`);
}
return Array.from(
{ length: Math.ceil((to - from) / step) },
(_, i) => i * step + from
);
};
// Usage
const r1 = range({ to: 10 });
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const r2 = range({ from: 10, to: 20 });
// [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
const r3 = range({ from: 10, to: 20, step: 3 });
// [10, 13, 16, 19]
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us