Commit f0fd0413 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

gib_list_randomize: use code better suited for short lists (closes #151)

Note that the i == r check was never reached in the original code but may be
in the new variant. Since a shuffled list may well have one or two elements
retain their absolute position once in a while, this should not be a problem.

tests with 2- and 3-element lists show an even distribution amongst all
elements.
parent 68037c4d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ git HEAD
      three projects (one of which is feh). There is at least one known bug
      in it, and as I do not have the time to take over giblib development,
      importing the library seems to be the best solution.
    * Fix/improve --randomize for short filelists (closes #151)

Sun, 27 Apr 2014 20:28:02 +0200  Daniel Friesel <derf+feh@finalrewind.org>

+4 −6
Original line number Diff line number Diff line
@@ -362,12 +362,10 @@ gib_list_randomize(gib_list * list)
   srand(getpid() * time(NULL) % ((unsigned int) -1));
   for (i = 0; i < len - 1; i++)
   {
      r = (int) ((len - i - 1) * ((float) rand()) / (RAND_MAX + 1.0)) + i + 1;
      if (i == r)
         abort();
      t = farray[i];
      farray[i] = farray[r];
      farray[r] = t;
      r = i + rand() / (RAND_MAX / (len - i) + 1 );
      t = farray[r];
      farray[r] = farray[i];
      farray[i] = t;
   }
   list = farray[0];
   list->prev = NULL;