src/core/random_func.hpp
author rubidium
Sun, 03 Feb 2008 20:34:26 +0000
branchNewGRF_ports
changeset 6877 889301acc299
parent 6872 1c4a4a609f85
child 6878 7d1ff2f621c7
permissions -rw-r--r--
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
     1
/* $Id$ */
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
     2
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
     3
/** @file random_func.hpp */
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
     4
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
     5
#ifndef RANDOM_FUNC_HPP
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
     6
#define RANDOM_FUNC_HPP
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
     7
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
     8
#if defined(__APPLE__)
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
     9
	/* Apple already has Random declared */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    10
	#define Random OTTD_Random
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    11
#endif /* __APPLE__ */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    12
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    13
/**************
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    14
 * Warning: DO NOT enable this unless you understand what it does
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    15
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    16
 * If enabled, in a network game all randoms will be dumped to the
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    17
 *  stdout if the first client joins (or if you are a client). This
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    18
 *  is to help finding desync problems.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    19
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    20
 * Warning: DO NOT enable this unless you understand what it does
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    21
 **************/
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    22
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    23
//#define RANDOM_DEBUG
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    24
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    25
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    26
// Enable this to produce higher quality random numbers.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    27
// Doesn't work with network yet.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    28
// #define MERSENNE_TWISTER
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    29
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    30
/**
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    31
 * Structure to encapsulate the pseudo random number generators.
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    32
 */
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    33
struct Randomizer {
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    34
	/** The state of the randomizer */
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    35
	uint32 state[2];
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    36
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    37
	/**
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    38
	 * Generate the next pseudo random number
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    39
	 * @return the random number
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    40
	 */
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    41
	uint32 Next();
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    42
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    43
	/**
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    44
	 * Generate the next pseudo random number scaled to max
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    45
	 * @param max the maximum value of the returned random number
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    46
	 * @return the random number
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    47
	 */
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    48
	uint32 Next(uint16 max);
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    49
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    50
	/**
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    51
	 * (Re)set the state of the random number generator.
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    52
	 * @param seed the new state
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    53
	 */
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    54
	void SetSeed(uint32 seed);
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    55
};
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    56
extern Randomizer _random; ///< Random used in the game state calculations
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    57
extern Randomizer _interactive_random; ///< Random used every else where is does not (directly) influence the game state
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    58
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    59
void SetRandomSeed(uint32 seed);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    60
#ifdef RANDOM_DEBUG
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    61
	#define Random() DoRandom(__LINE__, __FILE__)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    62
	uint32 DoRandom(int line, const char *file);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    63
	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    64
	uint DoRandomRange(uint max, int line, const char *file);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    65
#else
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    66
	static inline uint32 Random() { return _random.Next(); }
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    67
	static inline uint32 RandomRange(uint16 max) { return _random.Next(max); }
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    68
#endif
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    69
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    70
static inline uint32 InteractiveRandom() { return _interactive_random.Next(); }
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
    71
static inline uint32 InteractiveRandomRange(uint16 max) { return _interactive_random.Next(max); }
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    72
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    73
/**
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    74
 * Checks if a given randomize-number is below a given probability.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    75
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    76
 * This function is used to check if the given probability by the fraction of (a/b)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    77
 * is greater than low 16 bits of the given randomize-number v.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    78
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    79
 * Do not use this function twice on the same random 16 bits as it will yield
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    80
 * the same result. One can use a random number for two calls to Chance16I,
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    81
 * where one call sends the low 16 bits and the other the high 16 bits.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    82
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    83
 * @param a The numerator of the fraction
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    84
 * @param b The denominator of the fraction, must of course not be null
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    85
 * @param r The given randomize-number
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    86
 * @return True if v is less or equals (a/b)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    87
 */
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    88
static inline bool Chance16I(const uint a, const uint b, const uint32 r)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    89
{
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    90
	assert(b != 0);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    91
	return (uint16)r < (uint16)((a << 16) / b);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    92
}
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    93
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    94
/**
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    95
 * Flips a coin with a given probability.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    96
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    97
 * This macro can be used to get true or false randomized according to a
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    98
 * given probability. The parameter a and b create a percent value with
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
    99
 * (a/b). The macro returns true in (a/b) percent.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   100
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   101
 * @see Chance16I()
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   102
 * @param a The numerator of the fraction
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   103
 * @param b The denominator of the fraction
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   104
 * @return True in (a/b) percent
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   105
 */
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   106
static inline bool Chance16(const uint a, const uint b)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   107
{
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   108
	return Chance16I(a, b, Random());
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   109
}
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   110
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   111
/**
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   112
 * Flips a coin with a given probability and saves the randomize-number in a variable.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   113
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   114
 * This function uses the same parameters as Chance16. The third parameter
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   115
 * must be a variable the randomize-number from Random() is saved in.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   116
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   117
 * The low 16 bits of r will already be used and can therefor not be passed to
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   118
 * Chance16I. One can only send the high 16 bits to Chance16I.
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   119
 *
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   120
 * @see Chance16I()
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   121
 * @param a The numerator of the fraction
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   122
 * @param b The denominator of the fraction
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   123
 * @param r The variable to save the randomize-number from Random()
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   124
 * @return True in (a/b) percent
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   125
 */
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   126
static inline bool Chance16R(const uint a, const uint b, uint32 &r)
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   127
{
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   128
	r = Random();
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   129
	return Chance16I(a, b, r);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   130
}
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   131
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
diff changeset
   132
#endif /* RANDOM_FUNC_HPP */