]> mj.ucw.cz Git - libucw.git/commitdiff
Random: Defined random_gen_seed() to simplify old programs.
authorPavel Charvat <pchar@ucw.cz>
Tue, 9 Jun 2020 17:11:11 +0000 (19:11 +0200)
committerPavel Charvat <pchar@ucw.cz>
Tue, 22 Mar 2022 18:06:59 +0000 (18:06 +0000)
images/image-test.c
images/ucw-image-sim-test.c
ucw/lib.h
ucw/mempool.c
ucw/random-legacy.c
ucw/sorter/sort-test.c
ucw/trie-test.c

index 2e6c3d51785b42b40611a3f1b3fd3e82f03f322f..8098deff43253d68d1adcd65685756b76f347e48 100644 (file)
@@ -19,8 +19,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#include <time.h>
-#include <unistd.h>
 
 static uint want_image_iface;
 static uint want_threads;
@@ -225,7 +223,7 @@ main(int argc, char **argv)
     else
       die("Invalid parameter");
 
-  srandom(time(NULL) ^ getpid());
+  random_gen_seed();
 
   if (want_image_iface)
     test_image_iface();
index 8734b30e26959091227a931ccca23634050593d2..2247db6442d655c826c42df0b78d0aae9a50d3ba 100644 (file)
@@ -20,9 +20,6 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <stdio.h>
-#include <time.h>
-#include <sys/types.h>
-#include <unistd.h>
 
 static void NONRET
 usage(void)
@@ -214,7 +211,7 @@ main(int argc, char **argv)
     file_name_2 = argv[optind++];
 
   MSG("Initializing image library");
-  srandom(time(NULL) ^ getpid());
+  random_gen_seed();
   srgb_to_luv_init();
   image_context_init(&ctx);
 
index 6f93bb7e73b5470c86bd8a568ff43cd03fab8835..003f7b1b15f372d55a8d7ec585f8e088ec9ff26d 100644 (file)
--- a/ucw/lib.h
+++ b/ucw/lib.h
@@ -36,6 +36,8 @@
 #define page_alloc_zero ucw_page_alloc_zero
 #define page_free ucw_page_free
 #define page_realloc ucw_page_realloc
+#define random_set_seed ucw_random_set_seed
+#define random_gen_seed ucw_random_gen_seed
 #define random_max ucw_random_max
 #define random_max_u64 ucw_random_max_u64
 #define random_u32 ucw_random_u32
@@ -234,8 +236,10 @@ void big_free(void *start, u64 len);               /** Free block allocated by @big_alloc() o
 
 /*** === Random numbers (random-legacy.c) ***/
 
+void random_set_seed(uint seed);               /** Initialize seed value for the legacy interface below. **/
+uint random_gen_seed(void);                    /** Combines fastrand_gen_seed_value() and random_set_seed(). **/
 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). **/
+uint random_max(uint max);                     /** Return a pseudorandom 30-bit number in range [0,@max). **/
 u64 random_u64(void);                          /** Return a pseudorandom 64-bit number. **/
 u64 random_max_u64(u64 max);                   /** Return a pseudorandom 64-bit number in range [0,@max). **/
 
index 05251c0487d425f1b062535e80f0b30b8b15e3d6..194cdaf41e8670bc97da741e07e443e550ebd5ad 100644 (file)
@@ -433,8 +433,6 @@ mp_pop(struct mempool *pool)
 
 #include <ucw/getopt.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
 
 static void
 fill(byte *ptr, uint len, uint magic)
@@ -454,12 +452,12 @@ check(byte *ptr, uint len, uint magic, uint align)
 
 int main(int argc, char **argv)
 {
-  srand(time(NULL));
   log_init(argv[0]);
   cf_def_file = NULL;
   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || argc != optind)
     die("Invalid usage");
 
+  random_gen_seed();
   uint max = 1000, n = 0, m = 0, can_realloc = 0;
   void *ptr[max];
   struct mempool_state *state[max];
index 016a17a2e2e39f5ff11482b12e7aa00ccb739843..07afddf664d73f152fc5106bcffd3fa992856d8a 100644 (file)
@@ -2,18 +2,34 @@
  *     UCW Library -- Unbiased Random Numbers
  *
  *     (c) 1998--2006 Martin Mares <mj@ucw.cz>
+ *     (c) 2020 Pavel Charvat <pchar@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 <ucw/random.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);
 
+void
+random_set_seed(uint seed)
+{
+  srandom(seed);
+}
+
+uint
+random_gen_seed(void)
+{
+  uint seed = fastrand_gen_seed_value();
+  random_set_seed(seed);
+  return seed;
+}
+
 uint
 random_u32(void)
 {
index f8384a7554a144eba31211e414ab1fb2b282f037..6aabe6d961605a8d815e07cbcb38f51da72d87d3 100644 (file)
@@ -333,7 +333,7 @@ test_strings(uint mode, u64 size)
   uint avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
   uint N = MIN(size / avg_item_size, 0xffffffff);
   msg(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
-  srand(1);
+  random_set_seed(1);
 
   struct key4 k, lastk;
   byte buf[256], buf2[256];
index 9114fe92cd61ffc6826e7357b90d40dd8aed2a83..e53a94963c6b41ce7f53f74f370ee03ab1b3bd73 100644 (file)
@@ -12,9 +12,8 @@
 #include <ucw/lib.h>
 #include <ucw/getopt.h>
 
-#include <stdlib.h>
+#include <alloca.h>
 #include <string.h>
-#include <time.h>
 
 #define TRIE_PREFIX(x) basic_##x
 #define TRIE_NODE_TYPE char
@@ -200,7 +199,7 @@ int main(int argc, char **argv)
   log_init(argv[0]);
   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || optind + 1 != argc)
     die("Invalid usage, see the source code");
-  srandom(time(NULL));
+  random_gen_seed();
 
   char *test = argv[optind];
   if (!strcmp(test, "basic"))