Languages
[Edit]
EN

TypeScript - random bytes

0 points
Created by:
evangeline
420

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]
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join