Verified Commit d035016f authored by RobLoach's avatar RobLoach
Browse files

Update to use C++ Random Wrapper

parent d34a4cdf
......@@ -10,6 +10,9 @@ SOURCES_CXX += $(wildcard \
test/*.cpp \
)
# random
FLAGS += -Ivendor/random/include
# filesystem
FLAGS += -Ivendor/filesystem
......
......@@ -5,44 +5,32 @@
#include <string>
#include <sstream>
#include <iostream>
#include "effolkronium/random.hpp"
#include "zlib.h"
using Random = effolkronium::random_static;
namespace Modules {
bool math::load() {
setRandomSeed(static_cast<int>(time(0)));
return true;
}
double math::random() {
int num = rand();
return static_cast<double>(num) / static_cast<double>(RAND_MAX);
float math::random() {
return random(0.0f, 1.0f);
}
int math::random(int max) {
int num = rand();
return num % max + 1;
float math::random(float max) {
return random(0.0f, max);
}
int math::random(int min, int max) {
int num = rand();
if (min > max) {
min ^= max;
max ^= min;
min ^= max;
}
num = num % (max-min+1);
num += min;
return num;
float math::random(float min, float max) {
return Random::get(min, max);
}
void math::setRandomSeed(int seed) {
m_seed = seed;
srand(seed);
}
void math::setRandomSeed(int low, int high) {
setRandomSeed(low + high);
Random::seed(seed);
m_seed = seed;
}
int math::getRandomSeed() {
......
......@@ -13,23 +13,23 @@ class math {
/**
* @brief Get uniformly distributed pseudo-random number, between the given numbers.
*/
int random(int min, int max);
float random(float min, float max);
/**
* @brief Get uniformly distributed pseudo-random number, from 0 to the given max value.
* @brief Gets a random number from 0 to the max given.
*/
int random(int max);
float random(float max);
/**
* @brief Get uniformly distributed pseudo-random number, between 0 and 1.
* @brief Gets a random number between 0 and 1.
*/
double random();
float random();
/**
* @brief Sets the seed of the random number generator.
*/
void setRandomSeed(int seed);
/**
* @brief Sets the seed of the random number generator.
*/
void setRandomSeed(int low, int high);
/**
* @brief Gets the seed of the random number generator.
*/
......@@ -57,14 +57,13 @@ class math {
std::string decompress(const std::string& str);
/**
* @brief Pi
*/
const float pi = 3.14159265358979323846f;
/**
* @brief e
*
@brief e
*/
const float e = 2.718281828459045f;
int m_seed;
const float pi = 3.14159265358979323846f;
int m_seed = 0;
};
} // namespace Modules
......
......@@ -249,11 +249,10 @@ script::script(const std::string& file) {
chai.add(mathlib);
chai.add(fun(&math::pi), "pi");
chai.add(fun(&math::e), "e");
chai.add(fun<double, math>(&math::random), "random");
chai.add(fun<int, math, int>(&math::random), "random");
chai.add(fun<int, math, int, int>(&math::random), "random");
chai.add(fun<float, math>(&math::random), "random");
chai.add(fun<float, math, float>(&math::random), "random");
chai.add(fun<float, math, float, float>(&math::random), "random");
chai.add(fun<void, math, int>(&math::setRandomSeed), "setRandomSeed");
chai.add(fun<void, math, int, int>(&math::setRandomSeed), "setRandomSeed");
chai.add(fun(&math::getRandomSeed), "getRandomSeed");
chai.add(fun<std::string, math, const std::string&>(&math::compress), "compress");
chai.add(fun<std::string, math, const std::string&, int>(&math::compress), "compress");
......
......@@ -18,3 +18,7 @@ def load() {
event.quit()
}
def draw() {
}
......@@ -19,6 +19,3 @@ assert(true, "math.setRandomSeed(10)")
num = math.getRandomSeed()
assert_equal(num, 10, "math.getRandomSeed()")
math.setRandomSeed(15, 100)
assert_not_equal(math.getRandomSeed(), 10, "math.setRandomSeed(10, 100)")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment