diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-07-12 22:53:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-12 22:53:58 +0300 |
commit | 69df5ffa970dbcafde7dbb5d360c0ea28300cb50 (patch) | |
tree | ba5165b102784eb793da8e9cd8dc35108fbeeed0 /src/common/random.c | |
parent | 4b8dd1cdaadba7ae2200f114a4e11ef62454eaa1 (diff) | |
parent | 14c9f5228fec75fc4e9bc8552e3fbae16f356103 (diff) | |
download | hercules-69df5ffa970dbcafde7dbb5d360c0ea28300cb50.tar.gz hercules-69df5ffa970dbcafde7dbb5d360c0ea28300cb50.tar.bz2 hercules-69df5ffa970dbcafde7dbb5d360c0ea28300cb50.tar.xz hercules-69df5ffa970dbcafde7dbb5d360c0ea28300cb50.zip |
Merge pull request #1290 from HerculesWS/common_interfacing
Common interfacing
Diffstat (limited to 'src/common/random.c')
-rw-r--r-- | src/common/random.c | 62 |
1 files changed, 40 insertions, 22 deletions
diff --git a/src/common/random.c b/src/common/random.c index ba70a5c1d..b3a13e054 100644 --- a/src/common/random.c +++ b/src/common/random.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -27,6 +27,7 @@ #include "common/timer.h" // gettick #include <mt19937ar/mt19937ar.h> // init_genrand, genrand_int32, genrand_res53 +#include <stdlib.h> #if defined(WIN32) # include "common/winapi.h" @@ -35,8 +36,14 @@ # include <unistd.h> #endif +/** @file + * Implementation of the random number generator interface. + */ + +struct rnd_interface rnd_s; +struct rnd_interface *rnd; -/// Initializes the random number generator with an appropriate seed. +/// @copydoc rnd_interface::init() void rnd_init(void) { unsigned long seed = (unsigned long)timer->gettick(); @@ -53,53 +60,64 @@ void rnd_init(void) #endif // HAVE_GETTID #endif init_genrand(seed); + + // Also initialize the stdlib rng, just in case it's used somewhere. + srand((unsigned int)seed); } +/// @copydoc rnd_interface::final() +void rnd_final(void) +{ +} -/// Initializes the random number generator. +/// @copydoc rnd_interface::seed() void rnd_seed(uint32 seed) { init_genrand(seed); } - -/// Generates a random number in the interval [0, SINT32_MAX] -int32 rnd(void) +/// @copydoc rnd_interface::random() +int32 rnd_random(void) { return (int32)genrand_int31(); } - -/// Generates a random number in the interval [0, dice_faces) -/// NOTE: interval is open ended, so dice_faces is excluded (unless it's 0) +/// @copydoc rnd_interface::roll() uint32 rnd_roll(uint32 dice_faces) { - return (uint32)(rnd_uniform()*dice_faces); + return (uint32)(rnd->uniform()*dice_faces); } - -/// Generates a random number in the interval [min, max] -/// Returns min if range is invalid. +/// @copydoc rnd_interface::value() int32 rnd_value(int32 min, int32 max) { - if( min >= max ) + if (min >= max) return min; - return min + (int32)(rnd_uniform()*(max-min+1)); + 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 +/// @copydoc rnd_interface::uniform() 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 +/// @copydoc rnd_interface::uniform53() double rnd_uniform53(void) { return genrand_res53(); } + +/// Interface base initialization. +void rnd_defaults(void) +{ + rnd = &rnd_s; + rnd->init = rnd_init; + rnd->final = rnd_final; + rnd->seed = rnd_seed; + rnd->random = rnd_random; + rnd->roll = rnd_roll; + rnd->value = rnd_value; + rnd->uniform = rnd_uniform; + rnd->uniform53 = rnd_uniform53; +} |