src/core/random_func.hpp
author Tero Marttila <terom@fixme.fi>
Tue, 22 Jul 2008 21:51:14 +0300
changeset 11180 982e9f814f97
parent 11050 091271fcfbb9
permissions -rw-r--r--
scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     1
/* $Id$ */
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     2
10429
1b99254f9607 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9288
diff changeset
     3
/** @file random_func.hpp Pseudo random number generator. */
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     4
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     5
#ifndef RANDOM_FUNC_HPP
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     6
#define RANDOM_FUNC_HPP
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     7
8622
b917f0b0dd6b (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 8463
diff changeset
     8
#if defined(__APPLE__)
b917f0b0dd6b (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 8463
diff changeset
     9
	/* Apple already has Random declared */
b917f0b0dd6b (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 8463
diff changeset
    10
	#define Random OTTD_Random
b917f0b0dd6b (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 8463
diff changeset
    11
#endif /* __APPLE__ */
b917f0b0dd6b (svn r11687) -Codechange: move some defines to a better place
skidd13
parents: 8463
diff changeset
    12
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    13
/**************
68fb2ccbce06 (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
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    15
 *
68fb2ccbce06 (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
68fb2ccbce06 (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
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    18
 *  is to help finding desync problems.
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    19
 *
68fb2ccbce06 (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
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    21
 **************/
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    22
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    23
//#define RANDOM_DEBUG
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    24
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    25
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    26
/**
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    27
 * Structure to encapsulate the pseudo random number generators.
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    28
 */
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    29
struct Randomizer {
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    30
	/** The state of the randomizer */
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    31
	uint32 state[2];
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    32
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    33
	/**
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    34
	 * Generate the next pseudo random number
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    35
	 * @return the random number
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    36
	 */
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    37
	uint32 Next();
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    38
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    39
	/**
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    40
	 * Generate the next pseudo random number scaled to max
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    41
	 * @param max the maximum value of the returned random number
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    42
	 * @return the random number
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    43
	 */
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    44
	uint32 Next(uint16 max);
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    45
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    46
	/**
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    47
	 * (Re)set the state of the random number generator.
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    48
	 * @param seed the new state
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    49
	 */
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    50
	void SetSeed(uint32 seed);
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    51
};
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    52
extern Randomizer _random; ///< Random used in the game state calculations
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    53
extern Randomizer _interactive_random; ///< Random used every else where is does not (directly) influence the game state
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8844
diff changeset
    54
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    55
void SetRandomSeed(uint32 seed);
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    56
#ifdef RANDOM_DEBUG
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    57
	#define Random() DoRandom(__LINE__, __FILE__)
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    58
	uint32 DoRandom(int line, const char *file);
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    59
	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    60
	uint DoRandomRange(uint max, int line, const char *file);
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    61
#else
11050
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    62
	static FORCEINLINE uint32 Random()
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    63
	{
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    64
		return _random.Next();
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    65
	}
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    66
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    67
	static FORCEINLINE uint32 RandomRange(uint16 max)
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    68
	{
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    69
		return _random.Next(max);
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    70
	}
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    71
#endif
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    72
11050
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    73
static FORCEINLINE uint32 InteractiveRandom()
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    74
{
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    75
	return _interactive_random.Next();
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    76
}
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    77
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    78
static FORCEINLINE uint32 InteractiveRandomRange(uint16 max)
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    79
{
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    80
	return _interactive_random.Next(max);
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
    81
}
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    82
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    83
/**
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    84
 * Checks if a given randomize-number is below a given probability.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    85
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    86
 * This function is used to check if the given probability by the fraction of (a/b)
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    87
 * is greater than low 16 bits of the given randomize-number v.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    88
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    89
 * Do not use this function twice on the same random 16 bits as it will yield
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    90
 * the same result. One can use a random number for two calls to Chance16I,
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    91
 * where one call sends the low 16 bits and the other the high 16 bits.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    92
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    93
 * @param a The numerator of the fraction
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    94
 * @param b The denominator of the fraction, must of course not be null
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    95
 * @param r The given randomize-number
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    96
 * @return True if v is less or equals (a/b)
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    97
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10429
diff changeset
    98
static FORCEINLINE bool Chance16I(const uint a, const uint b, const uint32 r)
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    99
{
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   100
	assert(b != 0);
9071
6790e9c11b5d (svn r12156) -Fix (r11454): Chance16I was now biased towards zero - round to nearest now
smatz
parents: 8930
diff changeset
   101
	return (uint16)r < (uint16)(((a << 16) + b / 2) / b);
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   102
}
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   103
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   104
/**
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   105
 * Flips a coin with a given probability.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   106
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   107
 * This macro can be used to get true or false randomized according to a
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   108
 * given probability. The parameter a and b create a percent value with
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   109
 * (a/b). The macro returns true in (a/b) percent.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   110
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   111
 * @see Chance16I()
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   112
 * @param a The numerator of the fraction
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   113
 * @param b The denominator of the fraction
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   114
 * @return True in (a/b) percent
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   115
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10429
diff changeset
   116
static FORCEINLINE bool Chance16(const uint a, const uint b)
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   117
{
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   118
	return Chance16I(a, b, Random());
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   119
}
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   120
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   121
/**
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   122
 * Flips a coin with a given probability and saves the randomize-number in a variable.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   123
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   124
 * This function uses the same parameters as Chance16. The third parameter
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   125
 * must be a variable the randomize-number from Random() is saved in.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   126
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   127
 * The low 16 bits of r will already be used and can therefor not be passed to
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   128
 * Chance16I. One can only send the high 16 bits to Chance16I.
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   129
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   130
 * @see Chance16I()
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   131
 * @param a The numerator of the fraction
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   132
 * @param b The denominator of the fraction
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   133
 * @param r The variable to save the randomize-number from Random()
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   134
 * @return True in (a/b) percent
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   135
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10429
diff changeset
   136
static FORCEINLINE bool Chance16R(const uint a, const uint b, uint32 &r)
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   137
{
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   138
	r = Random();
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   139
	return Chance16I(a, b, r);
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   140
}
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
   141
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
   142
#endif /* RANDOM_FUNC_HPP */