jeudi 18 novembre 2021

How to pick an element from an array with a probability distribution?

I have a mathematical/programming question about a problem I am trying to solve. Given this simple array of integers:

int[] goals = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Do you know how could I implement a function to pick an element from the array given a distributed probability like the one from this example?:

  • The probability of picking goals[0] is 50%
  • The probability of picking goals[1] is 30%
  • The probability of picking goals[2] is 12%
  • The probability of picking goals[3] is 2.5%
  • The probability of picking goals[4] is 0.85%
  • etcetera

The probability distribution is of my choice, hardcoded to keep things simple.

Thank you very much for your inputs!




Aucun commentaire:

Enregistrer un commentaire