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/stars/stars.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/modules/stars') diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index 21ae7f5..5506559 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -32,6 +32,7 @@ typedef struct stars_context_t { float offset_x; float offset_y; float offset_angle; + unsigned seed; } stars_context_t; typedef struct stars_setup_t { @@ -44,8 +45,8 @@ static stars_setup_t stars_default_setup = { }; -float get_random_unit_coord() { - return (((float)rand()/(float)RAND_MAX)*2.0)-1.0; +float get_random_unit_coord(unsigned *seed) { + return (((float)rand_r(seed)/(float)RAND_MAX)*2.0)-1.0; } @@ -63,6 +64,7 @@ static void * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpu return NULL; ctxt->points = NULL; + ctxt->seed = seed; ctxt->rot_adj = ((stars_setup_t *)setup)->rot_adj; ctxt->rot_rate = 0.00; ctxt->rot_angle = 0; @@ -72,12 +74,12 @@ static void * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpu //add a bunch of points for(z=0.01; z<1; z=z+0.01) { - for(int i=0; iseed)%16; i++){ p_ptr = malloc(sizeof(struct points)); if (!p_ptr) return NULL; - p_ptr->x = get_random_unit_coord(); - p_ptr->y = get_random_unit_coord(); + p_ptr->x = get_random_unit_coord(&ctxt->seed); + p_ptr->y = get_random_unit_coord(&ctxt->seed); p_ptr->z = z; p_ptr->next = ctxt->points; ctxt->points = p_ptr; @@ -185,12 +187,12 @@ static void stars_render_fragment(void *context, unsigned ticks, unsigned cpu, t } // add stars at horizon - for(int i=0; iseed)%16; i++){ tmp_ptr = malloc(sizeof(struct points)); if (!tmp_ptr) break; - tmp_ptr->x = get_random_unit_coord(); - tmp_ptr->y = get_random_unit_coord(); + tmp_ptr->x = get_random_unit_coord(&ctxt->seed); + tmp_ptr->y = get_random_unit_coord(&ctxt->seed); tmp_ptr->z = 0.01; tmp_ptr->next = ctxt->points; ctxt->points = tmp_ptr; -- cgit v1.2.3