-1

How to generate random numbers 0 to 26 using crypto.getRandomValues(). help

var array = new Uint32Array(1);
window.crypto.getRandomValues(array);

console.log(Math.floor(array[0] * 5));
marcus
  • 43
  • 6

1 Answers1

0

As suggested, personally you can use Math.random() which is made for those things:

let number = Math.random() * 26

But math-wise, you can just get a number between 0 and N using the module class N+1, so like:

let number = yourFunctionThatUsesCrypto() % 27 // [0,26]
let number = yourFunctionThatUsesCrypto() % 26 // [0,26)

Otherwise, as suggested from @Mr. Polywhirl you can scale the number between 0,1 (normalize) and then multiply it by the limit you want (in this case, 26)

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48