I am writing a program that tells the user which juice taste to buy. I store the avaible juices in an array and the previously bought in another array. Based on how many times a taste has been bought and the time since it was last bought, each taste gets a score. The higher score the more likely it should be for that taste to be selected.
When I arrive at the step to randomly select a juice taste (that is, selecting an index in the array) I have two instances of NSMutableArray
, one for the avaible tastes, one for the scores. Both arrays have 17 slots.
It might looks something like this: (only three different tastes)
@[@"Apple juice", @"Orange juice", @"Grape juice"]; // Avaible tastes array
@[19.21, 45.83, 24.84]; // Scores array
How do I select one taste randomly from this?
Initially, I thought that each taste could have a range, starting from the previous taste's score going to the previous score + the taste's own score. Then a random number between 0 and the total score would be generated. The taste who's range matches the random number would be the one that was going to be selected, but this feels like a overly complex solution.
Is there an easier way?
Edit
By the way, it is written in Objective-C.