4 * Copyright (c) 1997 Ben Harrison, and others
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies. Other copyrights may also apply.
11 #ifndef INCLUDED_Z_RAND_H
12 #define INCLUDED_Z_RAND_H
14 #include "system/h-basic.h"
16 /**** Available constants ****/
19 * Random Number Generator -- Degree of "complex" RNG -- see "misc.c"
20 * This value is hard-coded at 63 for a wide variety of reasons.
24 /**** Available macros ****/
27 * Generates a random long integer X where O<=X<M.
28 * The integer X falls along a uniform distribution.
29 * For example, if M is 100, you get "percentile dice"
31 #define randint0(M) ((s32b)Rand_div(M))
34 * Generates a random long integer X where A<=X<=B
35 * The integer X falls along a uniform distribution.
36 * Note: rand_range(0,N-1) == randint0(N)
38 #define rand_range(A, B) ((A) + (randint0(1 + (B) - (A))))
41 * Generate a random long integer X where A-D<=X<=A+D
42 * The integer X falls along a uniform distribution.
43 * Note: rand_spread(A,D) == rand_range(A-D,A+D)
45 #define rand_spread(A, D) ((A) + (randint0(1 + (D) + (D))) - (D))
48 * Generate a random long integer X where 1<=X<=M
49 * Also, "correctly" handle the case of M<=1
51 #define randint1(M) (randint0(M) + 1)
54 * Evaluate to TRUE "P" percent of the time
56 #define magik(P) (randint0(100) < (P))
58 #define one_in_(X) (randint0(X) == 0)
61 * Evaluate to TRUE "S" percent of the time
63 #define saving_throw(S) (randint0(100) < (S))
65 extern u16b Rand_place;
66 extern u32b Rand_state[RAND_DEG];
68 void Rand_state_init(void);
69 void Rand_state_set(u32b seed);
70 void Rand_state_backup(u32b *backup_state);
71 void Rand_state_restore(u32b *backup_state);
72 s32b Rand_div(s32b m);
73 s16b randnor(int mean, int stand);
74 s16b damroll(DICE_NUMBER num, DICE_SID sides);
75 s16b maxroll(DICE_NUMBER num, DICE_SID sides);
76 s32b div_round(s32b n, s32b d);
77 s32b Rand_external(s32b m);