34 lines
809 B
C++
34 lines
809 B
C++
#include <random>
|
|
#include <chrono>
|
|
|
|
#include "src/Loader.hpp"
|
|
#include "src/Module.hpp"
|
|
|
|
extern "C" void lib_rand(grino::Loader& loader)
|
|
{
|
|
grino::Loc loc {"core/rand"};
|
|
auto mod = loader.add_module("rand");
|
|
|
|
mod->loader()->add_native("randf", [loc](auto args){
|
|
std::mt19937 r;
|
|
|
|
r.seed(std::chrono::steady_clock::now().time_since_epoch().count());
|
|
|
|
std::uniform_real_distribution<float> urd(0.0f, 1.0f);
|
|
|
|
return grino::Value::make_float(loc, urd(r));
|
|
});
|
|
|
|
mod->loader()->add_native("range", [loc](auto args){
|
|
std::mt19937 r;
|
|
|
|
r.seed(std::chrono::steady_clock::now().time_since_epoch().count());
|
|
int from = args[0]->as_int();
|
|
int to = args[1]->as_int();
|
|
|
|
std::uniform_int_distribution<int> dist(from, to);
|
|
|
|
return grino::Value::make_int(loc, dist(r));
|
|
});
|
|
}
|