src/core/random_func.hpp
author peter1138
Tue, 22 Jan 2008 07:27:06 +0000
changeset 8374 7a1b6c89cb89
parent 8348 4d7c1c5055b3
child 8434 558c39956ba2
permissions -rw-r--r--
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     1
/* $Id$ */
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     2
8348
4d7c1c5055b3 (svn r11914) -Documentation: fix some @file statement
glx
parents: 8142
diff changeset
     3
/** @file random_func.hpp */
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     4
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     5
#ifndef RANDOM_FUNC_HPP
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     6
#define RANDOM_FUNC_HPP
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     7
8126
c96febd50363 (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 7967
diff changeset
     8
#if defined(__APPLE__)
c96febd50363 (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 7967
diff changeset
     9
	/* Apple already has Random declared */
c96febd50363 (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 7967
diff changeset
    10
	#define Random OTTD_Random
c96febd50363 (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 7967
diff changeset
    11
#endif /* __APPLE__ */
c96febd50363 (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 7967
diff changeset
    12
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    13
/**************
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    14
 * Warning: DO NOT enable this unless you understand what it does
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    15
 *
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    16
 * If enabled, in a network game all randoms will be dumped to the
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    17
 *  stdout if the first client joins (or if you are a client). This
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    18
 *  is to help finding desync problems.
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    19
 *
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    20
 * Warning: DO NOT enable this unless you understand what it does
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    21
 **************/
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    22
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    23
//#define RANDOM_DEBUG
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    24
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    25
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    26
// Enable this to produce higher quality random numbers.
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    27
// Doesn't work with network yet.
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    28
// #define MERSENNE_TWISTER
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    29
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    30
void SetRandomSeed(uint32 seed);
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    31
#ifdef RANDOM_DEBUG
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    32
	#define Random() DoRandom(__LINE__, __FILE__)
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    33
	uint32 DoRandom(int line, const char *file);
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    34
	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    35
	uint DoRandomRange(uint max, int line, const char *file);
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    36
#else
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    37
	uint32 Random();
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    38
	uint RandomRange(uint max);
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    39
#endif
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    40
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    41
uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    42
uint InteractiveRandomRange(uint max);
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    43
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    44
/**
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    45
 * Checks if a given randomize-number is below a given probability.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    46
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    47
 * This function is used to check if the given probability by the fraction of (a/b)
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    48
 * is greater than low 16 bits of the given randomize-number v.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    49
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    50
 * Do not use this function twice on the same random 16 bits as it will yield
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    51
 * the same result. One can use a random number for two calls to Chance16I,
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    52
 * where one call sends the low 16 bits and the other the high 16 bits.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    53
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    54
 * @param a The numerator of the fraction
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    55
 * @param b The denominator of the fraction, must of course not be null
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    56
 * @param r The given randomize-number
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    57
 * @return True if v is less or equals (a/b)
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    58
 */
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    59
static inline bool Chance16I(const uint a, const uint b, const uint32 r)
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    60
{
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    61
	assert(b != 0);
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    62
	return (uint16)r < (uint16)((a << 16) / b);
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    63
}
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    64
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    65
/**
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    66
 * Flips a coin with a given probability.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    67
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    68
 * This macro can be used to get true or false randomized according to a
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    69
 * given probability. The parameter a and b create a percent value with
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    70
 * (a/b). The macro returns true in (a/b) percent.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    71
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    72
 * @see Chance16I()
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    73
 * @param a The numerator of the fraction
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    74
 * @param b The denominator of the fraction
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    75
 * @return True in (a/b) percent
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    76
 */
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    77
static inline bool Chance16(const uint a, const uint b)
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    78
{
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    79
	return Chance16I(a, b, Random());
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    80
}
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    81
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    82
/**
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    83
 * Flips a coin with a given probability and saves the randomize-number in a variable.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    84
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    85
 * This function uses the same parameters as Chance16. The third parameter
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    86
 * must be a variable the randomize-number from Random() is saved in.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    87
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    88
 * The low 16 bits of r will already be used and can therefor not be passed to
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    89
 * Chance16I. One can only send the high 16 bits to Chance16I.
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    90
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    91
 * @see Chance16I()
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    92
 * @param a The numerator of the fraction
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    93
 * @param b The denominator of the fraction
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    94
 * @param r The variable to save the randomize-number from Random()
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    95
 * @return True in (a/b) percent
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    96
 */
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    97
static inline bool Chance16R(const uint a, const uint b, uint32 &r)
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    98
{
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
    99
	r = Random();
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   100
	return Chance16I(a, b, r);
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   101
}
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   102
8142
837f47089a7c (svn r11704) -Codechange: remove another bunch of useless includes.
rubidium
parents: 8126
diff changeset
   103
extern uint32 _random_seeds[2][2];
837f47089a7c (svn r11704) -Codechange: remove another bunch of useless includes.
rubidium
parents: 8126
diff changeset
   104
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
   105
#endif /* RANDOM_FUNC_HPP */