From: Florian Forster Date: Sun, 24 Mar 2013 13:21:27 +0000 (+0100) Subject: rrdtool plugin: Use cdrand_range() for the random variation. X-Git-Tag: collectd-5.3.0~18 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=639cdb38830b206598d99bd22898adcdba2877a3 rrdtool plugin: Use cdrand_range() for the random variation. --- diff --git a/src/rrdtool.c b/src/rrdtool.c index e7c48c0e..2c80762e 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -1,6 +1,6 @@ /** * collectd - src/rrdtool.c - * Copyright (C) 2006-2008 Florian octo Forster + * Copyright (C) 2006-2013 Florian octo Forster * Copyright (C) 2008-2008 Sebastian Harl * Copyright (C) 2009 Mariusz Gronczewski * @@ -18,7 +18,7 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster * Sebastian Harl * Mariusz Gronczewski **/ @@ -27,6 +27,7 @@ #include "plugin.h" #include "common.h" #include "utils_avltree.h" +#include "utils_random.h" #include "utils_rrdcreate.h" #include @@ -655,11 +656,8 @@ static int rrd_cache_flush_identifier (cdtime_t timeout, static int64_t rrd_get_random_variation (void) { - double dbl_timeout; - cdtime_t ctm_timeout; - double rand_fact; - _Bool negative; - int64_t ret; + long min; + long max; if (random_timeout <= 0) return (0); @@ -672,20 +670,10 @@ static int64_t rrd_get_random_variation (void) random_timeout = cache_timeout; } - /* This seems a bit complicated, but "random_timeout" is likely larger than - * RAND_MAX, so we can't simply use modulo here. */ - dbl_timeout = CDTIME_T_TO_DOUBLE (random_timeout); - rand_fact = ((double) random ()) - / ((double) RAND_MAX); - negative = (_Bool) (random () % 2); + max = (long) (random_timeout / 2); + min = max - ((long) random_timeout); - ctm_timeout = DOUBLE_TO_CDTIME_T (dbl_timeout * rand_fact); - - ret = (int64_t) ctm_timeout; - if (negative) - ret *= -1; - - return (ret); + return ((int64_t) cdrand_range (min, max)); } /* int64_t rrd_get_random_variation */ static int rrd_cache_insert (const char *filename,