EN
JavaScript - random int value
8
points
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
to2147483647
).
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