Hi dear list,

Here a patch proposal to have a wrapper around rand/random calls.

If it s ok with this one I ll send the rest.

Thanks.

Kind regards.
From 01b29c12410dba2797815aed1be602abc435902e Mon Sep 17 00:00:00 2001
From: David Carlier <devne...@gmail.com>
Date: Wed, 11 Apr 2018 17:20:49 +0100
Subject: [PATCH 1/5] BUILD/MEDIUM: standard: my_srand*/my_rand* functions.

BSD families systems have strong random number functions
generated with Chacha family algorithms. So we can use it
where it seems fit, other systems ought not be affected.
---
 include/common/standard.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/include/common/standard.h b/include/common/standard.h
index 6542759d9..2b1fa133b 100644
--- a/include/common/standard.h
+++ b/include/common/standard.h
@@ -839,6 +839,50 @@ static inline unsigned int my_ffsl(unsigned long a)
 	return cnt;
 }
 
+/* my_random call seeding */
+static inline void my_srandom(unsigned int seed)
+{
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+	(void)seed;
+#else
+	srandom(seed);
+#endif
+}
+
+/* Generates long random value */
+static inline long my_random(void)
+{
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+	long value;
+	arc4random_buf(&value, sizeof(value));
+	return value;
+#else
+	return random();
+#endif
+}
+
+/* my_rand call seeding */
+static inline void my_srand(unsigned int seed)
+{
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+	(void)seed;
+#else
+	srand(seed);
+#endif
+}
+
+/* Generates long random value */
+static inline int my_rand(void)
+{
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+	int value;
+	arc4random_buf(&value, sizeof(value));
+	return value;
+#else
+	return rand();
+#endif
+}
+
 /* Build a word with the <bits> lower bits set (reverse of my_popcountl) */
 static inline unsigned long nbits(int bits)
 {
-- 
2.16.2

Reply via email to