From 5f9808587bb5ecb39216c66f7520377412400313 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 22 Aug 2025 21:28:26 +0900 Subject: [PATCH] mruby-random: simplify unsigned arithmetic in rand_i function Remove intermediate bound variable and cast max directly to uint32_t where needed for unsigned operations. This eliminates C4146 warning about unary minus on unsigned type while maintaining the same mathematical behavior. Co-authored-by: Claude --- mrbgems/mruby-random/src/random.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c index 2530fea4e..f12b03ccf 100644 --- a/mrbgems/mruby-random/src/random.c +++ b/mrbgems/mruby-random/src/random.c @@ -142,13 +142,12 @@ rand_i(rand_state *t, mrb_int max) { /* return uniform integer in [0, max) without modulo bias */ if (max <= 0) return 0; - uint32_t bound = (uint32_t)max; - uint32_t threshold = (uint32_t)(-bound) % bound; /* power-of-two fast path => 0 */ + uint32_t threshold = (uint32_t)(-max) % (uint32_t)max; /* power-of-two fast path => 0 */ uint32_t r; do { r = rand_uint32(t); } while (r < threshold); - return (mrb_int)(r % bound); + return (mrb_int)(r % (uint32_t)max); } static mrb_value