Just generate a number from 1 to 111 and then multiply by 5, so you have a result between 1*5 to 111*5. I believe this is the easiest and clearest solution to understand, maybe even quickest.
An easy, and relatively safe, way to do this would be to take the 7 least significant bits of a byte and discard them if it is larger than 111 or smaller 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 the window.crypto.getRandomValues
which is safer, but slower than the Math.Random
. In the commentary mentioned that it would be for a game, depending on the case the Math.Random
be better, for being faster.
There is a possibility of 14%, if my accounts are right, the value is outside the desired (from 1 until 111), if the user is very unlucky the page can be in an infinite loop, if all attempts are within 14%.
Does generating 5 in 5 always mean generating 5 numbers? Was the fact that the 5 numbers you gave as an example were multiples of 5 mere coincidence? What have you tried to do? What was the difficulty found?
– Woss
I edited the question! I didn’t try anything, I’m a beginner in javascript ;-; I need it for a game Html5.
– Luan pedro