# Source:NetHack 3.6.1/src/rnd.c
Below is the full text to rnd.c from the source code of NetHack 3.6.1. To link to a particular line, write [[Source:NetHack 3.6.1/src/rnd.c#line123]], for example.

Contents

1 Top of file
2 rn2
3 rnl
4 rnd
5 d
6 rne
7 rnz

Top of file
&nbsp;/* NetHack 3.6	rnd.c	$NHDT-Date: 1524689470 2018/04/25 20:51:10 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.18 $ */
&nbsp;/*      Copyright (c) 2004 by Robert Patrick Rankin               */
&nbsp;/* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

&nbsp;
&nbsp;#include "hack.h"
&nbsp;
&nbsp;/* "Rand()"s definition is determined by [OS]conf.h */
&nbsp;#if defined(LINT) &amp;&amp; defined(UNIX) /* rand() is long... */
&nbsp;extern int NDECL(rand);
&nbsp;#define RND(x) (rand() % x)
&nbsp;#else /* LINT */
&nbsp;#if defined(UNIX) || defined(RANDOM)
&nbsp;#define RND(x) ((int) (Rand() % (long) (x)))
&nbsp;#else
&nbsp;/* Good luck: the bottom order bits are cyclic. */
&nbsp;#define RND(x) ((int) ((Rand() &gt;&gt; 3) % (x)))
&nbsp;#endif
&nbsp;#endif /* LINT */
&nbsp;
rn2
&nbsp;/* 0 &lt;= rn2(x) &lt; x */
&nbsp;int
&nbsp;rn2(x)
&nbsp;register int x;
&nbsp;{
&nbsp;#ifdef BETA
&nbsp;    if (x &lt;= 0) {
&nbsp;        impossible("rn2(%d) attempted", x);
&nbsp;        return 0;
&nbsp;    }
&nbsp;    x = RND(x);
&nbsp;    return x;
&nbsp;#else
&nbsp;    return RND(x);
&nbsp;#endif
&nbsp;}
&nbsp;
rnl
&nbsp;/* 0 &lt;= rnl(x) &lt; x; sometimes subtracting Luck;
&nbsp;   good luck approaches 0, bad luck approaches (x-1) */
&nbsp;int
&nbsp;rnl(x)
&nbsp;register int x;
&nbsp;{
&nbsp;    register int i, adjustment;
&nbsp;
&nbsp;#ifdef BETA
&nbsp;    if (x &lt;= 0) {
&nbsp;        impossible("rnl(%d) attempted", x);
&nbsp;        return 0;
&nbsp;    }
&nbsp;#endif
&nbsp;
&nbsp;    adjustment = Luck;
&nbsp;    if (x &lt;= 15) {
&nbsp;        /* for small ranges, use Luck/3 (rounded away from 0);
&nbsp;           also guard against architecture-specific differences
&nbsp;           of integer division involving negative values */
&nbsp;        adjustment = (abs(adjustment) + 1) / 3 * sgn(adjustment);
&nbsp;        /*
&nbsp;         *       11..13 -&gt;  4
&nbsp;         *        8..10 -&gt;  3
&nbsp;         *        5.. 7 -&gt;  2
&nbsp;         *        2.. 4 -&gt;  1
&nbsp;         *       -1,0,1 -&gt;  0 (no adjustment)
&nbsp;         *       -4..-2 -&gt; -1
&nbsp;         *       -7..-5 -&gt; -2
&nbsp;         *      -10..-8 -&gt; -3
&nbsp;         *      -13..-11-&gt; -4
&nbsp;         */
&nbsp;    }
&nbsp;
&nbsp;    i = RND(x);
&nbsp;    if (adjustment &amp;&amp; rn2(37 + abs(adjustment))) {
&nbsp;        i -= adjustment;
&nbsp;        if (i &lt; 0)
&nbsp;            i = 0;
&nbsp;        else if (i &gt;= x)
&nbsp;            i = x - 1;
&nbsp;    }
&nbsp;    return i;
&nbsp;}
&nbsp;
rnd
&nbsp;/* 1 &lt;= rnd(x) &lt;= x */
&nbsp;int
&nbsp;rnd(x)
&nbsp;register int x;
&nbsp;{
&nbsp;#ifdef BETA
&nbsp;    if (x &lt;= 0) {
&nbsp;        impossible("rnd(%d) attempted", x);
&nbsp;        return 1;
&nbsp;    }
&nbsp;#endif
&nbsp;    x = RND(x) + 1;
&nbsp;    return x;
&nbsp;}
&nbsp;
d
&nbsp;/* d(N,X) == NdX == dX+dX+...+dX N times; n &lt;= d(n,x) &lt;= (n*x) */
&nbsp;int
&nbsp;d(n, x)
&nbsp;register int n, x;
&nbsp;{
&nbsp;    register int tmp = n;
&nbsp;
&nbsp;#ifdef BETA
&nbsp;    if (x &lt; 0 || n &lt; 0 || (x == 0 &amp;&amp; n != 0)) {
&nbsp;        impossible("d(%d,%d) attempted", n, x);
&nbsp;        return 1;
&nbsp;    }
&nbsp;#endif
&nbsp;    while (n--)
&nbsp;        tmp += RND(x);
&nbsp;    return tmp; /* Alea iacta est. -- J.C. */
&nbsp;}
&nbsp;
rne
&nbsp;/* 1 &lt;= rne(x) &lt;= max(u.ulevel/3,5) */
&nbsp;int
&nbsp;rne(x)
&nbsp;register int x;
&nbsp;{
&nbsp;    register int tmp, utmp;
&nbsp;
&nbsp;    utmp = (u.ulevel &lt; 15) ? 5 : u.ulevel / 3;
&nbsp;    tmp = 1;
&nbsp;    while (tmp &lt; utmp &amp;&amp; !rn2(x))
&nbsp;        tmp++;
&nbsp;    return tmp;
&nbsp;
&nbsp;    /* was:
&nbsp;     *  tmp = 1;
&nbsp;     *  while (!rn2(x))
&nbsp;     *    tmp++;
&nbsp;     *  return min(tmp, (u.ulevel &lt; 15) ? 5 : u.ulevel / 3);
&nbsp;     * which is clearer but less efficient and stands a vanishingly
&nbsp;     * small chance of overflowing tmp
&nbsp;     */
&nbsp;}
&nbsp;
rnz
&nbsp;/* rnz: everyone's favorite! */
&nbsp;int
&nbsp;rnz(i)
&nbsp;int i;
&nbsp;{
&nbsp;#ifdef LINT
&nbsp;    int x = i;
&nbsp;    int tmp = 1000;
&nbsp;#else
&nbsp;    register long x = (long) i;
&nbsp;    register long tmp = 1000L;
&nbsp;#endif
&nbsp;
&nbsp;    tmp += rn2(1000);
&nbsp;    tmp *= rne(4);
&nbsp;    if (rn2(2)) {
&nbsp;        x *= tmp;
&nbsp;        x /= 1000;
&nbsp;    } else {
&nbsp;        x *= 1000;
&nbsp;        x /= tmp;
&nbsp;    }
&nbsp;    return (int) x;
&nbsp;}
&nbsp;
&nbsp;/*rnd.c*/