How to simulate a result based on a given probability (e.g.: 8%) in javascript

Asked

Viewed 25 times

0

I am mounting a result simulator of football game from some variables.

The logic is as follows: Every minute both teams have a chance to score a goal. This probability is calculated using some variables such as player’s note, tactics used, among others.

Assuming that Team A has an 8% probability of making a goal every minute, how can I calculate this probability and return values 1 for goal or 0 for no goal?

Something like that:

//VARIÁVEIS
var gol = 0;
var probabilidade = 0.8;
var tempo = 0;
var fimdejogo = 45;

//FUNÇÃO DE TEMPO E GOL
function TempoDeJogo() {
  while (tempo < fimdejogo) {
    tempo++;
    //FUNÇÃO DA PROBABILIDADE
    if (retorno_da_probabilidade == 1) {
      gol++;
      console.log(tempo, gol);
    }
  }
  console.log("Fim de Jogo");
}
TempoDeJogo();

  • if (Math.random() <= probabilidade) ... OBS: 0.8 is 80%, not 8%.

  • Show!!! , exactly what I needed, and thanks for the Obs, the 80% had not touched me...

No answers

Browser other questions tagged

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