# [mythtv] MythMusic playlists still not intelligent enough IMHO

Dan Wilga mythtv-dev2 at dwilga-linux1.amherst.edu
Wed Apr 18 14:50:39 UTC 2007

```I'm really happy about the recent change which resumes the last
played song, and also the addition of intelligent playlists. But I
don't think they're intelligent enough, as I'm still getting far too
many duplicated songs. I have over 1000 songs in my playlist, and it
managed to choose four in a row that I had recently heard.

What I propose is a rewrite of the shuffling algorithm, to make it
one-time random. This can actually be achieved without using *any*
additional memory. Take a look at this pseudocode:

- assume songs are in an array
- assume N is the length of the array (the number of songs in the list)
- find a prime number, P, such that it is guaranteed to be larger
than N and yet P+N <= MAX_INT
- choose the index of the first song, S, randomly:  S = rand() % N
- afterward, each new song, S' is calculated like so:  S' = (S + P) % N

A mathematics guru friend of mine sent me the proof that this
sequence is guaranteed to visit each song exactly once, in a
pseudorandom order. It also has the advantage that the sequence is
easily resumed. All you have to do is store S and P when stopping
playback.

A list of big primes can be found here:
http://primes.utm.edu/lists/small/millions/
It actually doesn't matter what prime number is chosen, as long as
it's larger than N, so it could be chosen at compile time based on
the compiler's MAX_INT.
--
Dan Wilga                                                        "Ook."
```