EN
JavaScript - random short value
7
points
In this short article, we would like to show to randomize short value using JavaScript.
Note: as short value we understand signed 16 bits integer value (from
-32768
to32767
).
Quick solution:
const value = Math.floor(65535 * Math.random()) - 32768;
Reusable function
// ONLINE-RUNNER:browser;
const randomShort = () => Math.floor(65535 * Math.random()) - 32768;
// Usage example:
console.log(randomShort()); // -3870
console.log(randomShort()); // 17488
console.log(randomShort()); // -28905