summaryrefslogtreecommitdiff
path: root/src
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
parent3cf2601f5d652037b79ca0cdaee051e4d9679c74 (diff)
Initialized the libc random generator using the time and the process ID.
Diffstat (limited to 'src')
-rw-r--r--src/common/shuffle.c17
-rw-r--r--src/core/core.c7
2 files changed, 21 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);
+ }
}
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");