diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-04-16 09:07:46 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-04-16 09:14:30 +0300 |
commit | c5a126ee70595aa187e7503d8872c689c42b5262 (patch) | |
tree | ab559a3e19a16283ebc81d2da11c87d8480b329d /source/blender/blenlib | |
parent | 8c98b1649d1cee38faff338414d35c87ee0f8c2a (diff) |
Docs: BLI_rand non-obvious behavior
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/intern/rand.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index a03b236b5c6..66c568a7ff3 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -68,6 +68,9 @@ RNG *BLI_rng_new(unsigned int seed) return rng; } +/** + * A version of #BLI_rng_new that hashes the seed. + */ RNG *BLI_rng_new_srandom(unsigned int seed) { RNG *rng = MEM_mallocN(sizeof(*rng), "rng"); @@ -87,6 +90,9 @@ void BLI_rng_seed(RNG *rng, unsigned int seed) rng->X = (((uint64_t) seed) << 16) | LOWSEED; } +/** + * Use a hash table to create better seed. + */ void BLI_rng_srandom(RNG *rng, unsigned int seed) { BLI_rng_seed(rng, seed + hash[seed & 255]); @@ -113,11 +119,17 @@ unsigned int BLI_rng_get_uint(RNG *rng) return (unsigned int) (rng->X >> 17); } +/** + * \return Random value (0..1), but never 1.0. + */ double BLI_rng_get_double(RNG *rng) { return (double) BLI_rng_get_int(rng) / 0x80000000; } +/** + * \return Random value (0..1), but never 1.0. + */ float BLI_rng_get_float(RNG *rng) { return (float) BLI_rng_get_int(rng) / 0x80000000; @@ -172,7 +184,7 @@ void BLI_rng_get_tri_sample_float_v2( void BLI_rng_shuffle_array(RNG *rng, void *data, unsigned int elem_size_i, unsigned int elem_tot) { - const size_t elem_size = (unsigned int)elem_size_i; + const size_t elem_size = (size_t)elem_size_i; unsigned int i = elem_tot; void *temp; @@ -196,6 +208,11 @@ void BLI_rng_shuffle_array(RNG *rng, void *data, unsigned int elem_size_i, unsig free(temp); } +/** + * Simulate getting \a n random values. + * + * \note Useful when threaded code needs consistent values, independent of task division. + */ void BLI_rng_skip(RNG *rng, int n) { while (n--) { @@ -208,7 +225,6 @@ void BLI_rng_skip(RNG *rng, int n) /* initialize with some non-zero seed */ static RNG theBLI_rng = {611330372042337130}; -/* using hash table to create better seed */ void BLI_srandom(unsigned int seed) { BLI_rng_srandom(&theBLI_rng, seed); |