aboutsummaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/misc.c b/misc.c
new file mode 100644
index 0000000..58c4fb7
--- /dev/null
+++ b/misc.c
@@ -0,0 +1,25 @@
+/*
+ * misc.c: Miscellaneous helpful functions.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+
+#include "puzzles.h"
+
+int rand_upto(int limit)
+{
+ unsigned long divisor = RAND_MAX / (unsigned)limit;
+ unsigned long max = divisor * (unsigned)limit;
+ unsigned long n;
+
+ assert(limit > 0);
+
+ do {
+ n = rand();
+ } while (n >= max);
+
+ n /= divisor;
+
+ return (int)n;
+}