Languages
[Edit]
EN

JavaScript - random int value

8 points
Created by:
FryerTuck
649

In this short article, we would like to show to randomize int value using JavaScript.

Note: as int value we understand signed 32 bits integer value (from -2147483648 to 2147483647).

Quick solution:

const value = Math.floor(4294967296 * Math.random()) - 2147483648;

 

Reusable function

// ONLINE-RUNNER:browser;

const randomInt = () => Math.floor(4294967296 * Math.random()) - 2147483648;


// Usage example:

console.log(randomInt());  //  -639505982
console.log(randomInt());  //  1018139768
console.log(randomInt());  // -1390047344

 

See also

  1. JavaScript - random boolean value

  2. JavaScript - random byte value

  3. JavaScript - random short value

  4. JavaScript - random long value

Alternative titles

  1. JavaScript - random Int32 value
  2. JavaScript - random 32 bits signed int value
  3. JavaScript - random <-2147483648, +2147483647> value
  4. JavaScript - random sint value
  5. JavaScript - random integer value
  6. JavaScript - random 32 bits signed integer value
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join