openmw-tes3coop/libs/openengine/misc/rng.cpp
dteviot 3f28634d1f 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.
2015-03-15 14:07:47 +13:00

29 lines
No EOL
597 B
C++

#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));
}
}
}