mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
b9090b089e
- Replace modulo with rejection sampling in rand_i() to remove modulo bias. This yields uniform integers in [0, max) and ensures Fisher–Yates shuffles are truly uniform. - Speed up Random#bytes by writing 4 bytes per PRNG call (pack a uint32_t) and add a negative-size check (raise ArgumentError). - Minor shuffle! tweak: hoist RARRAY_PTR/length out of the loop to avoid repeated lookups. - Lower GC pressure in Array#sample(n): collect unique indices in a small C buffer, then push array elements directly, avoiding temporary Ruby integers. Behavioral notes: - rand(n) and methods depending on it now have unbiased distributions. - Random#bytes(size) now explicitly rejects negative sizes. - Other semantics remain unchanged. Co-authored-by: OpenAI Coding Assistant <noreply@openai.com>