mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user