diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-04-16 03:12:40 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-04-16 03:12:40 +0400 |
commit | afb4b65167165613f177a531bd3d4dcb3649c1c6 (patch) | |
tree | 32e1446b5fc3ce8ec00fa0e8b9e0fcb2eedda127 /source/blender/blenlib/intern/rand.c | |
parent | 638b084f824bc345468bc8e02422b5da65a641a7 (diff) |
Random number generator: replace a bunch of usage of the global random number
generator with a local one. It's not thread safe and will not give repeatable
results, so in most cases it should not be used.
Also fixes #34992 where the noise texture of a displacement modifier was not
properly random in opengl animation render, because the seed got reset to a
fixed value by an unrelated function while for final render it changed each
frame.
Diffstat (limited to 'source/blender/blenlib/intern/rand.c')
-rw-r--r-- | source/blender/blenlib/intern/rand.c | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index 76d17f34b5e..743d910e418 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -73,6 +73,15 @@ RNG *BLI_rng_new(unsigned int seed) return rng; } +RNG *BLI_rng_new_srandom(unsigned int seed) +{ + RNG *rng = MEM_mallocN(sizeof(*rng), "rng"); + + BLI_rng_srandom(rng, seed); + + return rng; +} + void BLI_rng_free(RNG *rng) { MEM_freeN(rng); @@ -145,13 +154,8 @@ void BLI_rng_skip(RNG *rng, int n) /***/ -static RNG theBLI_rng = {0}; - -/* note, this one creates periodical patterns */ -void BLI_srand(unsigned int seed) -{ - BLI_rng_seed(&theBLI_rng, seed); -} +/* initialize with some non-zero seed */ +static RNG theBLI_rng = {611330372042337130}; /* using hash table to create better seed */ void BLI_srandom(unsigned int seed) @@ -164,23 +168,27 @@ int BLI_rand(void) return BLI_rng_get_int(&theBLI_rng); } -double BLI_drand(void) -{ - return BLI_rng_get_double(&theBLI_rng); -} - float BLI_frand(void) { return BLI_rng_get_float(&theBLI_rng); } -void BLI_fillrand(void *addr, int len) +float BLI_hash_frand(unsigned int seed) { - RNG rng; - unsigned char *p = addr; + r_uint64 X; + + seed = seed + hash[seed & 255]; + X = (((r_uint64) seed) << 16) | LOWSEED; + seed = (int)(((MULTIPLIER * X + ADDEND) & MASK) >> 17); + + seed = seed + hash[seed & 255]; + X = (((r_uint64) seed) << 16) | LOWSEED; + X = (int)(((MULTIPLIER * X + ADDEND) & MASK) >> 17); + + seed = seed + hash[seed & 255]; + X = (((r_uint64) seed) << 16) | LOWSEED; - BLI_rng_seed(&rng, (unsigned int) (PIL_check_seconds_timer() * 0x7FFFFFFF)); - while (len--) *p++ = BLI_rng_get_int(&rng) & 0xFF; + return (int)(((MULTIPLIER * X + ADDEND) & MASK) >> 17); } void BLI_array_randomize(void *data, int elemSize, int numElems, unsigned int seed) |