Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
random.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include "core/random.hpp"
3
4namespace Phasor
5{
6
13
14Value StdLib::rand_seed(const std::vector<Value> &args, VM *)
15{
16 checkArgCount(args, 2, "rand_seed");
17 int64_t s1 = args[0].asInt();
18 int64_t s2 = args[1].asInt();
19
20 if (s1 <= 0 || s2 <= 0)
21 {
22 throw std::runtime_error("rand_seed(): Both values must be positive integers");
23 }
24
25 PHASORstd_rand_seed(static_cast<uint64_t>(s1), static_cast<uint64_t>(s2));
26 return Value();
27}
28
29int64_t StdLib::rand_next_range(const std::vector<Value> &args, VM *)
30{
31 checkArgCount(args, 2, "rand_next_range");
32 int64_t min = args[0].asInt();
33 int64_t max = args[1].asInt();
34
35 if (min > max)
36 {
37 throw std::runtime_error("rand_get(): min value cannot be greater than max value");
38 }
39
40 return PHASORstd_rand_next_range(static_cast<uint64_t>(min), static_cast<uint64_t>(max));
41}
42
43double StdLib::rand_next_float(const std::vector<Value> &args, VM *)
44{
45 checkArgCount(args, 0, "rand_next_float");
47}
48
49} // namespace Phasor
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static int64_t rand_next_range(const std::vector< Value > &args, VM *vm)
Get a random number in range.
Definition random.cpp:29
static void registerRandomFunctions(VM *vm)
Definition random.cpp:7
static double rand_next_float(const std::vector< Value > &args, VM *vm)
Get a random float (technically a double at a low level).
Definition random.cpp:43
static Value rand_seed(const std::vector< Value > &args, VM *vm)
Seed the random number generator.
Definition random.cpp:14
Virtual Machine.
Definition VM.hpp:33
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
A value in the Phasor VM.
Definition Value.hpp:58
void PHASORstd_rand_seed(uint64_t s0, uint64_t s1)
Definition random.cpp:8
double PHASORstd_rand_next_double()
Definition random.cpp:26
int64_t PHASORstd_rand_next_range(int64_t min, int64_t max)
Definition random.cpp:31
The Phasor Programming Language and Runtime.
Definition AST.hpp:12