Skip to content

Commit

Permalink
Fix RandomScale calculation
Browse files Browse the repository at this point in the history
`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%
  • Loading branch information
nilx authored and t8m committed Nov 3, 2021
1 parent 410e9e3 commit 19c2e1d
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 19c2e1d

Please # to comment.