Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
Value probability based on percentage

Is there a way to extract a value based on a percentage?

Value probability:
Bad: 1%
Normal: 29%
Good: 70%

var move ["bad","normal","good"];

A simple conditional statement:

if (move == "bad") {
bad_move = "That's a bad move!";
} else if (move == "normal") {
normal_move = "Classic move!";
} else {
good_move = "Amazing move!";
}

Then, is PHP better than Javascript for this kind of problem?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You could write a function that samples values with given probability percentages:

function weightedSample(pairs) {
  const n = Math.random() * 100;
  const match = pairs.find(({value, probability}) => n <= probability);
  return match ? match.value : last(pairs).value;
}

function last(array) {
  return array[array.length - 1];
}

const result = weightedSample([
  {value: 'Bad', probability: 1},
  {value: 'Normal', probability: 29},
  {value: 'Good', probability: 70}
]);

console.log(result);

I can't say if PHP would be better. This kind of problem shouldn't be any more/less difficult in PHP. Which you should use (JS or PHP) really depends on whether or not the function should be run on the server or client.

about 4 years ago · Santiago Trujillo Relatório

0

I suggest to use a continuous check of the probability and the rest of the random number.

This function sets first the return value to the last possible index and iterates until the rest of the random value is smaller than the actual probability.

The probabilities have to sum to one.

function getRandomIndexByProbability(probabilities) {
    var r = Math.random(),
        index = probabilities.length - 1;

    probabilities.some(function (probability, i) {
        if (r < probability) {
            index = i;
            return true;
        }
        r -= probability;
    });
    return index;
}

var i,
    move = ["bad", "normal", "good"],
    probabilities = [0.01, 0.29, 0.7],
    count = {},
    index;

move.forEach(function (a) { count[a] = 0; });

for (i = 0; i < 1e6; i++) {
    index = getRandomIndexByProbability(probabilities);
    count[move[index]]++;
}

console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda