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:
xxxxxxxxxx
1
const value = Math.floor(256 * Math.random()) - 128;
xxxxxxxxxx
1
const randomByte = () => Math.floor(256 * Math.random() - 128);
2
3
4
// Usage example:
5
6
console.log(randomByte()); // 106
7
console.log(randomByte()); // 7
8
console.log(randomByte()); // -82