EN
TypeScript - random bytes
0
points
In this short article, we would like to show how using TypeScript, draw some random bytes array.
Practical example:
const randomBytes = (count: number): number[] => {
const result = Array(count);
for (let i = 0; i < count; ++i) {
result[i] = Math.floor(256 * Math.random());
}
return result;
};
// Usage example:
const a: number[] = randomBytes(5);
const b: number[] = randomBytes(10);
const c: number[] = randomBytes(15);
console.log(a); // [55, 114, 30, 83, 96]
console.log(b); // [42, 26, 254, 43, 30, 14, 179, 4, 214, 72]
console.log(c); // [238, 203, 230, 91, 223, 161, 59, 10, 105, 200, 45, 145, 64, 145, 210]
Example output:
[55, 114, 30, 83, 96]
[42, 26, 254, 43, 30, 14, 179, 4, 214, 72]
[238, 203, 230, 91, 223, 161, 59, 10, 105, 200, 45, 145, 64, 145, 210]