From 04452763110f0245617c876ecbb271dffb01d05d Mon Sep 17 00:00:00 2001 From: flaviojs Date: Sat, 25 Jun 2011 22:09:35 +0000 Subject: * Added Mersenne Twister MT19937 as a random number generator. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@14865 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/random.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common/random.h | 20 +++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/common/random.c create mode 100644 src/common/random.h (limited to 'src') diff --git a/src/common/random.c b/src/common/random.c new file mode 100644 index 000000000..b7f2c080c --- /dev/null +++ b/src/common/random.c @@ -0,0 +1,83 @@ +// Copyright (c) Athena Dev Teams - Licensed under GNU GPL +// For more information, see LICENCE in the main folder + +#include "../common/timer.h" // gettick +#include "random.h" +#if defined(WIN32) + #define WIN32_LEAN_AND_MEAN + #include +#elif defined(HAVE_GETPID) || defined(HAVE_GETTID) + #include + #include +#endif +#include // time +#include // init_genrand, genrand_int32, genrand_res53 + + +/// Initializes the random number generator with an appropriate seed. +void rnd_init(void) +{ + uint32 seed = gettick(); + seed += (uint32)time(NULL); +#if defined(WIN32) + seed += GetCurrentProcessId(); + seed += GetCurrentThreadId(); +#else +#if defined(HAVE_GETPID) + seed += (uint32)getpid(); +#endif // HAVE_GETPID +#if defined(HAVE_GETTID) + seed += (uint32)gettid(); +#endif // HAVE_GETTID +#endif + init_genrand(seed); +} + + +/// Initializes the random number generator. +void rnd_seed(uint32 seed) +{ + init_genrand(seed); +} + + +/// Generates a random number in the interval [0, UINT32_MAX] +uint32 rnd(void) +{ + return (uint32)genrand_int32(); +} + + +/// Generates a random number in the interval [0, dice_faces) +/// NOTE: interval is open ended, so dice_faces is excluded (unless it's 0) +uint32 rnd_roll(uint32 dice_faces) +{ + return (uint32)(rnd_uniform()*dice_faces); +} + + +/// Generates a random number in the interval [min, max] +/// Returns min if range is invalid. +int32 rnd_value(int32 min, int32 max) +{ + if( min >= max ) + return min; + return min + (int32)(rnd_uniform()*(max-min+1)); +} + + +/// Generates a random number in the interval [0.0, 1.0) +/// NOTE: interval is open ended, so 1.0 is excluded +double rnd_uniform(void) +{ + return ((uint32)genrand_int32())*(1.0/4294967296.0);// divided by 2^32 +} + + +/// Generates a random number in the interval [0.0, 1.0) with 53-bit resolution +/// NOTE: interval is open ended, so 1.0 is excluded +/// NOTE: 53 bits is the maximum precision of a double +double rnd_uniform53(void) +{ + return genrand_res53(); +} diff --git a/src/common/random.h b/src/common/random.h new file mode 100644 index 000000000..1bb8ecfb5 --- /dev/null +++ b/src/common/random.h @@ -0,0 +1,20 @@ +// Copyright (c) Athena Dev Teams - Licensed under GNU GPL +// For more information, see LICENCE in the main folder + +#ifndef _RANDOM_H_ +#define _RANDOM_H_ + +#ifndef _CBASETYPES_H_ +#include "../common/cbasetypes.h" +#endif + +void rnd_init(void); +void rnd_seed(uint32); + +uint32 rnd(void);// [0, UINT32_MAX] +uint32 rnd_roll(uint32 dice_faces);// [0, dice_faces) +int32 rnd_value(int32 min, int32 max);// [min, max] +double rnd_uniform(void);// [0.0, 1.0) +double rnd_uniform53(void);// [0.0, 1.0) + +#endif /* _RANDOM_H_ */ -- cgit v1.2.3-60-g2f50