From ce4a020fb88ce33d24e99f0ad8afec50152d26fd Mon Sep 17 00:00:00 2001 From: aleju Date: Fri, 6 Sep 2019 16:56:31 +0200 Subject: [PATCH] Change from fixed to random seed --- CHANGELOG.md | 14 ++++++++++++-- imgaug/random.py | 17 ++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee5a88114..5f9731ab5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -502,10 +502,10 @@ Changes: * [rarely breaking] Removed `imgaug.imgaug.CURRENT_RANDOM_STATE`. Use `imgaug.random.get_global_rng()` instead. * [rarely breaking] Removed `imgaug.imgaug.SEED_MIN_VALUE`. - Use `imgaug.random.SEED_MIN_VALUE` instead or sample seeds via + Use `imgaug.random.SEED_MIN_VALUE` instead or sample seeds via `imgaug.random.generate_seeds_()`. * [rarely breaking] Removed `imgaug.imgaug.SEED_MAX_VALUE`. - Use `imgaug.random.SEED_MAX_VALUE` instead or sample seeds via + Use `imgaug.random.SEED_MAX_VALUE` instead or sample seeds via `imgaug.random.generate_seeds_()`. * Optimized RNG handling throughout all augmenters to minimize the number of RNG copies. RNGs are now re-used as often as possible. This improves @@ -517,6 +517,16 @@ Changes: change. * [breaking] The above listed changes will lead to different values being sampled for the same seeds (compared to past versions of the library). +* [breaking] The seed for `imgaug`'s global random number generator is now + sampled from numpy's default random number generator. That means, that every + run of a program using `imgaug` will by default use a different seed and + hence result in different samples. Previously, a fixed seed was used, + resulting in the same samples for each run (unless the seed was manually + changed to a fixed one). It also means that seeding numpy will automatically + also seed imgaug (not guarantueed that this behaviour will be kept in + future releases). The change from fixed to random seed was done, because the + old (fixed) behaviour didn't match the common practice (and especially not + numpy's standard behaviour) and hence led to confusion. #408 ## Fixes diff --git a/imgaug/random.py b/imgaug/random.py index 966ab9275..dbea98462 100644 --- a/imgaug/random.py +++ b/imgaug/random.py @@ -66,13 +66,6 @@ SEED_MIN_VALUE = 0 SEED_MAX_VALUE = 2**31-1 -# We pick a large random seed here, that makes collisions with seeds chosen -# by the user unlikely to happen. -# Note that the library might switch to random-seed instead of fixed-seed -# in the future, so it is not guarantueed that this constant lives for very -# long. -SEED_GLOBAL_RNG = 148194721 - # TODO decrease pool_size in SeedSequence to 2 or 1? # TODO add 'with resetted_rng(...)' # TODO change random_state to rng or seed @@ -735,8 +728,14 @@ def get_global_rng(): """ global GLOBAL_RNG if GLOBAL_RNG is None: - # TODO replace seed by constant - GLOBAL_RNG = RNG(convert_seed_to_generator(SEED_GLOBAL_RNG)) + # This uses numpy's random state to sample a seed. + # Alternatively, `secrets.randbits(n_bits)` (3.6+) and + # `os.urandom(n_bytes)` could be used. + # See https://stackoverflow.com/a/27286733/3760780 + # for an explanation how random.seed() picks a random seed value. + seed = generate_seed_(np.random) + + GLOBAL_RNG = RNG(convert_seed_to_generator(seed)) return GLOBAL_RNG