diff options
| author | Tom Lane | 2021-11-29 02:32:36 +0000 |
|---|---|---|
| committer | Tom Lane | 2021-11-29 02:33:07 +0000 |
| commit | 3804539e48e794781c6145c7f988f5d507418fa8 (patch) | |
| tree | 317904b43ca8c1d510b23cb8fdd7b05a75e971bc /src/test | |
| parent | f44ceb46ec2d8da48f6e145bf462d5620c25e079 (diff) | |
Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.
Standardize on xoroshiro128** as our basic PRNG algorithm, eliminating
a bunch of platform dependencies as well as fundamentally-obsolete PRNG
code. In addition, this API replacement will ease replacing the
algorithm again in future, should that become necessary.
xoroshiro128** is a few percent slower than the drand48 family,
but it can produce full-width 64-bit random values not only 48-bit,
and it should be much more trustworthy. It's likely to be noticeably
faster than the platform's random(), depending on which platform you
are thinking about; and we can have non-global state vectors easily,
unlike with random(). It is not cryptographically strong, but neither
are the functions it replaces.
Fabien Coelho, reviewed by Dean Rasheed, Aleksander Alekseev, and myself
Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2105241211230.165418@pseudo
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/modules/test_bloomfilter/test_bloomfilter.c | 4 | ||||
| -rw-r--r-- | src/test/modules/test_integerset/test_integerset.c | 6 | ||||
| -rw-r--r-- | src/test/modules/test_rbtree/test_rbtree.c | 5 |
3 files changed, 8 insertions, 7 deletions
diff --git a/src/test/modules/test_bloomfilter/test_bloomfilter.c b/src/test/modules/test_bloomfilter/test_bloomfilter.c index 96c50114286..7109f0476c4 100644 --- a/src/test/modules/test_bloomfilter/test_bloomfilter.c +++ b/src/test/modules/test_bloomfilter/test_bloomfilter.c @@ -12,6 +12,7 @@ */ #include "postgres.h" +#include "common/pg_prng.h" #include "fmgr.h" #include "lib/bloomfilter.h" #include "miscadmin.h" @@ -83,9 +84,8 @@ create_and_test_bloom(int power, int64 nelements, int callerseed) * Generate random seed, or use caller's. Seed should always be a * positive value less than or equal to PG_INT32_MAX, to ensure that any * random seed can be recreated through callerseed if the need arises. - * (Don't assume that RAND_MAX cannot exceed PG_INT32_MAX.) */ - seed = callerseed < 0 ? random() % PG_INT32_MAX : callerseed; + seed = callerseed < 0 ? pg_prng_int32p(&pg_global_prng_state) : callerseed; /* Create Bloom filter, populate it, and report on false positive rate */ filter = bloom_create(nelements, bloom_work_mem, seed); diff --git a/src/test/modules/test_integerset/test_integerset.c b/src/test/modules/test_integerset/test_integerset.c index 21c6f49b378..58ffff89b38 100644 --- a/src/test/modules/test_integerset/test_integerset.c +++ b/src/test/modules/test_integerset/test_integerset.c @@ -12,6 +12,7 @@ */ #include "postgres.h" +#include "common/pg_prng.h" #include "fmgr.h" #include "lib/integerset.h" #include "miscadmin.h" @@ -248,8 +249,7 @@ test_pattern(const test_spec *spec) * only a small part of the integer space is used. We would very * rarely hit values that are actually in the set. */ - x = (pg_lrand48() << 31) | pg_lrand48(); - x = x % (last_int + 1000); + x = pg_prng_uint64_range(&pg_global_prng_state, 0, last_int + 1000); /* Do we expect this value to be present in the set? */ if (x >= last_int) @@ -571,7 +571,7 @@ test_huge_distances(void) */ while (num_values < 1000) { - val += pg_lrand48(); + val += pg_prng_uint32(&pg_global_prng_state); values[num_values++] = val; } diff --git a/src/test/modules/test_rbtree/test_rbtree.c b/src/test/modules/test_rbtree/test_rbtree.c index 713ebd1b26d..646c6e42fa8 100644 --- a/src/test/modules/test_rbtree/test_rbtree.c +++ b/src/test/modules/test_rbtree/test_rbtree.c @@ -13,6 +13,7 @@ #include "postgres.h" +#include "common/pg_prng.h" #include "fmgr.h" #include "lib/rbtree.h" #include "utils/memutils.h" @@ -108,7 +109,7 @@ GetPermutation(int size) */ for (i = 1; i < size; i++) { - int j = random() % (i + 1); + int j = pg_prng_uint64_range(&pg_global_prng_state, 0, i); if (j < i) /* avoid fetching undefined data if j=i */ permutation[i] = permutation[j]; @@ -320,7 +321,7 @@ testdelete(int size, int delsize) for (i = 0; i < delsize; i++) { - int k = random() % size; + int k = pg_prng_uint64_range(&pg_global_prng_state, 0, size - 1); while (chosen[k]) k = (k + 1) % size; |
