fix a bug of random

This commit is contained in:
blueloveTH 2025-02-15 00:49:32 +08:00
parent 069e80f2f5
commit 26a073de4f

View File

@ -137,24 +137,19 @@ int64_t mt19937__randint(mt19937* self, int64_t a, int64_t b) {
static bool Random__new__(int argc, py_Ref argv) { static bool Random__new__(int argc, py_Ref argv) {
mt19937* ud = py_newobject(py_retval(), py_totype(argv), 0, sizeof(mt19937)); mt19937* ud = py_newobject(py_retval(), py_totype(argv), 0, sizeof(mt19937));
mt19937__ctor(ud); mt19937__ctor(ud);
if(argc == 1) return true;
if(argc == 2) {
PY_CHECK_ARG_TYPE(1, tp_int);
py_i64 seed = py_toint(py_arg(1));
mt19937__seed(ud, seed);
return true; return true;
} }
return TypeError("Random(): expected 0 or 1 arguments, got %d", argc - 1);
}
static bool Random__init__(int argc, py_Ref argv) { static bool Random__init__(int argc, py_Ref argv) {
if(argc == 1) { if(argc == 1) {
// do nothing // do nothing
} else if(argc == 2) { } else if(argc == 2) {
mt19937* ud = py_touserdata(py_arg(0)); mt19937* ud = py_touserdata(py_arg(0));
if(!py_isnone(&argv[1])){
PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(1, tp_int);
py_i64 seed = py_toint(py_arg(1)); py_i64 seed = py_toint(py_arg(1));
mt19937__seed(ud, (uint32_t)seed); mt19937__seed(ud, (uint32_t)seed);
}
} else { } else {
return TypeError("Random(): expected 1 or 2 arguments, got %d"); return TypeError("Random(): expected 1 or 2 arguments, got %d");
} }