Source:NetHack 3.6.1/src/rnd.c

From NetHackWiki
(Redirected from Source:Ref/rnl)
Jump to: navigation, search

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

  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 $ */
  2.  /*      Copyright (c) 2004 by Robert Patrick Rankin               */
  3.  /* 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.

  1.  
  2.  #include "hack.h"
  3.  
  4.  /* "Rand()"s definition is determined by [OS]conf.h */
  5.  #if defined(LINT) && defined(UNIX) /* rand() is long... */
  6.  extern int NDECL(rand);
  7.  #define RND(x) (rand() % x)
  8.  #else /* LINT */
  9.  #if defined(UNIX) || defined(RANDOM)
  10.  #define RND(x) ((int) (Rand() % (long) (x)))
  11.  #else
  12.  /* Good luck: the bottom order bits are cyclic. */
  13.  #define RND(x) ((int) ((Rand() >> 3) % (x)))
  14.  #endif
  15.  #endif /* LINT */
  16.  

rn2

  1.  /* 0 <= rn2(x) < x */
  2.  int
  3.  rn2(x)
  4.  register int x;
  5.  {
  6.  #ifdef BETA
  7.      if (x <= 0) {
  8.          impossible("rn2(%d) attempted", x);
  9.          return 0;
  10.      }
  11.      x = RND(x);
  12.      return x;
  13.  #else
  14.      return RND(x);
  15.  #endif
  16.  }
  17.  

rnl

  1.  /* 0 <= rnl(x) < x; sometimes subtracting Luck;
  2.     good luck approaches 0, bad luck approaches (x-1) */
  3.  int
  4.  rnl(x)
  5.  register int x;
  6.  {
  7.      register int i, adjustment;
  8.  
  9.  #ifdef BETA
  10.      if (x <= 0) {
  11.          impossible("rnl(%d) attempted", x);
  12.          return 0;
  13.      }
  14.  #endif
  15.  
  16.      adjustment = Luck;
  17.      if (x <= 15) {
  18.          /* for small ranges, use Luck/3 (rounded away from 0);
  19.             also guard against architecture-specific differences
  20.             of integer division involving negative values */
  21.          adjustment = (abs(adjustment) + 1) / 3 * sgn(adjustment);
  22.          /*
  23.           *       11..13 ->  4
  24.           *        8..10 ->  3
  25.           *        5.. 7 ->  2
  26.           *        2.. 4 ->  1
  27.           *       -1,0,1 ->  0 (no adjustment)
  28.           *       -4..-2 -> -1
  29.           *       -7..-5 -> -2
  30.           *      -10..-8 -> -3
  31.           *      -13..-11-> -4
  32.           */
  33.      }
  34.  
  35.      i = RND(x);
  36.      if (adjustment && rn2(37 + abs(adjustment))) {
  37.          i -= adjustment;
  38.          if (i < 0)
  39.              i = 0;
  40.          else if (i >= x)
  41.              i = x - 1;
  42.      }
  43.      return i;
  44.  }
  45.  

rnd

  1.  /* 1 <= rnd(x) <= x */
  2.  int
  3.  rnd(x)
  4.  register int x;
  5.  {
  6.  #ifdef BETA
  7.      if (x <= 0) {
  8.          impossible("rnd(%d) attempted", x);
  9.          return 1;
  10.      }
  11.  #endif
  12.      x = RND(x) + 1;
  13.      return x;
  14.  }
  15.  

d

  1.  /* d(N,X) == NdX == dX+dX+...+dX N times; n <= d(n,x) <= (n*x) */
  2.  int
  3.  d(n, x)
  4.  register int n, x;
  5.  {
  6.      register int tmp = n;
  7.  
  8.  #ifdef BETA
  9.      if (x < 0 || n < 0 || (x == 0 && n != 0)) {
  10.          impossible("d(%d,%d) attempted", n, x);
  11.          return 1;
  12.      }
  13.  #endif
  14.      while (n--)
  15.          tmp += RND(x);
  16.      return tmp; /* Alea iacta est. -- J.C. */
  17.  }
  18.  

rne

  1.  /* 1 <= rne(x) <= max(u.ulevel/3,5) */
  2.  int
  3.  rne(x)
  4.  register int x;
  5.  {
  6.      register int tmp, utmp;
  7.  
  8.      utmp = (u.ulevel < 15) ? 5 : u.ulevel / 3;
  9.      tmp = 1;
  10.      while (tmp < utmp && !rn2(x))
  11.          tmp++;
  12.      return tmp;
  13.  
  14.      /* was:
  15.       *  tmp = 1;
  16.       *  while (!rn2(x))
  17.       *    tmp++;
  18.       *  return min(tmp, (u.ulevel < 15) ? 5 : u.ulevel / 3);
  19.       * which is clearer but less efficient and stands a vanishingly
  20.       * small chance of overflowing tmp
  21.       */
  22.  }
  23.  

rnz

  1.  /* rnz: everyone's favorite! */
  2.  int
  3.  rnz(i)
  4.  int i;
  5.  {
  6.  #ifdef LINT
  7.      int x = i;
  8.      int tmp = 1000;
  9.  #else
  10.      register long x = (long) i;
  11.      register long tmp = 1000L;
  12.  #endif
  13.  
  14.      tmp += rn2(1000);
  15.      tmp *= rne(4);
  16.      if (rn2(2)) {
  17.          x *= tmp;
  18.          x /= 1000;
  19.      } else {
  20.          x *= 1000;
  21.          x /= tmp;
  22.      }
  23.      return (int) x;
  24.  }
  25.  
  26.  /*rnd.c*/