vendredi 17 août 2018

Selecting several random items from an array when some items should be selected more often than the others

Suppose I have an array of objects, where each object has a value and a priority between 0 and 1. For example:

const arr = [
  { value: 'test', priority: 0.15 },
  { value: 'test2', priority: 0.70 },
  { value: 'test3', priority: 0.01 },
];

I want to select a random item from this array, but making sure that there is a very small chance to select test3 and a very high chance to select test2.

How can I do that?




Aucun commentaire:

Enregistrer un commentaire