src/core/random_func.hpp
author terom@frrb.lan
Fri, 19 Dec 2008 01:38:09 +0200
changeset 10439 50f056aa3024
parent 9928 fa24e759e11d
permissions -rw-r--r--
industries, unmoveables... everything but the landscape
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
9111
48ce04029fe4 (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: 8792
diff changeset
     3
/** @file random_func.hpp Pseudo random number generator. */
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
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    26
/**
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    27
 * Structure to encapsulate the pseudo random number generators.
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    28
 */
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    29
struct Randomizer {
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    30
	/** The state of the randomizer */
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    31
	uint32 state[2];
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    32
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    33
	/**
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    34
	 * Generate the next pseudo random number
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    35
	 * @return the random number
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    36
	 */
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    37
	uint32 Next();
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    38
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    39
	/**
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    40
	 * Generate the next pseudo random number scaled to max
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    41
	 * @param max the maximum value of the returned random number
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    42
	 * @return the random number
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    43
	 */
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    44
	uint32 Next(uint16 max);
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    45
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    46
	/**
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    47
	 * (Re)set the state of the random number generator.
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    48
	 * @param seed the new state
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    49
	 */
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    50
	void SetSeed(uint32 seed);
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    51
};
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    52
extern Randomizer _random; ///< Random used in the game state calculations
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    53
extern Randomizer _interactive_random; ///< Random used every else where is does not (directly) influence the game state
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8348
diff changeset
    54
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    55
/** Stores the state of all random number generators */
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    56
struct SavedRandomSeeds {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    57
	Randomizer random;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    58
	Randomizer interactive_random;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    59
};
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    60
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    61
/** Saves the current seeds
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    62
 * @param storage Storage for saving
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    63
 */
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    64
static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    65
{
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    66
	storage->random = _random;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    67
	storage->interactive_random = _interactive_random;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    68
}
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    69
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    70
/** Restores previously saved seeds
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    71
 * @param storage Storage where SaveRandomSeeds() stored th seeds
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    72
 */
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    73
static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    74
{
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    75
	_random = storage.random;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    76
	_interactive_random = storage.interactive_random;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    77
}
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9576
diff changeset
    78
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    79
void SetRandomSeed(uint32 seed);
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    80
#ifdef RANDOM_DEBUG
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    81
	#define Random() DoRandom(__LINE__, __FILE__)
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    82
	uint32 DoRandom(int line, const char *file);
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    83
	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    84
	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
    85
#else
9576
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    86
	static FORCEINLINE uint32 Random()
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    87
	{
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    88
		return _random.Next();
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    89
	}
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    90
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    91
	static FORCEINLINE uint32 RandomRange(uint16 max)
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    92
	{
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    93
		return _random.Next(max);
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    94
	}
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    95
#endif
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    96
9576
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    97
static FORCEINLINE uint32 InteractiveRandom()
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    98
{
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    99
	return _interactive_random.Next();
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   100
}
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   101
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   102
static FORCEINLINE uint32 InteractiveRandomRange(uint16 max)
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   103
{
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   104
	return _interactive_random.Next(max);
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   105
}
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
   106
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   107
/**
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   108
 * 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
   109
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   110
 * 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
   111
 * 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
   112
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   113
 * 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
   114
 * 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
   115
 * 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
   116
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   117
 * @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
   118
 * @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
   119
 * @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
   120
 * @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
   121
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9111
diff changeset
   122
static FORCEINLINE bool Chance16I(const uint a, const uint b, const uint32 r)
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   123
{
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   124
	assert(b != 0);
8575
fbda4db54309 (svn r12156) -Fix (r11454): Chance16I was now biased towards zero - round to nearest now
smatz
parents: 8434
diff changeset
   125
	return (uint16)r < (uint16)(((a << 16) + b / 2) / b);
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   126
}
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   127
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   128
/**
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   129
 * 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
   130
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   131
 * 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
   132
 * 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
   133
 * (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
   134
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   135
 * @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
   136
 * @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
   137
 * @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
   138
 * @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
   139
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9111
diff changeset
   140
static FORCEINLINE bool Chance16(const uint a, const uint b)
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   141
{
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   142
	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
   143
}
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   144
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   145
/**
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   146
 * 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
   147
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   148
 * 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
   149
 * 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
   150
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   151
 * 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
   152
 * 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
   153
 *
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   154
 * @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
   155
 * @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
   156
 * @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
   157
 * @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
   158
 * @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
   159
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9111
diff changeset
   160
static FORCEINLINE bool Chance16R(const uint a, const uint b, uint32 &r)
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   161
{
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   162
	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
   163
	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
   164
}
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7936
diff changeset
   165
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
   166
#endif /* RANDOM_FUNC_HPP */