From: Vsevolod Stakhov Date: Fri, 6 May 2016 15:50:35 +0000 (+0100) Subject: [Feature] Add utility function to return random number from 0 to 1 X-Git-Tag: 1.3.0~532^2~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8cbcc5d357fe85895dc3311f0484bbba782f026f;p=rspamd.git [Feature] Add utility function to return random number from 0 to 1 --- diff --git a/src/libutil/util.c b/src/libutil/util.c index 87395c568..4fc3eb613 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2027,7 +2027,7 @@ rspamd_hash_seed (void) } gdouble -rspamd_time_jitter (gdouble in, gdouble jitter) +rspamd_random_double (void) { guint64 rnd_int; double res; @@ -2037,11 +2037,17 @@ rspamd_time_jitter (gdouble in, gdouble jitter) res = rnd_int; res *= transform_bias; + return res; +} + +gdouble +rspamd_time_jitter (gdouble in, gdouble jitter) +{ if (jitter == 0) { jitter = in; } - return in + jitter * res; + return in + jitter * rspamd_random_double (); } gboolean diff --git a/src/libutil/util.h b/src/libutil/util.h index ec8150fae..48e91cb4f 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -409,6 +409,12 @@ guint64 rspamd_hash_seed (void); */ gdouble rspamd_time_jitter (gdouble in, gdouble jitter); +/** + * Return random double in range [0..1) + * @return + */ +gdouble rspamd_random_double (void); + /** * Constant time version of memcmp */