From 8653d547995d7e702e57106b6c6118a5e16f4326 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 11 Feb 1998 09:10:22 +0000 Subject: [PATCH] Added a nice bounded random-number generator. Use where appropriate. --- lib/lib.h | 4 ++++ lib/random.c | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 lib/random.c diff --git a/lib/lib.h b/lib/lib.h index d4ac835f..e0613edd 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -158,4 +158,8 @@ int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen); void scan_obj_tree(byte *, void (*)(ulg, byte *)); +/* random.c */ + +uns random_max(uns); + #endif diff --git a/lib/random.c b/lib/random.c new file mode 100644 index 00000000..01adda5b --- /dev/null +++ b/lib/random.c @@ -0,0 +1,22 @@ +/* + * Sherlock Library -- Unbiased Range Correction for random() + * + * (c) 1998 Martin Mares, + */ + +#include +#include + +#include "lib.h" + +uns +random_max(uns max) +{ + uns r, l; + + l = (RAND_MAX + 1U) - ((RAND_MAX + 1U) % max); + do + r = random(); + while (r >= l); + return r % max; +} -- 2.39.2