samedi 29 août 2020

Choice Random with different probability [duplicate]

I have groups of fruits.

Group 1 - Apples 5%, Pears - 5%, oranges - 10%, passion fruit - 8%

Group 2 - Apples 1%, Pears - 80%, oranges - 19%

Group 3 - Apples 0.01%, Pears 0.01%, oranges 0.01%, bananas 0.03%

Group 4 - twigs 0.01%, pineapple - 0.01%, bananas - 0.03%

I can't figure out how to make a function of the probability of pulling or not pulling a fruit, and if you pull it out, then the probability "which fruit". It is need applicable for different group.

The method below does not solve my problem, because I have different probabilities for each group, and the sum of the probabilities is always different.

public int getRandomInt () {
  double x = new Random (). nextDouble ();
  if (x <20) return 3;
  else if (x> = 20 && x <50) return 2;
  else return 1;
}



Aucun commentaire:

Enregistrer un commentaire