Q:

Slideshow Algorithm

I need to design an algorithm for a photo slideshow that is constantly receiving new images, so that the oldest pictures appear less in the presentation, until a balance between the old photos and those that have appeared.
I have thought that every image could have a counter of the number of times they have been shown and prioritize those pictures with the lowest value in that variable.
Any other ideas or solutions would be well received.

A:

You can achieve an overall near-uniform distribution (each image appears about the same number of times for the long run), but I wouldn't recommend doing it. Images that were available early would appear very very rarely later on. A better user experience would be to simply choose a random image from all the available images at each step.
If you still want near-uniform distribution for the long run, you should set the probability for any image based on the number of times it appeared so far. For example:
p(i) = 1 - count(i) / (max_count() + epsilon)
Here is a simple R code that simulates such process. 37 random images are selected before a new image becomes available. This process is repeated 3000 times:
h    <- 3000              # total images
eps  <- 0.001

t    <- integer(length=h) # t[i]: no. of instances of value i in r
r    <- c()               # proceded vector of indexes of images 
m    <- 0                 # highest number of appearances for an image

for (i in 1:h)
    for (j in 1:37)                                   # select 37 random images in range 1..i
    {
        v    <- sample(1:i, 1, prob=1-t[1:i]/(m+eps)) # select image i with weight 1-t[i]/(m+eps)
        r    <- c(r, v)                               # add to output vector

        t[v] <- t[v]+1                                # update appearances count
        m    <- max(m, t[v])                          # update highest number of appearances
    }

plot(table(r))

The output plot shows the number of times each image appeared:
epsilon = 0.001:

epsilon = 0.0001:

If we look, for example at the indexes in the output vector in which, say, image #3 was selected:
> which(r==3)
 [1]    75    76    77    78    79    80    81    82    83    84    85    86    87    88    89    90    91    92    93    94
[21]    95    96    97    98    99   100   101   102   103   104   105   106   107   108   109   110   111  1189 34767 39377
[41] 70259

Note that if epsilon is very small, the sequence will seem less random (newer images are much preferred). For the long run however, any epsilon will do.

