diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-05-21 23:27:02 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-05-21 23:27:02 -0700 |
commit | c1fed8c508dd89c86de7dd647de0d014c441344e (patch) | |
tree | fac4d327c18fe5a2a205a2f4b4ad7f8287ec3536 /src/modules/stars | |
parent | e21cdb67718a5d203372fd0c425e8be3e1d273f3 (diff) |
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.
Diffstat (limited to 'src/modules/stars')
-rw-r--r-- | src/modules/stars/stars.c | 18 |
1 files changed, 10 insertions, 8 deletions
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; i<rand()%16; i++){ + for(int i=0; i<rand_r(&ctxt->seed)%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; i<rand()%16; i++){ + for(int i=0; i<rand_r(&ctxt->seed)%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; |