OSDN Git Service

./bootstrap 実行時に warning が出ないよう修正。
[hengband/hengband.git] / src / z-rand.c
index 1ace876..d1e82e6 100644 (file)
@@ -313,4 +313,82 @@ s16b maxroll(int num, int sides)
 }
 
 
+/*
+ * Given a numerator and a denominator, supply a properly rounded result,
+ * using the RNG to smooth out remainders.  -LM-
+ */
+s32b div_round(s32b n, s32b d)
+{
+        s32b tmp;
+
+        /* Refuse to divide by zero */
+        if (!d) return (n);
+
+        /* Division */
+        tmp = n / d;
+
+        /* Rounding */
+        if ((ABS(n) % ABS(d)) > randint0(ABS(d)))
+        {
+                /* Increase the absolute value */
+                if (n * d > 0L) tmp += 1L;
+                else            tmp -= 1L;
+        }
+
+        /* Return */
+        return (tmp);
+}
 
+
+
+
+/*
+ * Extract a "random" number from 0 to m-1, using the "simple" RNG.
+ *
+ * This function should be used when generating random numbers in
+ * "external" program parts like the main-*.c files.  It preserves
+ * the current RNG state to prevent influences on game-play.
+ *
+ * Could also use rand() from <stdlib.h> directly. XXX XXX XXX
+ */
+u32b Rand_simple(u32b m)
+{
+       static bool initialized = FALSE;
+       static u32b simple_rand_value;
+       bool old_rand_quick;
+       u32b old_rand_value;
+       u32b result;
+
+
+       /* Save RNG state */
+       old_rand_quick = Rand_quick;
+       old_rand_value = Rand_value;
+
+       /* Use "simple" RNG */
+       Rand_quick = TRUE;
+
+       if (initialized)
+       {
+               /* Use stored seed */
+               Rand_value = simple_rand_value;
+       }
+       else
+       {
+               /* Initialize with new seed */
+               Rand_value = time(NULL);
+               initialized = TRUE;
+       }
+
+       /* Get a random number */
+       result = randint0(m);
+
+       /* Store the new seed */
+       simple_rand_value = Rand_value;
+
+       /* Restore RNG state */
+       Rand_quick = old_rand_quick;
+       Rand_value = old_rand_value;
+
+       /* Use the value */
+       return (result);
+}