From c1fed8c508dd89c86de7dd647de0d014c441344e Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sat, 21 May 2022 23:27:02 -0700 Subject: modules/*: first stab at utilizing supplied seeds This is a mostly mechanical change of using rand_r() in place of rand(), using the provided seed as the seed state. There's some outstanding rand()s outside of create_context() which should probably get switched over, with the seed being stowed in the context struct. I didn't bother going deeper on this at the moment in the interests of getting to sleep soon. --- src/modules/voronoi/voronoi.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/modules/voronoi/voronoi.c') diff --git a/src/modules/voronoi/voronoi.c b/src/modules/voronoi/voronoi.c index 0a9ba70..adb180d 100644 --- a/src/modules/voronoi/voronoi.c +++ b/src/modules/voronoi/voronoi.c @@ -42,6 +42,7 @@ typedef struct voronoi_distances_t { } voronoi_distances_t; typedef struct voronoi_context_t { + unsigned seed; voronoi_setup_t setup; voronoi_distances_t distances; voronoi_cell_t cells[]; @@ -67,12 +68,12 @@ static void voronoi_randomize(voronoi_context_t *ctxt) for (size_t i = 0; i < ctxt->setup.n_cells; i++) { voronoi_cell_t *p = &ctxt->cells[i]; - p->origin.x = ((float)rand() * inv_rand_max) * 2.f - 1.f; - p->origin.y = ((float)rand() * inv_rand_max) * 2.f - 1.f; + p->origin.x = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; + p->origin.y = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; - p->color = ((uint32_t)(rand() % 256)) << 16; - p->color |= ((uint32_t)(rand() % 256)) << 8; - p->color |= ((uint32_t)(rand() % 256)); + p->color = ((uint32_t)(rand_r(&ctxt->seed) % 256)) << 16; + p->color |= ((uint32_t)(rand_r(&ctxt->seed) % 256)) << 8; + p->color |= ((uint32_t)(rand_r(&ctxt->seed) % 256)); } } @@ -89,6 +90,7 @@ static void * voronoi_create_context(unsigned seed, unsigned ticks, unsigned n_c return NULL; ctxt->setup = *(voronoi_setup_t *)setup; + ctxt->seed = seed; voronoi_randomize(ctxt); -- cgit v1.2.3