Manipulating chances with JS when using Math.random ()?

3

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% Odd or Even chance, but what if I wanted to manipulate those chances, so that the function is more likely to give me an Odd number, for example 70% chance of getting an Odd number and 30% chance of getting a Odd number.

  • Is it possible to do this?
  • What would be the logic applied and how can I do it?
  • asked by anonymous 04.04.2017 / 20:26

    4 answers

    1

    The algorithm below is made up of two parts:

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

    Adding an odd value to an even value will always generate an odd value. Any other addition case (Par + Par, 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();
        
    04.04.2017 / 21:46
    0

    The name itself already says ramdom() , that is, it returns a random number. If you want to change the chances of the returned number to be more even or 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
    }
    

    But since it's a random return, only with if's can you manipulate the result.

    Another way is for you 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 odd.

        
    04.04.2017 / 21:35
    0

    It would be more interesting to tell you which problem exactly you want to solve. But according to your example, you can first make a raffle to determine if the number you want is even or odd. Then, back home 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 drawing successively until it hits what you need to generate.

        
    04.04.2017 / 21:39
    -2

    I do not quite understand, 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 
    }
    
        
    04.04.2017 / 21:25