src/core/random_func.hpp
author rubidium
Sat, 15 Dec 2007 22:05:02 +0000
changeset 8575 dfc40de58c04
parent 8463 3920ac0ab803
child 8622 b917f0b0dd6b
permissions -rw-r--r--
(svn r11640) -Fix: missed change of include when moving stuff to random_func.cpp
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
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     3
/** @file random_func.h */
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
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     8
/**************
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     9
 * 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
    10
 *
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    11
 * 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
    12
 *  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
    13
 *  is to help finding desync problems.
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    14
 *
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    15
 * 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
    16
 **************/
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    17
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    18
//#define RANDOM_DEBUG
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
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    21
// Enable this to produce higher quality random numbers.
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    22
// Doesn't work with network yet.
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    23
// #define MERSENNE_TWISTER
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
void SetRandomSeed(uint32 seed);
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    26
#ifdef RANDOM_DEBUG
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    27
	#define Random() DoRandom(__LINE__, __FILE__)
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    28
	uint32 DoRandom(int line, const char *file);
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    29
	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    30
	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
    31
#else
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    32
	uint32 Random();
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    33
	uint RandomRange(uint max);
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    34
#endif
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    35
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    36
uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    37
uint InteractiveRandomRange(uint max);
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    38
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    39
/**
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    40
 * 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
    41
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    42
 * 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
    43
 * 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
    44
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    45
 * 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
    46
 * 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
    47
 * 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
    48
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    49
 * @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
    50
 * @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
    51
 * @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
    52
 * @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
    53
 */
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    54
static inline bool Chance16I(const uint a, const uint b, const uint32 r)
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    55
{
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    56
	assert(b != 0);
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    57
	return (uint16)r < (uint16)((a << 16) / b);
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    58
}
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    59
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    60
/**
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    61
 * 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
    62
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    63
 * 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
    64
 * 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
    65
 * (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
    66
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    67
 * @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
    68
 * @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
    69
 * @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
    70
 * @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
    71
 */
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    72
static inline bool Chance16(const uint a, const uint b)
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    73
{
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    74
	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
    75
}
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    76
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    77
/**
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    78
 * 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
    79
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    80
 * 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
    81
 * 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
    82
 *
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    83
 * 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
    84
 * 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
    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
 * @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
    87
 * @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
    88
 * @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
    89
 * @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
    90
 * @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
    91
 */
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    92
static inline bool Chance16R(const uint a, const uint b, uint32 &r)
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    93
{
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    94
	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
    95
	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
    96
}
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8432
diff changeset
    97
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    98
#endif /* RANDOM_FUNC_HPP */