1 /**
2  * \file z-rand.h
3  * \brief A Random Number Generator for Angband
4  *
5  * Copyright (c) 1997 Ben Harrison, Randy Hutson
6  * Copyright (c) 2010 Erik Osheim
7  *
8  * See below for copyright on the WELL random number generator.
9  *
10  * This work is free software; you can redistribute it and/or modify it
11  * under the terms of either:
12  *
13  * a) the GNU General Public License as published by the Free Software
14  *    Foundation, version 2, or
15  *
16  * b) the "Angband licence":
17  *    This software may be copied and distributed for educational, research,
18  *    and not for profit purposes provided that this copyright and statement
19  *    are included in all such copies.  Other copyrights may also apply.
20  */
21 
22 #ifndef INCLUDED_Z_RAND_H
23 #define INCLUDED_Z_RAND_H
24 
25 #include "h-basic.h"
26 
27 /**
28  * Assumed maximum dungeon level.  This value is used for various
29  * calculations involving object and monster creation.  It must be at least
30  * 100. Setting it below 128 may prevent the creation of some objects.
31  */
32 #define MAX_RAND_DEPTH	128
33 
34 /**
35  * A struct representing a strategy for making a dice roll.
36  *
37  * The result will be base + XdY + BONUS, where m_bonus is used in a
38  * tricky way to determine BONUS.
39  */
40 typedef struct random {
41 	int base;
42 	int dice;
43 	int sides;
44 	int m_bonus;
45 } random_value;
46 
47 /**
48  * The number of 32-bit integers worth of seed state.
49  */
50 #define RAND_DEG 32
51 
52 /**
53  * Random aspects used by damcalc, m_bonus_calc, and ranvals
54  */
55 typedef enum {
56 	MINIMISE,
57 	AVERAGE,
58 	MAXIMISE,
59 	EXTREMIFY,
60 	RANDOMISE
61 } aspect;
62 
63 
64 /**
65  * Generates a random signed long integer X where "0 <= X < M" holds.
66  *
67  * The integer X falls along a uniform distribution.
68  */
69 #define randint0(M) ((s32b) Rand_div(M))
70 
71 
72 /**
73  * Generates a random signed long integer X where "1 <= X <= M" holds.
74  *
75  * The integer X falls along a uniform distribution.
76  */
77 #define randint1(M) ((s32b) Rand_div(M) + 1)
78 
79 /**
80  * Generate a random signed long integer X where "A - D <= X <= A + D" holds.
81  * Note that "rand_spread(A, D)" == "rand_range(A - D, A + D)"
82  *
83  * The integer X falls along a uniform distribution.
84  */
85 #define rand_spread(A, D) ((A) + (randint0(1 + (D) + (D))) - (D))
86 
87 /**
88  * Return true one time in `x`.
89  */
90 #define one_in_(x) (!randint0(x))
91 
92 /**
93  * Whether we are currently using the "quick" method or not.
94  */
95 extern bool Rand_quick;
96 
97 /**
98  * The state used by the "quick" RNG.
99  */
100 extern u32b Rand_value;
101 
102 /**
103  * The state used by the "complex" RNG.
104  */
105 extern u32b state_i;
106 extern u32b STATE[RAND_DEG];
107 extern u32b z0;
108 extern u32b z1;
109 extern u32b z2;
110 
111 
112 /**
113  * Initialise the RNG state with the given seed.
114  */
115 void Rand_state_init(u32b seed);
116 
117 /**
118  * Initialise the RNG
119  */
120 void Rand_init(void);
121 
122 /**
123  * Generates a random unsigned long integer X where "0 <= X < M" holds.
124  *
125  * The integer X falls along a uniform distribution.
126  */
127 u32b Rand_div(u32b m);
128 
129 /**
130  * Generate a signed random integer within `stand` standard deviations of
131  * `mean`, following a normal distribution.
132  */
133 s16b Rand_normal(int mean, int stand);
134 
135 /**
136  * Generate a signed random integer following a normal distribution, where
137  * `upper` and `lower` are approximate bounds, and `stand_u and `stand_l` are
138  * ten times the number of standard deviations from the mean we are assuming
139  * the bounds are.
140  */
141 int Rand_sample(int mean, int upper, int lower, int stand_u, int stand_l);
142 
143 /**
144  * Generate a semi-random number from 0 to m-1, in a way that doesn't affect
145  * gameplay.  This is intended for use by external program parts like the
146  * main-*.c files.
147  */
148 u32b Rand_simple(u32b m);
149 
150 /**
151  * Emulate a number `num` of dice rolls of dice with `sides` sides.
152  */
153 int damroll(int num, int sides);
154 
155 /**
156  * Calculation helper function for damroll
157  */
158 int damcalc(int num, int sides, aspect dam_aspect);
159 
160 /**
161  * Generates a random signed long integer X where "A <= X <= B"
162  * Note that "rand_range(0, N-1)" == "randint0(N)".
163  *
164  * The integer X falls along a uniform distribution.
165  */
166 int rand_range(int A, int B);
167 
168 /**
169  * Function used to determine enchantment bonuses, see function header for
170  * a more complete description.
171  */
172 s16b m_bonus(int max, int level);
173 
174 /**
175  * Calculation helper function for m_bonus.
176  */
177 s16b m_bonus_calc(int max, int level, aspect bonus_aspect);
178 
179 /**
180  * Calculation helper function for random_value structs.
181  */
182 int randcalc(random_value v, int level, aspect rand_aspect);
183 
184 /**
185  * Test to see if a value is within a random_value's range.
186  */
187 bool randcalc_valid(random_value v, int test);
188 
189 /**
190  * Test to see if a random_value actually varies.
191  */
192 bool randcalc_varies(random_value v);
193 
194 extern void rand_fix(u32b val);
195 
196 #endif /* INCLUDED_Z_RAND_H */
197