From 0aba23a14014d84a5ca1e3a2af45608cc9f23044 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 20 Jul 2022 21:43:03 -0700 Subject: modules/meta2d: more rand()->rand_r() conversions Normalized all the randomizers to use til_module_context.seed while in here. --- src/modules/meta2d/meta2d.c | 7 +++---- src/modules/meta2d/v2f.h | 10 +++++----- src/modules/meta2d/v3f.h | 12 ++++++------ 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'src/modules/meta2d') diff --git a/src/modules/meta2d/meta2d.c b/src/modules/meta2d/meta2d.c index 1bb8827..b7b6708 100644 --- a/src/modules/meta2d/meta2d.c +++ b/src/modules/meta2d/meta2d.c @@ -82,10 +82,9 @@ static til_module_context_t * meta2d_create_context(unsigned seed, unsigned tick for (int i = 0; i < META2D_NUM_BALLS; i++) { meta2d_ball_t *ball = &ctxt->balls[i]; - /* TODO: add _r() variants of v[23]f_rand()? */ - v2f_rand(&ball->position, &(v2f_t){-.7f, -.7f}, &(v2f_t){.7f, .7f}); - ball->radius = rand_r(&seed) / (float)RAND_MAX * .2f + .05f; - v3f_rand(&ball->color, &(v3f_t){0.f, 0.f, 0.f}, &(v3f_t){1.f, 1.f, 1.f}); + v2f_rand(&ball->position, &ctxt->til_module_context.seed, &(v2f_t){-.7f, -.7f}, &(v2f_t){.7f, .7f}); + ball->radius = rand_r(&ctxt->til_module_context.seed) / (float)RAND_MAX * .2f + .05f; + v3f_rand(&ball->color, &ctxt->til_module_context.seed, &(v3f_t){0.f, 0.f, 0.f}, &(v3f_t){1.f, 1.f, 1.f}); } return &ctxt->til_module_context; diff --git a/src/modules/meta2d/v2f.h b/src/modules/meta2d/v2f.h index 8f51ee0..088772d 100644 --- a/src/modules/meta2d/v2f.h +++ b/src/modules/meta2d/v2f.h @@ -278,19 +278,19 @@ static inline v2f_t * v2f_trilerp(v2f_t *res, const v2f_t *aaa, const v2f_t *aba } -static inline v2f_t _v2f_rand(const v2f_t *min, const v2f_t *max) +static inline v2f_t _v2f_rand(unsigned *seedp, const v2f_t *min, const v2f_t *max) { return (v2f_t){ - .x = min->x + (float)rand() * (1.f/RAND_MAX) * (max->x - min->x), - .y = min->y + (float)rand() * (1.f/RAND_MAX) * (max->y - min->y), + .x = min->x + (float)rand_r(seedp) * (1.f/RAND_MAX) * (max->x - min->x), + .y = min->y + (float)rand_r(seedp) * (1.f/RAND_MAX) * (max->y - min->y), }; } -static inline v2f_t * v2f_rand(v2f_t *res, const v2f_t *min, const v2f_t *max) +static inline v2f_t * v2f_rand(v2f_t *res, unsigned *seedp, const v2f_t *min, const v2f_t *max) { if (_v2f_allocated(&res)) - *res = _v2f_rand(min, max); + *res = _v2f_rand(seedp, min, max); return res; } diff --git a/src/modules/meta2d/v3f.h b/src/modules/meta2d/v3f.h index 79aea72..d923ecc 100644 --- a/src/modules/meta2d/v3f.h +++ b/src/modules/meta2d/v3f.h @@ -297,20 +297,20 @@ static inline v3f_t * v3f_cross(v3f_t *res, const v3f_t *a, const v3f_t *b) } -static inline v3f_t _v3f_rand(const v3f_t *min, const v3f_t *max) +static inline v3f_t _v3f_rand(unsigned *seedp, const v3f_t *min, const v3f_t *max) { return (v3f_t){ - .x = min->x + (float)rand() * (1.f/RAND_MAX) * (max->x - min->x), - .y = min->y + (float)rand() * (1.f/RAND_MAX) * (max->y - min->y), - .z = min->z + (float)rand() * (1.f/RAND_MAX) * (max->z - min->z), + .x = min->x + (float)rand_r(seedp) * (1.f/RAND_MAX) * (max->x - min->x), + .y = min->y + (float)rand_r(seedp) * (1.f/RAND_MAX) * (max->y - min->y), + .z = min->z + (float)rand_r(seedp) * (1.f/RAND_MAX) * (max->z - min->z), }; } -static inline v3f_t * v3f_rand(v3f_t *res, const v3f_t *min, const v3f_t *max) +static inline v3f_t * v3f_rand(v3f_t *res, unsigned *seedp, const v3f_t *min, const v3f_t *max) { if (_v3f_allocated(&res)) - *res = _v3f_rand(min, max); + *res = _v3f_rand(seedp, min, max); return res; } -- cgit v1.2.1