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:
xxxxxxxxxx
1
const value = Math.floor(65535 * Math.random()) - 32768;
xxxxxxxxxx
1
const randomShort = () => Math.floor(65535 * Math.random()) - 32768;
2
3
4
// Usage example:
5
6
console.log(randomShort()); // -3870
7
console.log(randomShort()); // 17488
8
console.log(randomShort()); // -28905