From e3f0d6c57dab6624e6e3e07e42c58c3904f8cd5b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 18 Apr 2006 16:17:14 +0000 Subject: [PATCH] * Changed the cucul_rand() behaviour. Now cucul_rand(0, 10) returns random values between 0 and 9 (used to be 0 and 10). Updated documentation accordingly. --- cucul/cucul.c | 5 +++-- cucul/dither.c | 2 +- test/demo.c | 42 +++++++++++++++++++++--------------------- test/dithering.c | 18 +++++++++--------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/cucul/cucul.c b/cucul/cucul.c index 5eb51bc..548dd9b 100644 --- a/cucul/cucul.c +++ b/cucul/cucul.c @@ -241,11 +241,12 @@ void cucul_free_canvas(cucul_canvas_t *cv) * * \param min The lower bound of the integer range. * \param max The upper bound of the integer range. - * \return A random integer comprised between \p min and \p max, inclusive. + * \return A random integer comprised between \p min and \p max - 1 + * (inclusive). */ int cucul_rand(int min, int max) { - return min + (int)((1.0*(max-min+1)) * rand() / (RAND_MAX+1.0)); + return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0)); } /* diff --git a/cucul/dither.c b/cucul/dither.c index 1b2a1da..a62d5b6 100644 --- a/cucul/dither.c +++ b/cucul/dither.c @@ -1251,7 +1251,7 @@ static void init_random_dither(int line) static unsigned int get_random_dither(void) { - return cucul_rand(0x00, 0xff); + return cucul_rand(0x00, 0x100); } static void increment_random_dither(void) diff --git a/test/demo.c b/test/demo.c index c6299be..9ef79c9 100644 --- a/test/demo.c +++ b/test/demo.c @@ -310,8 +310,8 @@ static void demo_all(void) /* Draw a trail behind the foreground sprite */ for(j = i - 60; j < i; j++) { - int delta = cucul_rand(-5, 5); - cucul_set_color(cv, cucul_rand(0, 15), cucul_rand(0, 15)); + int delta = cucul_rand(-5, 6); + cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16)); cucul_putchar(cv, cucul_get_canvas_width(cv) / 2 + cos(0.02*j) * (delta + cucul_get_canvas_width(cv) / 4), cucul_get_canvas_height(cv) / 2 @@ -327,8 +327,8 @@ static void demo_all(void) static void demo_dots(void) { - int xmax = cucul_get_canvas_width(cv) - 1; - int ymax = cucul_get_canvas_height(cv) - 1; + int xmax = cucul_get_canvas_width(cv); + int ymax = cucul_get_canvas_height(cv); int i; static char chars[10] = { @@ -338,7 +338,7 @@ static void demo_dots(void) for(i = 1000; i--;) { /* Putpixel */ - cucul_set_color(cv, cucul_rand(0, 15), cucul_rand(0, 15)); + cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16)); cucul_putchar(cv, cucul_rand(0, xmax), cucul_rand(0, ymax), chars[cucul_rand(0, 9)]); } @@ -376,11 +376,11 @@ static void demo_lines(void) } else { - xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1); - xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1); + xa = cucul_rand(0, w); ya = cucul_rand(0, h); + xb = cucul_rand(0, w); yb = cucul_rand(0, h); } - cucul_set_color(cv, cucul_rand(0, 15), CUCUL_COLOR_BLACK); + cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK); if(outline > 1) cucul_draw_thin_line(cv, xa, ya, xb, yb); else @@ -400,14 +400,14 @@ static void demo_boxes(void) } else { - xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1); - xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1); + xa = cucul_rand(0, w); ya = cucul_rand(0, h); + xb = cucul_rand(0, w); yb = cucul_rand(0, h); } - cucul_set_color(cv, cucul_rand(0, 15), cucul_rand(0, 15)); + cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16)); cucul_fill_box(cv, xa, ya, xb, yb, "#"); - cucul_set_color(cv, cucul_rand(0, 15), CUCUL_COLOR_BLACK); + cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK); if(outline == 2) cucul_draw_thin_box(cv, xa, ya, xb, yb); else if(outline == 1) @@ -435,10 +435,10 @@ static void demo_ellipses(void) } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h); } - cucul_set_color(cv, cucul_rand(0, 15), cucul_rand(0, 15)); + cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16)); cucul_fill_ellipse(cv, x, y, a, b, "#"); - cucul_set_color(cv, cucul_rand(0, 15), CUCUL_COLOR_BLACK); + cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK); if(outline == 2) cucul_draw_thin_ellipse(cv, x, y, a, b); else if(outline == 1) @@ -460,15 +460,15 @@ static void demo_triangles(void) else { - xa = cucul_rand(0, w - 1); ya = cucul_rand(0, h - 1); - xb = cucul_rand(0, w - 1); yb = cucul_rand(0, h - 1); - xc = cucul_rand(0, w - 1); yc = cucul_rand(0, h - 1); + xa = cucul_rand(0, w); ya = cucul_rand(0, h); + xb = cucul_rand(0, w); yb = cucul_rand(0, h); + xc = cucul_rand(0, w); yc = cucul_rand(0, h); } - cucul_set_color(cv, cucul_rand(0, 15), cucul_rand(0, 15)); + cucul_set_color(cv, cucul_rand(0, 16), cucul_rand(0, 16)); cucul_fill_triangle(cv, xa, ya, xb, yb, xc, yc, "#"); - cucul_set_color(cv, cucul_rand(0, 15), CUCUL_COLOR_BLACK); + cucul_set_color(cv, cucul_rand(0, 16), CUCUL_COLOR_BLACK); if(outline == 2) cucul_draw_thin_triangle(cv, xa, ya, xb, yb, xc, yc); else if(outline == 1) @@ -477,8 +477,8 @@ static void demo_triangles(void) static void demo_sprites(void) { - cucul_draw_sprite(cv, cucul_rand(0, cucul_get_canvas_width(cv) - 1), - cucul_rand(0, cucul_get_canvas_height(cv) - 1), sprite, 0); + cucul_draw_sprite(cv, cucul_rand(0, cucul_get_canvas_width(cv)), + cucul_rand(0, cucul_get_canvas_height(cv)), sprite, 0); } #if 0 diff --git a/test/dithering.c b/test/dithering.c index d506996..2fcba79 100644 --- a/test/dithering.c +++ b/test/dithering.c @@ -54,7 +54,7 @@ int main(void) /* distance to 40% */ dist = XRATIO * (x - 40) * (x - 40) + YRATIO * y * y; - if(cucul_rand(-FUZZY, FUZZY) + dist < dista) + if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista) { nearb = neara; distb = dista; neara = 1; dista = dist; } @@ -65,22 +65,22 @@ int main(void) /* check dist to 70% */ dist = XRATIO * (x - 70) * (x - 70) + YRATIO * y * y; - if(cucul_rand(-FUZZY, FUZZY) + dist < dista) + if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista) { nearb = neara; distb = dista; neara = 2; dista = dist; } - else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) + else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb) { nearb = 2; distb = dist; } /* check dist to white */ dist = XRATIO * (x - 100) * (x - 100) + YRATIO * y * y; - if(cucul_rand(-FUZZY, FUZZY) + dist < dista) + if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista) { nearb = neara; distb = dista; neara = 3; dista = dist; } - else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) + else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb) { nearb = 3; distb = dist; } @@ -89,11 +89,11 @@ int main(void) /* check dist to dark */ dist = XRATIO * (x - 40) * (x - 40) + YRATIO * (y - 100) * (y - 100); dist = dist * 12 / 16; - if(cucul_rand(-FUZZY, FUZZY) + dist < dista) + if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista) { nearb = neara; distb = dista; neara = 4; dista = dist; } - else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) + else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb) { nearb = 4; distb = dist; } @@ -101,11 +101,11 @@ int main(void) /* check dist to light */ dist = XRATIO * (x - 100) * (x - 100) + YRATIO * (y - 100) * (y - 100); dist = dist * 8 / 16; - if(cucul_rand(-FUZZY, FUZZY) + dist < dista) + if(cucul_rand(-FUZZY, FUZZY+1) + dist < dista) { nearb = neara; distb = dista; neara = 5; dista = dist; } - else if(cucul_rand(-FUZZY, FUZZY) + dist < distb) + else if(cucul_rand(-FUZZY, FUZZY+1) + dist < distb) { nearb = 5; distb = dist; }