summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2016-10-21 20:57:58 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2016-10-21 20:57:58 (GMT)
commitcff2f506464abf93c35feb6d644ad08b79feb856 (patch)
tree342c686ff7dcc4a5ed12f8af8e4afa5483a8609f /src/common
parent3cf2601f5d652037b79ca0cdaee051e4d9679c74 (diff)
Initialized the libc random generator using the time and the process ID.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/shuffle.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/common/shuffle.c b/src/common/shuffle.c
index 6b20866..e2e0a3d 100644
--- a/src/common/shuffle.c
+++ b/src/common/shuffle.c
@@ -53,6 +53,14 @@ void shuffle(void *base, size_t nmemb, size_t size)
* Application de l'algorithme Fisher-Yates.
*
* Cf. https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
+ *
+ * En version hors-ligne, cela donne :
+ *
+ * -- To shuffle an array a of n elements (indices 0..n-1):
+ * for i from 0 to nāˆ’2 do
+ * j ā† random integer such that i ā‰¤ j < n
+ * exchange a[i] and a[j]
+ *
*/
if (nmemb > 1)
@@ -65,9 +73,12 @@ void shuffle(void *base, size_t nmemb, size_t size)
{
j = i + rand() % (nmemb - i);
- memcpy(tmp, list + i * size, size);
- memcpy(list + i * size, list + j * size, size);
- memcpy(list + j * size, tmp, size);
+ if (j != i)
+ {
+ memcpy(tmp, list + i * size, size);
+ memcpy(list + i * size, list + j * size, size);
+ memcpy(list + j * size, tmp, size);
+ }
}