From be2bd2baf7d37bd51fc1d27b05287756f0e8fff2 Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Tue, 9 Jun 2020 19:11:11 +0200 Subject: [PATCH] Random: Defined random_gen_seed() to simplify old programs. --- images/image-test.c | 4 +--- images/ucw-image-sim-test.c | 5 +---- ucw/lib.h | 6 +++++- ucw/mempool.c | 4 +--- ucw/random-legacy.c | 16 ++++++++++++++++ ucw/sorter/sort-test.c | 2 +- ucw/trie-test.c | 5 ++--- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/images/image-test.c b/images/image-test.c index 2e6c3d51..8098deff 100644 --- a/images/image-test.c +++ b/images/image-test.c @@ -19,8 +19,6 @@ #include #include #include -#include -#include 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(); diff --git a/images/ucw-image-sim-test.c b/images/ucw-image-sim-test.c index 8734b30e..2247db64 100644 --- a/images/ucw-image-sim-test.c +++ b/images/ucw-image-sim-test.c @@ -20,9 +20,6 @@ #include #include #include -#include -#include -#include 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); diff --git a/ucw/lib.h b/ucw/lib.h index 6f93bb7e..003f7b1b 100644 --- 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). **/ diff --git a/ucw/mempool.c b/ucw/mempool.c index 05251c04..194cdaf4 100644 --- a/ucw/mempool.c +++ b/ucw/mempool.c @@ -433,8 +433,6 @@ mp_pop(struct mempool *pool) #include #include -#include -#include 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]; diff --git a/ucw/random-legacy.c b/ucw/random-legacy.c index 016a17a2..07afddf6 100644 --- a/ucw/random-legacy.c +++ b/ucw/random-legacy.c @@ -2,18 +2,34 @@ * UCW Library -- Unbiased Random Numbers * * (c) 1998--2006 Martin Mares + * (c) 2020 Pavel Charvat * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. */ #include +#include #include /* 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) { diff --git a/ucw/sorter/sort-test.c b/ucw/sorter/sort-test.c index f8384a75..6aabe6d9 100644 --- a/ucw/sorter/sort-test.c +++ b/ucw/sorter/sort-test.c @@ -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]; diff --git a/ucw/trie-test.c b/ucw/trie-test.c index 9114fe92..e53a9496 100644 --- a/ucw/trie-test.c +++ b/ucw/trie-test.c @@ -12,9 +12,8 @@ #include #include -#include +#include #include -#include #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")) -- 2.39.2