How to generate random numbers in javascript, differently

Asked

Viewed 5,180 times

5

I need to generate random in javascript, so that they are only generated 5 out of 5 and that it is within a limitation, for example 5 to 555. Example: 5, 355, 425, 125, 550

  • Multiples of 5 (5,10,15,20,25,30,35,40...)
  • The numbers must have a minimum of 5 and a maximum of 555!
  • 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?

  • I edited the question! I didn’t try anything, I’m a beginner in javascript ;-; I need it for a game Html5.

3 answers

2

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%.

2


Simply:

Math.round(Math.random() * 550 / 5) * 5 + 5;

If you want something with variable values or can be easily called, you can create a function:

function myRandom(min, max, multiple) {
    return Math.round(Math.random() * (max - min) / multiple) * multiple + min;
}

0

Try something like this:

function obterNumeroAleatorio(n1, n2) {
  const min = Math.ceil(n1);
  const max = Math.floor(n2);
  return Math.floor(Math.random() * (max - min)) + min;
}

function modificarNumeroParaSerMultiploDeCinco(n) {
  const numeroDivisivelPorCinco = Math.round(n / 5) * 5;
  return numeroDivisivelPorCinco;
}

const numeroAleatorio = obterNumeroAleatorio(5, 555);
const numeroFinal = modificarNumeroParaSerMultiploDeCinco(numeroAleatorio);

console.log('numeroAleatorio:', numeroAleatorio);
console.log('numeroFinal:', numeroFinal);

In the first constant, the number is generated randomly, and in the second it is done with a multiple of 5. What you probably need is just the numeroFinal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.