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>
Refactor mrb_ary_sample to use mrb_alloca for the 'idx' array. This
ensures that the memory is automatically freed when the C function
returns, preventing a memory leak if an exception is raised during
array manipulation.
Co-authored-by: Gemini <gemini@google.com>
- 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>
Replace XML-style markup tags in comments with markdown equivalents:
- <code>...</code> to `...` (inline code)
- <tt>...</tt> to `...` (teletype/monospace)
- <i>...</i> to *...* (italics/emphasis)
- +...+ to `...` (parameter/variable references)
Updated 80+ files across core source, headers, mrbgems, and libraries
to use consistent markdown formatting in documentation comments.
Handled edge cases including special characters like <=> operators.
Co-authored-by: Atlassian Rovo Dev
To achieve this, the following changes were made:
- Exported `mrb_bint_size`, `mrb_bint_from_bytes`, and `mrb_bint_sign`
functions from `mruby-bigint` to be used in other mrbgems.
- Modified `mruby-random` to use these new functions to handle Bigint
arguments in the `rand` method.
Co-authored-by: Gemini <gemini@google.com>
Added complete call-seq documentation for 7 missing public methods in the
random gem, improving documentation coverage from 25% to 100%.
Documentation added:
- Random.new: Create new random number generator with optional seed
- Random#rand: Generate random numbers (float, integer, or range)
- Random#srand: Seed the random number generator
- Random#bytes: Generate random byte strings
- Random.rand/rand: Class method and Kernel method for default generator
- Random.srand/srand: Class method and Kernel method for seeding
- Random.bytes: Class method for random bytes using default generator
Each method now includes:
- Clear method signatures with parameter and return types
- Detailed descriptions of random number generation behavior
- Practical examples showing different usage patterns
- Notes about default vs instance generators
- Cross-references between class methods and Kernel methods
- Range and numeric type handling explanations
The existing Array methods (shuffle, shuffle!, sample) were already
well-documented and remain unchanged. This completes the documentation
for all random number generation functionality in mruby, covering both
the Random class API and the traditional Kernel methods.
This significantly improves usability for developers working with
random number generation, cryptographic applications, and statistical
sampling in embedded Ruby environments.
Co-authored-by: Atlassian Rovo Dev
Utilize process randomiser which may not be provided by non Linux OSes.
With those OSes, rand() would return same values if the processes are
invoked within a second.
For Array#sample and Array#shuffle. They used to take optional ordinal
argument for Random object but CRuby uses `random:` keyword argument.
Now mruby is compatible with CRuby here.
Instead of including `mruby/presym.h` everywhere, we provided the
fallback `mruby/presym.inc` under `include/mruby` directory, and specify
`-I<build-dir>/include` before `-I<top-dir>/include` in `presym.rake`.
So even when someone drops `-I<build-dir>/include` in compiler options,
it just compiles without failure.
- Integrate `Fixnum` and `Integer`
- Remove `Integral`
- `int / int -> int`
- Replace `mrb_fixnum()` to `mrb_int()`
- Replace `mrb_fixnum_value()` to `mrb_int_value()`.
- Use `mrb_integer_p()` instead of `mrb_fixnum_p()`
* The `Fixnum` constant is now an alias for the `Integer` class.
* Remove `struct mrb_state::fixnum_class` member.
If necessary, use `struct mrb_state::integer_class` instead.