From 19c2e1deb47b2fd9fb0e4ac3667bebc06d86174d Mon Sep 17 00:00:00 2001 From: Nicolas Limare Date: Wed, 3 Nov 2021 23:49:30 +0900 Subject: [PATCH] Fix RandomScale calculation `random()` returns a value in `[0 .. 2^31[` (cf. [posix spec](https://pubs.opengroup.org/onlinepubs/9699919799/functions/random.html)). On some systems, `RAND_MAX != 2^31 - 1`. This patch ensures that scaling factor is in < 100% --- src/cron.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cron.c b/src/cron.c index 33e33ab..da9aee3 100644 --- a/src/cron.c +++ b/src/cron.c @@ -308,7 +308,7 @@ int main(int argc, char *argv[]) { if (gettimeofday(&tv, &tz) != 0) tv.tv_usec = 0; srandom((unsigned int)(pid + tv.tv_usec)); - RandomScale = (double)random() / (double)RAND_MAX; + RandomScale = (double)random() / (double)(1lu << 31); snprintf(buf, sizeof(buf), "RANDOM_DELAY will be scaled with factor %d%% if used.", (int)(RandomScale*100)); log_it("CRON", pid, "INFO", buf, 0);