0

to explain what I mean, lets start with a list of items each with their own score

[2,3,4]

adding these up these gives 9, so I want to be able to select a random number in the total range, where this number would represent one of these items

item 1, score of 2, range 0-1

item 2, score of 3, range 2-4

item 3, score of 4, range 5-8

The list would not be sorted and the "scores" could be any value

I could loop through the whole list and check if the number is say 2-4 and so on through the list, or do a binary search on initially the lower range... but I think there should be some way I can just take the random number a calculate which item it represents...

Chris Camacho
  • 1,164
  • 1
  • 9
  • 31
  • 1
    Didn't get the question...... how you are calculating range ?? – Rajeev Singh Apr 25 '17 at 14:26
  • @RajeevSingh, apparently, each element is a relative probability that this element gets selected. – hidefromkgb Apr 25 '17 at 14:34
  • 1
    [Data structure for loaded dice?](http://stackoverflow.com/questions/5027757/data-structure-for-loaded-dice) [Weighted random selection with and without replacement.](http://stackoverflow.com/questions/352670/weighted-random-selection-with-and-without-replacement) – Bernhard Barker Apr 25 '17 at 14:39
  • Possible duplicate of [Weighted random selection with and without replacement](http://stackoverflow.com/questions/352670/weighted-random-selection-with-and-without-replacement) – Paul Hankin Apr 25 '17 at 14:57

0 Answers0