EN
JavaScript - random byte value
8
points
In this short article, we would like to show to randomize byte value using JavaScript.
Note: as byte value we understand signed 8 bits integer value (from
-128
to127
).
Quick solution:
const value = Math.floor(256 * Math.random()) - 128;
Reusable function
// ONLINE-RUNNER:browser;
const randomByte = () => Math.floor(256 * Math.random() - 128);
// Usage example:
console.log(randomByte()); // 106
console.log(randomByte()); // 7
console.log(randomByte()); // -82