[hackers] [sbase][PATCH] shuf: use libutil/random functions instead of custom ones

From: Elie Le Vaillant <eolien55_AT_disroot.org>
Date: Wed, 28 Feb 2024 01:27:49 +0100

---
 shuf.c | 37 +------------------------------------
 1 file changed, 1 insertion(+), 36 deletions(-)
diff --git a/shuf.c b/shuf.c
index 2655d08..3d8ec51 100644
--- a/shuf.c
+++ b/shuf.c
_AT_@ -7,41 +7,6 @@
 #include "text.h"
 #include "util.h"
 
-/*
- * Uniformity is achieved by generating new random numbers until the one
- * returned is outside the range [0, 2**32 % upper_bound).  This
- * guarantees the selected random number will be inside
- * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
- * after reduction modulo upper_bound.
- *
- * Copied off OpenBSD (original is arc4random_uniform)
- */
-uint32_t
-random_uniform(uint32_t upper_bound)
-{
-	uint32_t r, min;
-
-	if (upper_bound < 2)
-		return 0;
-
-	/* 2**32 % x == (2**32 - x) % x */
-	min = -upper_bound % upper_bound;
-
-	/*
-	 * This could theoretically loop forever but each retry has
-	 * p > 0.5 (worst case, usually far better) of selecting a
-	 * number inside the range we need, so it should rarely need
-	 * to re-roll.
-	 */
-	for (;;) {
-		r = random(); /* arc4random() is better, but we don't always have it */
-		if (r >= min)
-			break;
-	}
-
-	return r % upper_bound;
-}
-
 static void
 usage(void)
 {
_AT_@ -82,7 +47,7 @@ main(int argc, char *argv[])
 		}
 	}
 
-	srandom((intptr_t)buf.nlines | time(NULL));
+	random_seed();
 
 	if (!rflag) {
 		for (i = buf.nlines - 1; i > 0; i--) {
-- 
2.44.0
Received on Wed Feb 28 2024 - 01:27:49 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 28 2024 - 01:36:36 CET