Source:NetHack 3.1.0/rnd.c
Jump to navigation
Jump to search
Below is the full text to rnd.c from the source code of NetHack 3.1.0. To link to a particular line, write [[NetHack 3.1.0/rnd.c#line123]], for example.
Warning! This is the source code from an old release. For the latest release, see Source code
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. /* SCCS Id: @(#)rnd.c 3.1 90/22/02 2. /* NetHack may be freely redistributed. See license for details. */ 3. 4. #include "hack.h" 5. 6. #if defined(LINT) && defined(UNIX) /* rand() is long... */ 7. extern int NDECL(rand); 8. #define RND(x) (rand() % x) 9. #else /* LINT */ 10. /* rand() is either random() or lrand48() - see config.h. */ 11. #ifdef UNIX 12. #define RND(x) (int)(Rand() % (long)(x)) 13. #else 14. /* Good luck: the bottom order bits are cyclic. */ 15. #define RND(x) (int)((Rand()>>3) % (x)) 16. #endif 17. #endif /* LINT */ 18. 19. #ifdef OVL0 20. 21. int 22. rn2(x) /* 0 <= rn2(x) < x */ 23. register int x; 24. { 25. #ifdef DEBUG 26. if (x == 0) { 27. impossible("rn2(0) attempted"); 28. return(0); 29. } 30. #endif 31. return(RND(x)); 32. } 33. 34. #endif /* OVL0 */ 35. #ifdef OVLB 36. 37. int 38. rnl(x) /* 0 <= rnl(x) < x; sometimes subtracting Luck */ 39. register int x; /* good luck approaches 0, bad luck approaches (x-1) */ 40. { 41. register int i; 42. 43. #ifdef DEBUG 44. if (x == 0) { 45. impossible("rnl(0) attempted"); 46. return(0); 47. } 48. #endif 49. i = RND(x); 50. 51. if (Luck && rn2(50 - Luck)) { 52. i -= (x <= 15 && Luck >= -5 ? Luck/3 : Luck); 53. if (i < 0) i = 0; 54. else if (i >= x) i = x-1; 55. } 56. 57. return i; 58. } 59. 60. #endif /* OVLB */ 61. #ifdef OVL0 62. 63. int 64. rnd(x) /* 1 <= rnd(x) <= x */ 65. register int x; 66. { 67. #ifdef DEBUG 68. if (x == 0) { 69. impossible("rnd(0) attempted"); 70. return(1); 71. } 72. #endif 73. return(RND(x)+1); 74. } 75. 76. #endif /* OVL0 */ 77. #ifdef OVL1 78. 79. int 80. d(n,x) /* n <= d(n,x) <= (n*x) */ 81. register int n, x; 82. { 83. register int tmp = n; 84. 85. #ifdef DEBUG 86. if (x == 0) { 87. impossible("d(n,0) attempted"); 88. return(1); 89. } 90. #endif 91. while(n--) tmp += RND(x); 92. return(tmp); /* Alea iacta est. -- J.C. */ 93. } 94. 95. #endif /* OVL1 */ 96. #ifdef OVLB 97. 98. int 99. rne(x) /* by stewr 870807 */ 100. register int x; 101. { 102. register int tmp = 1; 103. while(!rn2(x)) tmp++; 104. return(min(tmp,(u.ulevel < 15) ? 5 : (int)u.ulevel/3)); 105. } 106. 107. int 108. rnz(i) 109. int i; 110. { 111. #ifdef LINT 112. int x = i; 113. int tmp = 1000; 114. #else 115. register long x = i; 116. register long tmp = 1000; 117. #endif 118. tmp += rn2(1000); 119. tmp *= rne(4); 120. if (rn2(2)) { x *= tmp; x /= 1000; } 121. else { x *= 1000; x /= tmp; } 122. return((int)x); 123. } 124. 125. #endif /* OVLB */ 126. 127. /*rnd.c*/