Difference between revisions of "Source:NetHack 3.6.1/src/rnd.c"
Jump to navigation
Jump to search
(beep boop) |
m (beep boop) |
||
(One intermediate revision by the same user not shown) | |||
Line 2: | Line 2: | ||
Below is the full text to '''rnd.c''' from the [[NetHack 3.6.1 source code|source code]] of [[NetHack 3.6.1]]. To link to a particular line, write [[Source:NetHack 3.6.1/src/rnd.c#line123|<nowiki>[[Source:NetHack 3.6.1/src/rnd.c#line123]]</nowiki>]], for example. | Below is the full text to '''rnd.c''' from the [[NetHack 3.6.1 source code|source code]] of [[NetHack 3.6.1]]. To link to a particular line, write [[Source:NetHack 3.6.1/src/rnd.c#line123|<nowiki>[[Source:NetHack 3.6.1/src/rnd.c#line123]]</nowiki>]], for example. | ||
− | + | == Top of file == | |
+ | |||
<span id="line1">1. /* NetHack 3.6 rnd.c $NHDT-Date: 1524689470 2018/04/25 20:51:10 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.18 $ */</span> | <span id="line1">1. /* NetHack 3.6 rnd.c $NHDT-Date: 1524689470 2018/04/25 20:51:10 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.18 $ */</span> | ||
<span id="line2">2. /* Copyright (c) 2004 by Robert Patrick Rankin */</span> | <span id="line2">2. /* Copyright (c) 2004 by Robert Patrick Rankin */</span> | ||
<span id="line3">3. /* NetHack may be freely redistributed. See license for details. */</span> | <span id="line3">3. /* NetHack may be freely redistributed. See license for details. */</span> | ||
+ | |||
+ | {{NGPL}} | ||
+ | |||
<span id="line4">4. </span> | <span id="line4">4. </span> | ||
<span id="line5">5. #include "hack.h"</span> | <span id="line5">5. #include "hack.h"</span> | ||
Line 22: | Line 26: | ||
<span id="line18">18. #endif /* LINT */</span> | <span id="line18">18. #endif /* LINT */</span> | ||
<span id="line19">19. </span> | <span id="line19">19. </span> | ||
+ | |||
+ | == rn2 == | ||
+ | |||
<span id="line20">20. /* 0 <= rn2(x) < x */</span> | <span id="line20">20. /* 0 <= rn2(x) < x */</span> | ||
<span id="line21">21. int</span> | <span id="line21">21. int</span> | ||
Line 39: | Line 46: | ||
<span id="line35">35. }</span> | <span id="line35">35. }</span> | ||
<span id="line36">36. </span> | <span id="line36">36. </span> | ||
+ | |||
+ | == rnl == | ||
+ | |||
<span id="line37">37. /* 0 <= rnl(x) < x; sometimes subtracting Luck;</span> | <span id="line37">37. /* 0 <= rnl(x) < x; sometimes subtracting Luck;</span> | ||
<span id="line38">38. good luck approaches 0, bad luck approaches (x-1) */</span> | <span id="line38">38. good luck approaches 0, bad luck approaches (x-1) */</span> | ||
Line 84: | Line 94: | ||
<span id="line80">80. }</span> | <span id="line80">80. }</span> | ||
<span id="line81">81. </span> | <span id="line81">81. </span> | ||
+ | |||
+ | == rnd == | ||
+ | |||
<span id="line82">82. /* 1 <= rnd(x) <= x */</span> | <span id="line82">82. /* 1 <= rnd(x) <= x */</span> | ||
<span id="line83">83. int</span> | <span id="line83">83. int</span> | ||
Line 99: | Line 112: | ||
<span id="line95">95. }</span> | <span id="line95">95. }</span> | ||
<span id="line96">96. </span> | <span id="line96">96. </span> | ||
+ | |||
+ | == d == | ||
+ | |||
<span id="line97">97. /* d(N,X) == NdX == dX+dX+...+dX N times; n <= d(n,x) <= (n*x) */</span> | <span id="line97">97. /* d(N,X) == NdX == dX+dX+...+dX N times; n <= d(n,x) <= (n*x) */</span> | ||
<span id="line98">98. int</span> | <span id="line98">98. int</span> | ||
Line 117: | Line 133: | ||
<span id="line113">113. }</span> | <span id="line113">113. }</span> | ||
<span id="line114">114. </span> | <span id="line114">114. </span> | ||
+ | |||
+ | == rne == | ||
+ | |||
<span id="line115">115. /* 1 <= rne(x) <= max(u.ulevel/3,5) */</span> | <span id="line115">115. /* 1 <= rne(x) <= max(u.ulevel/3,5) */</span> | ||
<span id="line116">116. int</span> | <span id="line116">116. int</span> | ||
Line 140: | Line 159: | ||
<span id="line136">136. }</span> | <span id="line136">136. }</span> | ||
<span id="line137">137. </span> | <span id="line137">137. </span> | ||
+ | |||
+ | == rnz == | ||
+ | |||
<span id="line138">138. /* rnz: everyone's favorite! */</span> | <span id="line138">138. /* rnz: everyone's favorite! */</span> | ||
<span id="line139">139. int</span> | <span id="line139">139. int</span> |
Latest revision as of 00:20, 18 May 2018
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.
Top of file
/* NetHack 3.6 rnd.c $NHDT-Date: 1524689470 2018/04/25 20:51:10 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.18 $ */
/* Copyright (c) 2004 by Robert Patrick Rankin */
/* 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.
#include "hack.h"
/* "Rand()"s definition is determined by [OS]conf.h */
#if defined(LINT) && defined(UNIX) /* rand() is long... */
extern int NDECL(rand);
#define RND(x) (rand() % x)
#else /* LINT */
#if defined(UNIX) || defined(RANDOM)
#define RND(x) ((int) (Rand() % (long) (x)))
#else
/* Good luck: the bottom order bits are cyclic. */
#define RND(x) ((int) ((Rand() >> 3) % (x)))
#endif
#endif /* LINT */
rn2
/* 0 <= rn2(x) < x */
int
rn2(x)
register int x;
{
#ifdef BETA
if (x <= 0) {
impossible("rn2(%d) attempted", x);
return 0;
}
x = RND(x);
return x;
#else
return RND(x);
#endif
}
rnl
/* 0 <= rnl(x) < x; sometimes subtracting Luck;
good luck approaches 0, bad luck approaches (x-1) */
int
rnl(x)
register int x;
{
register int i, adjustment;
#ifdef BETA
if (x <= 0) {
impossible("rnl(%d) attempted", x);
return 0;
}
#endif
adjustment = Luck;
if (x <= 15) {
/* for small ranges, use Luck/3 (rounded away from 0);
also guard against architecture-specific differences
of integer division involving negative values */
adjustment = (abs(adjustment) + 1) / 3 * sgn(adjustment);
/*
* 11..13 -> 4
* 8..10 -> 3
* 5.. 7 -> 2
* 2.. 4 -> 1
* -1,0,1 -> 0 (no adjustment)
* -4..-2 -> -1
* -7..-5 -> -2
* -10..-8 -> -3
* -13..-11-> -4
*/
}
i = RND(x);
if (adjustment && rn2(37 + abs(adjustment))) {
i -= adjustment;
if (i < 0)
i = 0;
else if (i >= x)
i = x - 1;
}
return i;
}
rnd
/* 1 <= rnd(x) <= x */
int
rnd(x)
register int x;
{
#ifdef BETA
if (x <= 0) {
impossible("rnd(%d) attempted", x);
return 1;
}
#endif
x = RND(x) + 1;
return x;
}
d
/* d(N,X) == NdX == dX+dX+...+dX N times; n <= d(n,x) <= (n*x) */
int
d(n, x)
register int n, x;
{
register int tmp = n;
#ifdef BETA
if (x < 0 || n < 0 || (x == 0 && n != 0)) {
impossible("d(%d,%d) attempted", n, x);
return 1;
}
#endif
while (n--)
tmp += RND(x);
return tmp; /* Alea iacta est. -- J.C. */
}
rne
/* 1 <= rne(x) <= max(u.ulevel/3,5) */
int
rne(x)
register int x;
{
register int tmp, utmp;
utmp = (u.ulevel < 15) ? 5 : u.ulevel / 3;
tmp = 1;
while (tmp < utmp && !rn2(x))
tmp++;
return tmp;
/* was:
* tmp = 1;
* while (!rn2(x))
* tmp++;
* return min(tmp, (u.ulevel < 15) ? 5 : u.ulevel / 3);
* which is clearer but less efficient and stands a vanishingly
* small chance of overflowing tmp
*/
}
rnz
/* rnz: everyone's favorite! */
int
rnz(i)
int i;
{
#ifdef LINT
int x = i;
int tmp = 1000;
#else
register long x = (long) i;
register long tmp = 1000L;
#endif
tmp += rn2(1000);
tmp *= rne(4);
if (rn2(2)) {
x *= tmp;
x /= 1000;
} else {
x *= 1000;
x /= tmp;
}
return (int) x;
}
/*rnd.c*/