Simply generate a number from 1 to 111 and then multiply by 5, so you have a result between 1 * 5 and 111 * 5. I think this is the easiest and clearest solution to understand, maybe even faster.
An easy, relatively safe way to do this would be to grab the 7 least significant bits of a byte and discard them if it is greater than 111 or less than 1.
function NumeroAleatorio() {
while (true) {
// Obtemos o numero aleátorio entre 0 até 255
var numero = new Uint8Array(1);
window.crypto.getRandomValues(numero);
// Obtemos apenas os 7 primeiros bits, fazendo ser de 0 até 127.
numero = numero[0] & 0x7F;
// Se for válido, retornamos ele multiplicado por 5.
if (numero >= 1 && numero <= 111) {
return numero * 5;
}
}
}
document.write("Gerado: " + NumeroAleatorio())
This uses window.crypto.getRandomValues
which is safer, but slower than Math.Random
. In the comments mentioned it would be for a game, depending on the case Math.Random
is better, for being faster.
There is a 14% chance, if my accounts are right, of the value being out of the desired (from 1 to 111), if the user is very unlucky the page can stay in an infinite loop if all the attempts are within the 14%.