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 i64 s1 = args[0].asInt();
18 i64 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<u64>(s1), static_cast<u64>(s2));
26 return Value();
27}
28
29i64 StdLib::rand_next_range(const std::vector<Value> &args, VM *)
30{
31 checkArgCount(args, 2, "rand_next_range");
32 i64 min = args[0].asInt();
33 i64 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<u64>(min), static_cast<u64>(max));
41}
42
43f64 StdLib::rand_next_float(const std::vector<Value> &args, VM *)
44{
45 checkArgCount(args, 0, "rand_next_float");
47}
48
49} // namespace Phasor
static i64 rand_next_range(const std::vector< Value > &args, VM *vm)
Get a random number in range.
Definition random.cpp:29
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static f64 rand_next_float(const std::vector< Value > &args, VM *vm)
Get a random float (technically a f64 at a low level).
Definition random.cpp:43
static void registerRandomFunctions(VM *vm)
Definition random.cpp:7
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:36
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:59
Phasor::f64 PHASORstd_rand_next_double()
Definition random.cpp:27
void PHASORstd_rand_seed(Phasor::u64 s0, Phasor::u64 s1)
Definition random.cpp:9
Phasor::i64 PHASORstd_rand_next_range(Phasor::i64 min, Phasor::i64 max)
Definition random.cpp:32
The Phasor Programming Language and Runtime.
Definition AST.hpp:13
int64_t i64
Definition phsint.hpp:16
double f64
Definition phsint.hpp:7
uint64_t u64
Definition phsint.hpp:12