summaryrefslogtreecommitdiff
path: root/src/common/random.c
diff options
context:
space:
mode:
authorai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec>2011-07-10 06:17:06 +0000
committerai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec>2011-07-10 06:17:06 +0000
commit9ef634559f3cde232586b1dc6fd00bea3f3d8e53 (patch)
tree541295af8a6fbfe15b09da353680be6dd4d6b3ff /src/common/random.c
parent9c100697bf79604fcef8b7eedbadeed45c63b41b (diff)
downloadhercules-9ef634559f3cde232586b1dc6fd00bea3f3d8e53.tar.gz
hercules-9ef634559f3cde232586b1dc6fd00bea3f3d8e53.tar.bz2
hercules-9ef634559f3cde232586b1dc6fd00bea3f3d8e53.tar.xz
hercules-9ef634559f3cde232586b1dc6fd00bea3f3d8e53.zip
* Merged changes from trunk [14827:14894/trunk].
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/branches/renewal@14895 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/common/random.c')
-rw-r--r--src/common/random.c83
1 files changed, 83 insertions, 0 deletions
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 <windows.h>
+#elif defined(HAVE_GETPID) || defined(HAVE_GETTID)
+ #include <sys/types.h>
+ #include <unistd.h>
+#endif
+#include <time.h> // time
+#include <mt19937ar.h> // 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();
+}