diff options
author | Daniel Friesel <derf@finalrewind.org> | 2014-05-05 21:22:12 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2014-05-05 21:23:55 +0200 |
commit | f0fd0413e2904e0824707d866a044341184bbe13 (patch) | |
tree | 50bef4a185e2ff13a55b4b8fd238181abf1badf6 /src | |
parent | 68037c4d220302a9f7ab250b3a220a5f64217244 (diff) |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/gib_list.c | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/src/gib_list.c b/src/gib_list.c index 1c313ba..a05fb4c 100644 --- a/src/gib_list.c +++ b/src/gib_list.c @@ -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; |