src/core/random_func.hpp
changeset 11049 f8bbc9635251
parent 10429 1b99254f9607
child 11050 091271fcfbb9
equal deleted inserted replaced
11047:f4e7290c23b9 11049:f8bbc9635251
    57 	#define Random() DoRandom(__LINE__, __FILE__)
    57 	#define Random() DoRandom(__LINE__, __FILE__)
    58 	uint32 DoRandom(int line, const char *file);
    58 	uint32 DoRandom(int line, const char *file);
    59 	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
    59 	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
    60 	uint DoRandomRange(uint max, int line, const char *file);
    60 	uint DoRandomRange(uint max, int line, const char *file);
    61 #else
    61 #else
    62 	static inline uint32 Random() { return _random.Next(); }
    62 	static FORCEINLINE uint32 Random() { return _random.Next(); }
    63 	static inline uint32 RandomRange(uint16 max) { return _random.Next(max); }
    63 	static FORCEINLINE uint32 RandomRange(uint16 max) { return _random.Next(max); }
    64 #endif
    64 #endif
    65 
    65 
    66 static inline uint32 InteractiveRandom() { return _interactive_random.Next(); }
    66 static FORCEINLINE uint32 InteractiveRandom() { return _interactive_random.Next(); }
    67 static inline uint32 InteractiveRandomRange(uint16 max) { return _interactive_random.Next(max); }
    67 static FORCEINLINE uint32 InteractiveRandomRange(uint16 max) { return _interactive_random.Next(max); }
    68 
    68 
    69 /**
    69 /**
    70  * Checks if a given randomize-number is below a given probability.
    70  * Checks if a given randomize-number is below a given probability.
    71  *
    71  *
    72  * This function is used to check if the given probability by the fraction of (a/b)
    72  * This function is used to check if the given probability by the fraction of (a/b)
    79  * @param a The numerator of the fraction
    79  * @param a The numerator of the fraction
    80  * @param b The denominator of the fraction, must of course not be null
    80  * @param b The denominator of the fraction, must of course not be null
    81  * @param r The given randomize-number
    81  * @param r The given randomize-number
    82  * @return True if v is less or equals (a/b)
    82  * @return True if v is less or equals (a/b)
    83  */
    83  */
    84 static inline bool Chance16I(const uint a, const uint b, const uint32 r)
    84 static FORCEINLINE bool Chance16I(const uint a, const uint b, const uint32 r)
    85 {
    85 {
    86 	assert(b != 0);
    86 	assert(b != 0);
    87 	return (uint16)r < (uint16)(((a << 16) + b / 2) / b);
    87 	return (uint16)r < (uint16)(((a << 16) + b / 2) / b);
    88 }
    88 }
    89 
    89 
    97  * @see Chance16I()
    97  * @see Chance16I()
    98  * @param a The numerator of the fraction
    98  * @param a The numerator of the fraction
    99  * @param b The denominator of the fraction
    99  * @param b The denominator of the fraction
   100  * @return True in (a/b) percent
   100  * @return True in (a/b) percent
   101  */
   101  */
   102 static inline bool Chance16(const uint a, const uint b)
   102 static FORCEINLINE bool Chance16(const uint a, const uint b)
   103 {
   103 {
   104 	return Chance16I(a, b, Random());
   104 	return Chance16I(a, b, Random());
   105 }
   105 }
   106 
   106 
   107 /**
   107 /**
   117  * @param a The numerator of the fraction
   117  * @param a The numerator of the fraction
   118  * @param b The denominator of the fraction
   118  * @param b The denominator of the fraction
   119  * @param r The variable to save the randomize-number from Random()
   119  * @param r The variable to save the randomize-number from Random()
   120  * @return True in (a/b) percent
   120  * @return True in (a/b) percent
   121  */
   121  */
   122 static inline bool Chance16R(const uint a, const uint b, uint32 &r)
   122 static FORCEINLINE bool Chance16R(const uint a, const uint b, uint32 &r)
   123 {
   123 {
   124 	r = Random();
   124 	r = Random();
   125 	return Chance16I(a, b, r);
   125 	return Chance16I(a, b, r);
   126 }
   126 }
   127 
   127