From: Martin Mares Date: Wed, 11 Feb 1998 09:10:22 +0000 (+0000) Subject: Added a nice bounded random-number generator. Use where appropriate. X-Git-Tag: holmes-import~1676 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=8653d547995d7e702e57106b6c6118a5e16f4326;p=libucw.git Added a nice bounded random-number generator. Use where appropriate. --- 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; +}