Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 319 characters in body
Source Link
Lior Elrom
  • 21.1k
  • 16
  • 83
  • 94

Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

// 0 -> 10
const rand1 = Math.floor(Math.random() * 11);
// 1 -> 10
const rand2 = Math.floor(Math.random() * 10) + 1;
// 5 -> 20
const rand3 = Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
const rand4 = Math.floor(Math.random() * 9) - 10;
console.log(rand1);
console.log(rand2);
console.log(rand3);
console.log(rand4);

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
const rand1 = Math.floor(Math.random() * 11);
// 1 -> 10
const rand2 = Math.floor(Math.random() * 10) + 1;
// 5 -> 20
const rand3 = Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
const rand4 = Math.floor(Math.random() * 9) - 10;
console.log(rand1);
console.log(rand2);
console.log(rand3);
console.log(rand4);

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

Commonmark migration
Source Link

#Math.random()

Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

#Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

added 789 characters in body
Source Link
Lior Elrom
  • 21.1k
  • 16
  • 83
  • 94

#Math.random()

From theReturns an Mozilla Developer Network documentationinteger random number between min (included) and max (included):

// Returns a random integer betweenfunction randomInteger(min, (includemax) and{
 max (include)
return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

#Math.random()

From the Mozilla Developer Network documentation:

// Returns a random integer between min (include) and max (include)
Math.floor(Math.random() * (max - min + 1)) + min;

Useful examples:

// 0 - 10
Math.floor(Math.random() * 11);
// 1 - 10
Math.floor(Math.random() * 10) + 1;
// 5 - 20
Math.floor(Math.random() * 16) + 5;
// -10 - (-2)
Math.floor(Math.random() * 9) - 10;

#Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
 return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

** And always nice to be reminded (Mozilla):

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.

deleted 55 characters in body
Source Link
Lior Elrom
  • 21.1k
  • 16
  • 83
  • 94
Loading
edited body
Source Link
Lior Elrom
  • 21.1k
  • 16
  • 83
  • 94
Loading
added 62 characters in body
Source Link
Lior Elrom
  • 21.1k
  • 16
  • 83
  • 94
Loading
Source Link
Lior Elrom
  • 21.1k
  • 16
  • 83
  • 94
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /