diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2016-10-21 20:57:58 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2016-10-21 20:57:58 (GMT) |
commit | cff2f506464abf93c35feb6d644ad08b79feb856 (patch) | |
tree | 342c686ff7dcc4a5ed12f8af8e4afa5483a8609f | |
parent | 3cf2601f5d652037b79ca0cdaee051e4d9679c74 (diff) |
Initialized the libc random generator using the time and the process ID.
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | src/common/shuffle.c | 17 | ||||
-rw-r--r-- | src/core/core.c | 7 |
3 files changed, 29 insertions, 3 deletions
@@ -1,5 +1,13 @@ 16-10-21 Cyrille Bagard <nocbos@gmail.com> + * src/common/shuffle.c: + Avoid to copy overlapping memory areas. + + * src/core/core.c: + Initialize the libc random generator using the time and the process ID. + +16-10-21 Cyrille Bagard <nocbos@gmail.com> + * src/gtkext/graph/cluster.c: * src/gtkext/graph/edge.c: * src/gtkext/graph/edge.h: 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); + } } diff --git a/src/core/core.c b/src/core/core.c index ec7b0fc..b949e0a 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -24,6 +24,11 @@ #include "core.h" +#include <stdlib.h> +#include <time.h> +#include <unistd.h> + + #include <config.h> @@ -62,6 +67,8 @@ bool load_all_basic_components(void) { result = true; + srand(time(NULL) + getpid()); + add_pixmap_directory(PACKAGE_DATA_DIR); add_pixmap_directory(PACKAGE_SOURCE_DIR G_DIR_SEPARATOR_S "pixmaps"); |