H
H
hiwibu2016-05-28 21:40:57
JavaScript
hiwibu, 2016-05-28 21:40:57

How to implement frequency of occurrence algorithm?

I have an array with 100 objects. At the moment, a random object is displayed at each display.
Now we need to show certain elements more often based on the priority field.
For example, if the priority field = 2, then the element is shown 2 times more often. If 3, then 3.
Who has any thoughts on how this can be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2016-05-28
@hiwibu

Well, the usual distribution of weights. Each object has a weight. Sum up all the weights, throw a random within this amount. Next, start walking through the array, and sum up the weights. As soon as the total weight becomes more than the one given by random - here it is your element.
Example.
Three objects, with weights 2, 1, 1. You randomly throw up to 4. For example, 3 falls out. Start summing - first 2 - skip the first element. Then we add 1 - and this is equal to what was given randomly. We return the second element.
The main thing is not to confuse with threshold values.

A
Andrew, 2016-07-22
@iCoderXXI

I would solve this problem like this - I would assign a counter to each object, which is incremented when the object is shown.
Those. 100 objects - 100 counters.
Further, I would divide each counter by the priority of its object and add the values ​​​​into an array, for each element I would also add the pointer to the object.
Then I would sort this array in ascending order, and show the object by the very first link from the sorted array (while incrementing its individual counter).
And so every time. Thus, we get the display of the least shown object, taking into account priorities with minimal effort and with a fairly transparent logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question