When shuffling, tracks from albums with few songs are chosen disproportionally often
Environment
- Lollypop version: 1.1.99.2
- GTK+ version:3.24.8-1ubuntu1
- Operating system: Ubuntu 19.04
Bug
I have a few songs in my collection that are the only song in their album. I noticed when shuffling all of my music, that these songs would be played far more often than any other song in my collection. I took a look at the source code and I think I found two things in the __get_tracks_random()
function of player_shuffle.py
that are causing the issue:
def __get_tracks_random(self):
"""
Return a random track and make sure it has never been played
@return Track
"""
for album in sorted(self._albums, key=lambda *args: random.random()):
for track in sorted(album.tracks,
key=lambda *args: random.random()):
# Ignore current track, not an issue if playing one track
# in shuffle because LinearPlayer will handle next()
if track != App().player.current_track and (
album not in self.__already_played_tracks.keys() or
track not in self.__already_played_tracks[album]):
return track
if album in self.__already_played_tracks.keys():
self.__already_played_tracks.pop(album)
self.__already_played_albums.append(album)
return Track()
-
The algorithm first picks a random album then a random song. This means that a song in an album with few songs is more likely to be picked in the first place compared to if all songs were thrown into a single list and the algorithm picked from that.
-
When every song in an album has been played, they are removed from the
__already_played_tracks
dict and the album is added to__already_played_albums
, but when the algorithm chooses an album, it doesn't check that it is in the__already_played_albums
list. (Is this intentional? Or a bug?). This means that once an album is finished, all of its songs are effectively added again to the pool of songs to be picked. In the case of an album with only one song in it, that means the song will be immediately available to play again after is played. So it ends up being played much more often than the other songs.