From ff3cbca554f8d58084b7be7dd33d12c0388308b9 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 29 Jul 2025 19:39:02 +0800 Subject: [PATCH] add random methods --- include/pocketpy/pocketpy.h | 31 +++++++++++++++++++------------ src/modules/random.c | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 2f349d83..3cc78134 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -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 *************/ diff --git a/src/modules/random.c b/src/modules/random.c index babd3aed..ef8bdd9c 100644 --- a/src/modules/random.c +++ b/src/modules/random.c @@ -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 \ No newline at end of file +#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); +} \ No newline at end of file