From 95ef32f1a667af567b9d3173fe456154e522dfde Mon Sep 17 00:00:00 2001
From: Vito Caputo <vcaputo@pengaru.com>
Date: Thu, 14 Apr 2022 16:51:54 -0700
Subject: til: seed srand in til_init()

A future commit will remove srand() calls from modules, relying
instead on this srand() in til_init().

As mentioned in the comment, if modules actually want
reproducible deterministic pseudo-random values they should use
rand_r() (or their own PRNG) where they can control the seed.
---
 src/til.c | 9 +++++++++
 1 file changed, 9 insertions(+)

(limited to 'src')

diff --git a/src/til.c b/src/til.c
index 3cbd294..651a23a 100644
--- a/src/til.c
+++ b/src/til.c
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <sys/time.h>
+#include <time.h>
 #include <unistd.h>
 
 #include "til.h"
@@ -68,6 +69,14 @@ static const til_module_t	*modules[] = {
 /* initialize rototiller (create rendering threads) */
 int til_init(void)
 {
+	/* Various modules seed srand(), just do it here so they don't need to.
+	 * At some point in the future this might become undesirable, if reproducible
+	 * pseudo-randomized output is actually desirable.  But that should probably be
+	 * achieved using rand_r() anyways, since modules can't prevent others from playing
+	 * with srand().
+	 */
+	srand(time(NULL) + getpid());
+
 	if (!(til_threads = til_threads_create()))
 		return -errno;
 
-- 
cgit v1.2.3