mirror of https://github.com/OpenMW/openmw.git
consolidate random number logic
Note, I suspect Rng::rollClosedProbability() is not needed. The only difference between it and rollProbability() is that one time in 37k (on Windows), it will give an output of 1.0. On some versions of Linux, the value of 1.0 will occur about 1 time in 4 billion.pull/535/head
parent
cf077dcf5d
commit
3f28634d1f
@ -0,0 +1,29 @@
|
||||
#include "rng.hpp"
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
namespace OEngine {
|
||||
namespace Misc {
|
||||
|
||||
void Rng::init()
|
||||
{
|
||||
std::srand(static_cast<unsigned int>(std::time(NULL)));
|
||||
}
|
||||
|
||||
float Rng::rollProbability()
|
||||
{
|
||||
return static_cast<float>(std::rand() / (static_cast<double>(RAND_MAX)+1.0));
|
||||
}
|
||||
|
||||
float Rng::rollClosedProbability()
|
||||
{
|
||||
return static_cast<float>(std::rand() / static_cast<double>(RAND_MAX));
|
||||
}
|
||||
|
||||
int Rng::rollDice(int max)
|
||||
{
|
||||
return static_cast<int>((std::rand() / (static_cast<double>(RAND_MAX)+1.0)) * (max));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
#ifndef MISC_RNG_H
|
||||
#define MISC_RNG_H
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace OEngine {
|
||||
namespace Misc
|
||||
{
|
||||
|
||||
/*
|
||||
Provides central implementation of the RNG logic
|
||||
*/
|
||||
class Rng
|
||||
{
|
||||
public:
|
||||
|
||||
/// seed the RNG
|
||||
static void init();
|
||||
|
||||
/// return value in range [0.0f, 1.0f) <- note open upper range.
|
||||
static float rollProbability();
|
||||
|
||||
/// return value in range [0.0f, 1.0f] <- note closed upper range.
|
||||
static float rollClosedProbability();
|
||||
|
||||
/// return value in range [0, max) <- note open upper range.
|
||||
static int rollDice(int max);
|
||||
|
||||
/// return value in range [0, 99]
|
||||
static int roll0to99() { return rollDice(100); }
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue