From 6f75d1739fa6e1f76753a62e4417761df812c072 Mon Sep 17 00:00:00 2001 From: Linus Nielsen Feltzing Date: Mon, 22 Jul 2002 15:36:39 +0000 Subject: Slightly improved shuffle algorithm. Patch by Hardeep Sidhu git-svn-id: svn://svn.rockbox.org/rockbox/trunk@1397 a1c6a512-1295-4272-9138-f99709370657 --- apps/playlist.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) (limited to 'apps/playlist.c') diff --git a/apps/playlist.c b/apps/playlist.c index cac59bfd64..142f70e863 100644 --- a/apps/playlist.c +++ b/apps/playlist.c @@ -210,7 +210,7 @@ void add_indices_to_playlist( playlist_info_t *playlist ) */ void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) { - int count = 0; + int count; int candidate; int store; @@ -218,19 +218,15 @@ void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) srand( seed ); /* randomise entire indices list */ - - while( count < playlist->amount ) + for(count = playlist->amount - 1; count ; count--) { /* the rand is from 0 to RAND_MAX, so adjust to our value range */ - candidate = rand() % playlist->amount; + candidate = rand() % (count + 1); /* now swap the values at the 'count' and 'candidate' positions */ store = playlist->indices[candidate]; playlist->indices[candidate] = playlist->indices[count]; playlist->indices[count] = store; - - /* move along */ - count++; } } @@ -239,12 +235,3 @@ void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) * eval: (load-file "../firmware/rockbox-mode.el") * end: */ - - - - - - - - - -- cgit v1.2.3