add random methods

This commit is contained in:
blueloveTH 2025-07-29 19:39:02 +08:00
parent ca086f9732
commit ff3cbca554
2 changed files with 49 additions and 14 deletions

View File

@ -747,19 +747,26 @@ PK_API bool
/// noexcept
PK_API int py_dict_len(py_Ref self);
/************* random module *************/
PK_API void py_newRandom(py_OutRef out);
PK_API void py_Random_seed(py_Ref self, py_i64 seed);
PK_API py_f64 py_Random_random(py_Ref self);
PK_API py_f64 py_Random_uniform(py_Ref self, py_f64 a, py_f64 b);
PK_API py_i64 py_Random_randint(py_Ref self, py_i64 a, py_i64 b);
/************* vmath module *************/
void py_newvec2(py_OutRef out, c11_vec2);
void py_newvec3(py_OutRef out, c11_vec3);
void py_newvec2i(py_OutRef out, c11_vec2i);
void py_newvec3i(py_OutRef out, c11_vec3i);
void py_newcolor32(py_OutRef out, c11_color32);
c11_mat3x3* py_newmat3x3(py_OutRef out);
c11_vec2 py_tovec2(py_Ref self);
c11_vec3 py_tovec3(py_Ref self);
c11_vec2i py_tovec2i(py_Ref self);
c11_vec3i py_tovec3i(py_Ref self);
c11_mat3x3* py_tomat3x3(py_Ref self);
c11_color32 py_tocolor32(py_Ref self);
PK_API void py_newvec2(py_OutRef out, c11_vec2);
PK_API void py_newvec3(py_OutRef out, c11_vec3);
PK_API void py_newvec2i(py_OutRef out, c11_vec2i);
PK_API void py_newvec3i(py_OutRef out, c11_vec3i);
PK_API void py_newcolor32(py_OutRef out, c11_color32);
PK_API c11_mat3x3* py_newmat3x3(py_OutRef out);
PK_API c11_vec2 py_tovec2(py_Ref self);
PK_API c11_vec3 py_tovec3(py_Ref self);
PK_API c11_vec2i py_tovec2i(py_Ref self);
PK_API c11_vec3i py_tovec3i(py_Ref self);
PK_API c11_mat3x3* py_tomat3x3(py_Ref self);
PK_API c11_color32 py_tocolor32(py_Ref self);
/************* Others *************/

View File

@ -145,7 +145,7 @@ static bool Random__init__(int argc, py_Ref argv) {
// do nothing
} else if(argc == 2) {
mt19937* ud = py_touserdata(py_arg(0));
if(!py_isnone(&argv[1])){
if(!py_isnone(&argv[1])) {
PY_CHECK_ARG_TYPE(1, tp_int);
py_i64 seed = py_toint(py_arg(1));
mt19937__seed(ud, (uint32_t)seed);
@ -328,4 +328,32 @@ __ERROR:
#undef MATRIX_A
#undef UPPER_MASK
#undef LOWER_MASK
#undef ADD_INST_BOUNDMETHOD
#undef ADD_INST_BOUNDMETHOD
void py_newRandom(py_OutRef out) {
py_Type type = py_gettype("random", py_name("Random"));
assert(type != 0);
mt19937* ud = py_newobject(out, type, 0, sizeof(mt19937));
mt19937__ctor(ud);
}
void py_Random_seed(py_Ref self, py_i64 seed) {
mt19937* ud = py_touserdata(self);
mt19937__seed(ud, (uint32_t)seed);
}
py_f64 py_Random_random(py_Ref self) {
mt19937* ud = py_touserdata(self);
return mt19937__random(ud);
}
py_f64 py_Random_uniform(py_Ref self, py_f64 a, py_f64 b) {
mt19937* ud = py_touserdata(self);
return mt19937__uniform(ud, a, b);
}
py_i64 py_Random_randint(py_Ref self, py_i64 a, py_i64 b) {
mt19937* ud = py_touserdata(self);
if(a > b) { c11__abort("randint(a, b): a must be less than or equal to b"); }
return mt19937__randint(ud, a, b);
}