Manipulating JS odds when using Math.Random()?

Asked

Viewed 874 times

4

When using this function Math.floor((Math.random() * 10) + 1) I get a random number from 1 to 10. We assume that each number has a 10% chance so we have a 50% chance for Odd or Even, but if I wanted to manipulate those odds so that the function would be more likely to give me an Odd number, for example 70% chance of exiting an Odd number and 30% chance of exiting an Even number.

  1. It is possible to do this?
  2. What would be the applied logic and how can I do?

4 answers

2

The algorithm below is composed of two parts:

  • numTipo is generated: its value shall be 0 if the return of a random generation from 1 to 10 is greater than 8, or 1 negative case; and
  • numFinal simply contains a value between 2 and 100 which will always be par, where numTipo is subsequently added.

Adding an odd value to an even value will always generate an odd value. Any other addition case (Even + Even, Odd + Odd) generates an even number.

This algorithm forces the generation of values whose average distribution is 70% odd and 30% even.

function geraNum() {

  var numTipo = Math.floor((Math.random() * 10) + 1) > 7 ? 0 : 1;
  var numFinal = Math.floor((Math.random() * 50) + 1) *2 + numTipo;

  return numFinal;
}

function geraSerie() {

    var contaPar = 0;
    var contaImpar = 0;

    for (i = 0; i < 1000; i++) { 
        
        if (geraNum() % 2 == 0) {
            contaPar++;
        } else {
            contaImpar++;
        }
    }
    
    console.log("Pares: " + contaPar + ", Ímpares: " + contaImpar);    
}

geraSerie();
geraSerie();
geraSerie();
geraSerie();
geraSerie();

0

His name already says ramdom(), that is, it returns a random number. If you want to change the chances of the returned number being more even or being more odd, you can create a simple function to check the return of the code:

let myNumber = Math.floor((Math.random() * 10) + 1);
if (myNumber % 2 == 0) { 
  // par = faça alguma coisa
} else {
  // ímpar = faça alguma coisa
}

However, as it is a random return, only with the if's you can manipulate the result.

Another way is to create your own function (Ex: Math.myRandomFunction()) and in it you create your own logic, storing information and forcing the return to be more even or to be more odd.

0

It would be more interesting for you to say which problem exactly you want to solve. But, according to your example, you can first make a draw to determine whether the number you want is even or odd. Then, for the case, call a function that generates a number only in the pattern you want. For example:

if (x > 7) {
    return gerarNumeroPar();
} else {
    return gerarNumeroImpar();
} 

This function can be done by selecting from a previously generated list or even by drawing successively until you hit what you need to generate.

  • What is described in my role, is exactly the problem I want to solve ... I want to generate a random number between 1 and 10, but I want this number to have more chances of being Odd.

  • So my answer solves your problem. You understand it?

  • "x" is a random it being greater than 7 (70%) i Gero a pair, being smaller would be the 30% being an Odd, correct?

  • That’s it. It’s two draws. The first is this X to determine whether it’s going to be odd or even. Once this is determined, it calls the function that draws in a loop until the one that matches what was chosen comes out. I’m on the phone, so I did not put more details. Later edit to be more complete, but the idea is this.

  • I understood the applied logic, would this solution be a "gambiarra" or there is no more specific function for chance manipulation?

-3

I didn’t understand it very well, but if the goal is to be more conducive to a result, you can do it this way:

if(valor > 7){
    // 30% de chance de cair aqui 
}else{
    // 70% de chance de cair aqui 
}

Browser other questions tagged

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