Skip to content

Commit

Permalink
Cleanup|Math: Random number functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Nov 8, 2018
1 parent 0bedb48 commit 3631dab
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doomsday/apps/client/src/render/stateanimator.cpp
Expand Up @@ -650,7 +650,7 @@ void StateAnimator::triggerByState(String const &stateName)
{
// Test for the probability of this animation.
float chance = seq.def->getf(DEF_PROBABILITY, 1.f);
if (frand() > chance) continue;
if (randf() > chance) continue;

// Start the animation on the specified node (defaults to root),
// unless it is already running.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/core/range.h
Expand Up @@ -62,7 +62,7 @@ struct Range
return de::wrap(i, start, end);
}
inline Type random() const {
return start + frand() * size();
return start + randf() * size();
}
inline Range &operator |= (Type const &value) {
start = de::min(start, value);
Expand Down
7 changes: 6 additions & 1 deletion doomsday/sdk/libcore/include/de/math.h
Expand Up @@ -215,7 +215,12 @@ inline Type lerp(Type start, Type end, float pos) {
/**
* @return Random floating-point value in the range [0, 1).
*/
DENG2_PUBLIC float frand();
DENG2_PUBLIC float randf();

/**
* @return Random unsigned integer in the range [0, 4294967295].
*/
DENG2_PUBLIC duint32 randui32();

/** @} */

Expand Down
10 changes: 7 additions & 3 deletions doomsday/sdk/libcore/src/math.cpp
Expand Up @@ -25,16 +25,20 @@

namespace de {

float frand()
float randf()
{
using namespace std;

static minstd_rand rng(
uint32_t(chrono::system_clock::to_time_t(chrono::system_clock::now())));
static minstd_rand rng(uint32_t(chrono::system_clock::to_time_t(chrono::system_clock::now())));

return float(double(rng() - rng.min()) / double(rng.max() - rng.min() + 1));
}

duint32 randui32()
{
return duint32(randf() * 0x10000) | (duint32(randf() * 0x10000) << 16);
}

duint32 crc32(IByteArray const &data)
{
/* ====================================================================== */
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/scriptsys/bindings_math.cpp
Expand Up @@ -25,7 +25,7 @@ namespace de {

static Value *Function_Math_Random(Context &, Function::ArgumentValues const &)
{
return new NumberValue(frand());
return new NumberValue(randf());
}

static Value *Function_Math_RandInt(Context &, Function::ArgumentValues const &args)
Expand Down

0 comments on commit 3631dab

Please sign in to comment.