P
P
Pallid2015-02-25 09:21:39
Programming
Pallid, 2015-02-25 09:21:39

Algorithm for obtaining n number of records different from the one already received, how?

There is a table with records, let's say pictures are stored there, when you open the page, you need to get n pictures from it, when you click on the "more" button, get n more pictures that are different from those already received
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eddy_Em, 2015-02-25
@Eddy_Em

Option 1: for N objects, we create an array of N bytes, where each i corresponds to 0/1. 0 - if the object has already been used, 1 - if it has not been used yet. Then, using the RNG, we collect the required number of objects. Cons: the fewer objects, the longer we will extract them.
Option 2: Create a linked list. Accordingly, to select the i-th element, we will have to go through the i-1 previous ones. But there is no need to twitch when there are few elements left, because already used are retrieved from the list. Cons: having to iterate through the list to retrieve an object.
Option 3: linked list + array. Before the selection procedure, we iterate over the entire list and fill in the array of links. Next is the usual procedure for selecting from an array by index with the removal of the corresponding element from the list. Cons: each time before use, you need to rebuild the array.
Option 4: use wood. Cons: periodically have to rebalance it.
I recommend option 3: it seems to be the fastest of the proposed ones.
PS In the zhaboskriptike another option is possible: immediately drive the indexes of the elements into the array, and as soon as we use some index, remove it from the array. In fact, this is option 3 with slightly less memory consumption.

M
MiiNiPaa, 2015-02-25
@MiiNiPaa

Depending on the number of pictures:
1) If there are not many pictures, save links to these pictures/records in a table in a separate container. Then you simply select a random element and remove it from the container n times. Then n more times and so on
2) If there are many pictures
    a) Just choose randomly. The chances of getting already viewed pictures is low, the chance of the user noticing it is even lower
    b) Check if the picture has been shown and reselect if it has.
    c) Gather statistics on how many pages the user mainly views. (eg 5) Then split the set of all pictures into 5 non-overlapping subsets and generate each page only from the pictures of a specific subset. If the user does not calm down and has already looked through all the sets, generate completely randomly (with repetitions).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question