diff options
author | Daniel Dunbar <daniel@zuster.org> | 2005-07-20 07:33:44 +0400 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2005-07-20 07:33:44 +0400 |
commit | ba28fc489aab45968152153dd46e87a8a11f7e64 (patch) | |
tree | 864269ef9026835e6674d72bd5faf1b98dc0388d | |
parent | 87e76e85609e53753e271cc4d1591c6d86cf55ce (diff) |
- added an array shuffling function to BLI_rand
-rw-r--r-- | source/blender/blenlib/BLI_rand.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/intern/rand.c | 28 |
2 files changed, 31 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_rand.h b/source/blender/blenlib/BLI_rand.h index 6513dea8115..c352016ff53 100644 --- a/source/blender/blenlib/BLI_rand.h +++ b/source/blender/blenlib/BLI_rand.h @@ -65,5 +65,8 @@ void BLI_storerand (unsigned int loc_r[2]); */ void BLI_restorerand (unsigned int loc[2]); + /** Shuffle an array randomly using the given seed. */ +void BLI_array_randomize (void *data, int elemSize, int numElems, unsigned int seed); + #endif diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index 1166932019a..28a50a62922 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -29,6 +29,10 @@ * * ***** END GPL/BL DUAL LICENSE BLOCK ***** */ + +#include <stdlib.h> +#include <string.h> + #include "PIL_time.h" #include "BLI_rand.h" @@ -86,3 +90,27 @@ void BLI_fillrand(void *addr, int len) { while (len--) *p++= BLI_rand()&0xFF; BLI_restorerand(save); } + +void BLI_array_randomize(void *data, int elemSize, int numElems, unsigned int seed) +{ + unsigned int oldrand[2]; + int i = numElems; + void *temp = malloc(elemSize); + + BLI_storerand(oldrand); + BLI_srand(seed); + + while (--i) { + int j = BLI_rand()%i; + void *iElem = (unsigned char*)data + i*elemSize; + void *jElem = (unsigned char*)data + j*elemSize; + + memcpy(temp, iElem, elemSize); + memcpy(iElem, jElem, elemSize); + memcpy(jElem, temp, elemSize); + } + + BLI_restorerand(oldrand); + free(temp); +} + |