]> mj.ucw.cz Git - libucw.git/commitdiff
Random: Old pseudo-random implementation moved to random-legacy.c
authorPavel Charvat <pchar@ucw.cz>
Tue, 9 Jun 2020 09:27:08 +0000 (11:27 +0200)
committerPavel Charvat <pchar@ucw.cz>
Tue, 22 Mar 2022 18:06:59 +0000 (18:06 +0000)
ucw/Makefile
ucw/lib.h
ucw/random-legacy.c [new file with mode: 0644]
ucw/random.c [deleted file]

index dfdc9fbe49ef920175ab605660e2b56ceca15e7e..b9bff3921ff3b5789a4e2ca4f255416c02d1c247 100644 (file)
@@ -21,7 +21,8 @@ LIBUCW_MODS= \
        fw-hex \
        char-cat char-upper char-lower unicode varint stkstring \
        wildmatch regex \
-       prime primetable random \
+       prime primetable \
+       random-legacy \
        time-stamp time-timer time-conf \
        bit-ffs bit-fls bit-array \
        url \
index 0465ca1e4e4920fc441dd141bdafed25064e3ac1..6f93bb7e73b5470c86bd8a568ff43cd03fab8835 100644 (file)
--- a/ucw/lib.h
+++ b/ucw/lib.h
@@ -232,7 +232,7 @@ void *big_alloc(u64 len) LIKE_MALLOC;               /** Allocate a large memory block in the
 void *big_alloc_zero(u64 len) LIKE_MALLOC;     /** Allocate and clear a large memory block. **/
 void big_free(void *start, u64 len);           /** Free block allocated by @big_alloc() or @big_alloc_zero(). **/
 
-/*** === Random numbers (random.c) ***/
+/*** === Random numbers (random-legacy.c) ***/
 
 uint random_u32(void);                         /** Return a pseudorandom 32-bit number. **/
 uint random_max(uint max);                     /** Return a pseudorandom 32-bit number in range [0,@max). **/
diff --git a/ucw/random-legacy.c b/ucw/random-legacy.c
new file mode 100644 (file)
index 0000000..016a17a
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ *     UCW Library -- Unbiased Random Numbers
+ *
+ *     (c) 1998--2006 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include <ucw/lib.h>
+
+#include <stdlib.h>
+
+/* We expect the random generator in libc to give at least 30 bits of randomness */
+COMPILE_ASSERT(RAND_MAX_RANGE_TEST, RAND_MAX >= (1 << 30)-1);
+
+uint
+random_u32(void)
+{
+  return (random() & 0xffff) | ((random() & 0xffff) << 16);
+}
+
+uint
+random_max(uint max)
+{
+  uint r, l;
+
+  ASSERT(max <= (1 << 30));
+  l = (RAND_MAX + 1U) - ((RAND_MAX + 1U) % max);
+  do
+    r = random();
+  while (r >= l);
+  return r % max;
+}
+
+u64
+random_u64(void)
+{
+  return
+    ((u64)(random() & 0xffff) << 48) |
+    ((u64)(random() & 0xffffff) << 24) |
+    (random() & 0xffffff);
+}
+
+u64
+random_max_u64(u64 max)
+{
+  if (max < (1 << 30))
+    return random_max(max);
+
+  u64 r, l, m;
+  m = 0xffffffffffffffff;
+  l = m - (m % max);
+  do
+    r = random_u64();
+  while (r >= l);
+  return r % max;
+}
diff --git a/ucw/random.c b/ucw/random.c
deleted file mode 100644 (file)
index 016a17a..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- *     UCW Library -- Unbiased Random Numbers
- *
- *     (c) 1998--2006 Martin Mares <mj@ucw.cz>
- *
- *     This software may be freely distributed and used according to the terms
- *     of the GNU Lesser General Public License.
- */
-
-#include <ucw/lib.h>
-
-#include <stdlib.h>
-
-/* We expect the random generator in libc to give at least 30 bits of randomness */
-COMPILE_ASSERT(RAND_MAX_RANGE_TEST, RAND_MAX >= (1 << 30)-1);
-
-uint
-random_u32(void)
-{
-  return (random() & 0xffff) | ((random() & 0xffff) << 16);
-}
-
-uint
-random_max(uint max)
-{
-  uint r, l;
-
-  ASSERT(max <= (1 << 30));
-  l = (RAND_MAX + 1U) - ((RAND_MAX + 1U) % max);
-  do
-    r = random();
-  while (r >= l);
-  return r % max;
-}
-
-u64
-random_u64(void)
-{
-  return
-    ((u64)(random() & 0xffff) << 48) |
-    ((u64)(random() & 0xffffff) << 24) |
-    (random() & 0xffffff);
-}
-
-u64
-random_max_u64(u64 max)
-{
-  if (max < (1 << 30))
-    return random_max(max);
-
-  u64 r, l, m;
-  m = 0xffffffffffffffff;
-  l = m - (m % max);
-  do
-    r = random_u64();
-  while (r >= l);
-  return r % max;
-}