]> mj.ucw.cz Git - libucw.git/commitdiff
Added a nice bounded random-number generator. Use where appropriate.
authorMartin Mares <mj@ucw.cz>
Wed, 11 Feb 1998 09:10:22 +0000 (09:10 +0000)
committerMartin Mares <mj@ucw.cz>
Wed, 11 Feb 1998 09:10:22 +0000 (09:10 +0000)
lib/lib.h
lib/random.c [new file with mode: 0644]

index d4ac835f7d8fda352c5625af6f398b94183c176a..e0613eddf1733376c9de66985167d852077d9fcc 100644 (file)
--- 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 (file)
index 0000000..01adda5
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ *     Sherlock Library -- Unbiased Range Correction for random()
+ *
+ *     (c) 1998 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#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;
+}