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

98
Visualizações
Choosing random value from weighted options in java

When the user clicks, a random output from a set of outputs must be selected. The chances for one value needs to be higher than the other. For example, you click a button and you either receive Bronze, Silver, Gold, or Platinum. Obviously, if you click randomly, you should receive Bronze more than Silver, more than Gold, more than Platinum. For example, could it maybe look like this:

printRandom("Bronze", 30, "Silver", 20, "Gold", 10, "Platinum", 5, "You didn't get anything.");

Where the final result is when you were unlucky enough to get none. I've been having trouble with making one option more common than the other, not choosing a value in the first place. Thank you.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you want to keep it simple, you could do the following:

int random = Math.random();

if (random < 0.05) { // 5/100
  return "Platinum";
} else if (random < 0.1) { // 10/100
  return "Gold";
} else if (random < 0.2) { // 20/100
  return "Silver";
} else if (random < 0.3) { // 30/100
  return "Bronze";
} else {
  return "You didn't get anything.";
} 
over 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