From 7811276578e55be0950a1bdd5c9328bb9e29c6df Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 20 Jul 2022 12:35:06 -0700 Subject: modules/swarm: wire up seed to various randomizers Just plumbing seed down in the obvoius manner, this could probably be cleaned up a bit in the future. --- src/modules/swarm/swarm.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/modules') diff --git a/src/modules/swarm/swarm.c b/src/modules/swarm/swarm.c index fd852cd..0c58bf9 100644 --- a/src/modules/swarm/swarm.c +++ b/src/modules/swarm/swarm.c @@ -70,17 +70,17 @@ static swarm_setup_t swarm_default_setup = { }; -static inline float randf(float min, float max) +static inline float randf(unsigned *seed, float min, float max) { - return ((float)rand() / (float)RAND_MAX) * (max - min) + min; + return ((float)rand_r(seed) / (float)RAND_MAX) * (max - min) + min; } -static inline void v3f_rand(v3f_t *v, float min, float max) +static inline void v3f_rand(v3f_t *v, unsigned *seed, float min, float max) { - v->x = randf(min, max); - v->y = randf(min, max); - v->z = randf(min, max); + v->x = randf(seed, min, max); + v->y = randf(seed, min, max); + v->z = randf(seed, min, max); } @@ -150,12 +150,12 @@ static v3f_t v3f_lerp(v3f_t a, v3f_t b, float t) } -static void boid_randomize(boid_t *boid) +static void boid_randomize(boid_t *boid, unsigned *seed) { - v3f_rand(&boid->position, -1.f, 1.f); - v3f_rand(&boid->direction, -1.f, 1.f); + v3f_rand(&boid->position, seed, -1.f, 1.f); + v3f_rand(&boid->direction, seed, -1.f, 1.f); v3f_normalize(&boid->direction); - boid->velocity = randf(.05f, .2f); + boid->velocity = randf(seed, .05f, .2f); } @@ -195,7 +195,7 @@ static til_module_context_t * swarm_create_context(unsigned seed, unsigned ticks ctxt->setup = *(swarm_setup_t *)setup; for (unsigned i = 0; i < SWARM_SIZE; i++) - boid_randomize(&ctxt->boids[i]); + boid_randomize(&ctxt->boids[i], &seed); return &ctxt->til_module_context; } -- cgit v1.2.3