Source:NetHack 3.6.6/src/rnd.c
Jump to navigation
Jump to search
Below is the full text to rnd.c from the source code of NetHack 3.6.6. To link to a particular line, write [[Source:NetHack 3.6.6/src/rnd.c#line12]], for example.
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. /* 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. */ 4. 5. #include "hack.h" 6. 7. #ifdef USE_ISAAC64 8. #include "isaac64.h" 9. 10. #if 0 11. static isaac64_ctx rng_state; 12. #endif 13. 14. struct rnglist_t { 15. int FDECL((*fn), (int)); 16. boolean init; 17. isaac64_ctx rng_state; 18. }; 19. 20. enum { CORE = 0, DISP = 1 }; 21. 22. static struct rnglist_t rnglist[] = { 23. { rn2, FALSE, { 0 } }, /* CORE */ 24. { rn2_on_display_rng, FALSE, { 0 } }, /* DISP */ 25. }; 26. 27. int 28. whichrng(fn) 29. int FDECL((*fn), (int)); 30. { 31. int i; 32. 33. for (i = 0; i < SIZE(rnglist); ++i) 34. if (rnglist[i].fn == fn) 35. return i; 36. return -1; 37. } 38. 39. void 40. init_isaac64(seed, fn) 41. unsigned long seed; 42. int FDECL((*fn), (int)); 43. { 44. unsigned char new_rng_state[sizeof seed]; 45. unsigned i; 46. int rngindx = whichrng(fn); 47. 48. if (rngindx < 0) 49. panic("Bad rng function passed to init_isaac64()."); 50. 51. for (i = 0; i < sizeof seed; i++) { 52. new_rng_state[i] = (unsigned char) (seed & 0xFF); 53. seed >>= 8; 54. } 55. isaac64_init(&rnglist[rngindx].rng_state, new_rng_state, 56. (int) sizeof seed); 57. } 58. 59. static int 60. RND(int x) 61. { 62. return (isaac64_next_uint64(&rnglist[CORE].rng_state) % x); 63. } 64. 65. /* 0 <= rn2(x) < x, but on a different sequence from the "main" rn2; 66. used in cases where the answer doesn't affect gameplay and we don't 67. want to give users easy control over the main RNG sequence. */ 68. int 69. rn2_on_display_rng(x) 70. register int x; 71. { 72. return (isaac64_next_uint64(&rnglist[DISP].rng_state) % x); 73. } 74. 75. #else /* USE_ISAAC64 */ 76. 77. /* "Rand()"s definition is determined by [OS]conf.h */ 78. #if defined(LINT) && defined(UNIX) /* rand() is long... */ 79. extern int NDECL(rand); 80. #define RND(x) (rand() % x) 81. #else /* LINT */ 82. #if defined(UNIX) || defined(RANDOM) 83. #define RND(x) ((int) (Rand() % (long) (x))) 84. #else 85. /* Good luck: the bottom order bits are cyclic. */ 86. #define RND(x) ((int) ((Rand() >> 3) % (x))) 87. #endif 88. #endif /* LINT */ 89. int 90. rn2_on_display_rng(x) 91. register int x; 92. { 93. static unsigned seed = 1; 94. seed *= 2739110765; 95. return (int)((seed >> 16) % (unsigned)x); 96. } 97. #endif /* USE_ISAAC64 */ 98. 99. /* 0 <= rn2(x) < x */ 100. int 101. rn2(x) 102. register int x; 103. { 104. #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) 105. if (x <= 0) { 106. impossible("rn2(%d) attempted", x); 107. return 0; 108. } 109. x = RND(x); 110. return x; 111. #else 112. return RND(x); 113. #endif 114. } 115. 116. /* 0 <= rnl(x) < x; sometimes subtracting Luck; 117. good luck approaches 0, bad luck approaches (x-1) */ 118. int 119. rnl(x) 120. register int x; 121. { 122. register int i, adjustment; 123. 124. #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) 125. if (x <= 0) { 126. impossible("rnl(%d) attempted", x); 127. return 0; 128. } 129. #endif 130. 131. adjustment = Luck; 132. if (x <= 15) { 133. /* for small ranges, use Luck/3 (rounded away from 0); 134. also guard against architecture-specific differences 135. of integer division involving negative values */ 136. adjustment = (abs(adjustment) + 1) / 3 * sgn(adjustment); 137. /* 138. * 11..13 -> 4 139. * 8..10 -> 3 140. * 5.. 7 -> 2 141. * 2.. 4 -> 1 142. * -1,0,1 -> 0 (no adjustment) 143. * -4..-2 -> -1 144. * -7..-5 -> -2 145. * -10..-8 -> -3 146. * -13..-11-> -4 147. */ 148. } 149. 150. i = RND(x); 151. if (adjustment && rn2(37 + abs(adjustment))) { 152. i -= adjustment; 153. if (i < 0) 154. i = 0; 155. else if (i >= x) 156. i = x - 1; 157. } 158. return i; 159. } 160. 161. /* 1 <= rnd(x) <= x */ 162. int 163. rnd(x) 164. register int x; 165. { 166. #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) 167. if (x <= 0) { 168. impossible("rnd(%d) attempted", x); 169. return 1; 170. } 171. #endif 172. x = RND(x) + 1; 173. return x; 174. } 175. 176. /* d(N,X) == NdX == dX+dX+...+dX N times; n <= d(n,x) <= (n*x) */ 177. int 178. d(n, x) 179. register int n, x; 180. { 181. register int tmp = n; 182. 183. #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) 184. if (x < 0 || n < 0 || (x == 0 && n != 0)) { 185. impossible("d(%d,%d) attempted", n, x); 186. return 1; 187. } 188. #endif 189. while (n--) 190. tmp += RND(x); 191. return tmp; /* Alea iacta est. -- J.C. */ 192. } 193. 194. /* 1 <= rne(x) <= max(u.ulevel/3,5) */ 195. int 196. rne(x) 197. register int x; 198. { 199. register int tmp, utmp; 200. 201. utmp = (u.ulevel < 15) ? 5 : u.ulevel / 3; 202. tmp = 1; 203. while (tmp < utmp && !rn2(x)) 204. tmp++; 205. return tmp; 206. 207. /* was: 208. * tmp = 1; 209. * while (!rn2(x)) 210. * tmp++; 211. * return min(tmp, (u.ulevel < 15) ? 5 : u.ulevel / 3); 212. * which is clearer but less efficient and stands a vanishingly 213. * small chance of overflowing tmp 214. */ 215. } 216. 217. /* rnz: everyone's favorite! */ 218. int 219. rnz(i) 220. int i; 221. { 222. #ifdef LINT 223. int x = i; 224. int tmp = 1000; 225. #else 226. register long x = (long) i; 227. register long tmp = 1000L; 228. #endif 229. 230. tmp += rn2(1000); 231. tmp *= rne(4); 232. if (rn2(2)) { 233. x *= tmp; 234. x /= 1000; 235. } else { 236. x *= 1000; 237. x /= tmp; 238. } 239. return (int) x; 240. } 241. 242. /*rnd.c*/