1
0
Fork 0
forked from len0rd/rockbox

Slightly improved shuffle algorithm. Patch by Hardeep Sidhu

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@1397 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Linus Nielsen Feltzing 2002-07-22 15:36:39 +00:00
parent 703704d4f6
commit 6f75d1739f

View file

@ -210,7 +210,7 @@ void add_indices_to_playlist( playlist_info_t *playlist )
*/ */
void randomise_playlist( playlist_info_t *playlist, unsigned int seed ) void randomise_playlist( playlist_info_t *playlist, unsigned int seed )
{ {
int count = 0; int count;
int candidate; int candidate;
int store; int store;
@ -218,19 +218,15 @@ void randomise_playlist( playlist_info_t *playlist, unsigned int seed )
srand( seed ); srand( seed );
/* randomise entire indices list */ /* randomise entire indices list */
for(count = playlist->amount - 1; count ; count--)
while( count < playlist->amount )
{ {
/* the rand is from 0 to RAND_MAX, so adjust to our value range */ /* 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 */ /* now swap the values at the 'count' and 'candidate' positions */
store = playlist->indices[candidate]; store = playlist->indices[candidate];
playlist->indices[candidate] = playlist->indices[count]; playlist->indices[candidate] = playlist->indices[count];
playlist->indices[count] = store; 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") * eval: (load-file "../firmware/rockbox-mode.el")
* end: * end:
*/ */