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 <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-08-22 21:28:26 +09:00
parent 0ce99e442d
commit 5f9808587b
+2 -3
View File
@@ -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