summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-random/src/random.c
AgeCommit message (Collapse)Author
2021-10-31Added `Random.#bytes` methoddearblue
ref: https://docs.ruby-lang.org/ja/3.0.0/method/Random/i/bytes.html
2021-10-24Make `mrb_static_assert()` a variable argumentdearblue
`mrb_static_assert()` extends the macro function to take one or two arguments. If the argument is other than that, an error will occur. References: - static_assert のメッセージ省略を許可 - cpprefjp C++日本語リファレンス https://cpprefjp.github.io/lang/cpp17/extending_static_assert.html - c - Overloading Macro on Number of Arguments - Stack Overflow https://stackoverflow.com/a/11763277
2021-08-21Stirs internal state when `seed` is set in `Random`dearblue
This is to avoid the approximation of `Random#rand` when `seed`s are close together. For example, the first `#rand` values after doing `Random#srand(0)` and `Random#srand(1)` are very similar. Below is the sequence of mruby before this patch was given a `seed` of `0...20`: ```console % bin/mruby -e 'r = Random.new; 20.times { |i| r.srand(i); puts "seed=%2d %s" % [i, 10.times.map { "%0.3f" % r.rand }.join(" ")] }' seed= 0 0.643 0.585 0.198 0.732 0.087 0.605 0.548 0.468 0.573 0.966 seed= 1 0.643 0.585 0.198 0.607 0.370 0.605 0.633 0.593 0.395 0.439 seed= 2 0.643 0.585 0.197 0.981 0.652 0.730 0.875 0.713 0.529 0.269 seed= 3 0.643 0.585 0.198 0.857 0.934 0.730 0.960 0.216 0.286 0.523 seed= 4 0.643 0.585 0.197 0.231 0.217 0.605 0.959 0.958 0.478 0.482 seed= 5 0.643 0.585 0.197 0.106 0.249 0.605 0.044 0.330 0.925 0.047 seed= 6 0.643 0.585 0.197 0.481 0.781 0.731 0.285 0.960 0.804 0.725 seed= 7 0.643 0.585 0.197 0.356 0.813 0.731 0.370 0.711 0.937 0.448 seed= 8 0.643 0.585 0.198 0.732 0.329 0.108 0.243 0.974 0.766 0.936 seed= 9 0.643 0.585 0.198 0.607 0.611 0.108 0.827 0.102 0.962 0.597 seed=10 0.643 0.585 0.198 0.981 0.393 0.233 0.569 0.723 0.472 0.805 seed=11 0.643 0.585 0.198 0.857 0.676 0.233 0.154 0.222 0.603 0.371 seed=12 0.643 0.585 0.198 0.231 0.458 0.108 0.654 0.979 0.928 0.577 seed=13 0.643 0.585 0.198 0.106 0.490 0.108 0.239 0.355 0.749 0.831 seed=14 0.643 0.585 0.198 0.481 0.523 0.233 0.981 0.486 0.505 0.131 seed=15 0.643 0.585 0.198 0.356 0.555 0.234 0.565 0.233 0.011 0.666 seed=16 0.643 0.585 0.197 0.730 0.573 0.611 0.904 0.512 0.971 0.153 seed=17 0.643 0.585 0.197 0.605 0.855 0.611 0.240 0.636 0.041 0.509 seed=18 0.643 0.585 0.196 0.979 0.137 0.736 0.229 0.765 0.674 0.832 seed=19 0.643 0.585 0.197 0.855 0.420 0.736 0.566 0.268 0.183 0.219 ```
2021-08-14random.c: use `I` specifier for `mrb_get_args()`; ref #5530Yukihiro "Matz" Matsumoto
2021-07-25Remove redundant include headers.Yukihiro "Matz" Matsumoto
- stdlib.h - stddef.h - stdint.h - stdarg.h - limits.h - float.h
2021-04-24random.c: refactoring; #5421Yukihiro "Matz" Matsumoto
- `Random.rand` with `MRB_NO_FLOAT` behaves as `Random.rand(100)` - `Random.rand(i)` where `i<0` raises `ArgumentError`
2021-03-20random.c: fixed seed underflow bug.Yukihiro "Matz" Matsumoto
`MRB_INT_MIN` does not have a corresponding positive value.
2021-02-26Use `MRB_SYM()` more extensively.Yukihiro "Matz" Matsumoto
2021-01-26Revert "Minimize the changes in #5277"Yukihiro "Matz" Matsumoto
This reverts commit dc51d89ac22acc60b9bfeed87115863565b74085.
2021-01-22Minimize the changes in #5277Yukihiro "Matz" Matsumoto
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.
2021-01-11Avoid including `presym.inc` in existing header filesKOBAYASHI Shuji
Addressed an issue where existing programs linking `libmruby.a` could only be built by adding `<build-dir>/include` to compiler's include path.
2020-12-19🔒 Fix missing HTTPS on linksJohn Bampton
2020-10-12Use `mrb_int_value()` instead of `mrb_fixnum_value()`.Yukihiro "Matz" Matsumoto
Where fixnum overflow can happen.
2020-10-12Update `mruby-random` gem to support 32 bit platforms.Yukihiro "Matz" Matsumoto
`sizeof(rand_state)` had been bigger than `sizeof(void*)*3`. Changed random number generator to `Xorshift96` on 32 bit platforms.
2020-10-12Reorganize `Integer` system.Yukihiro "Matz" Matsumoto
- 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()`
2020-10-12Use xoshiro128++ instead of xorshift96/128.Yukihiro "Matz" Matsumoto
2020-10-12Fix `rand_real` to return random number `[0,1)` not `[0,1]`.Yukihiro "Matz" Matsumoto
2020-10-12Rename float configuration option names.Yukihiro "Matz" Matsumoto
- `MRB_WITHOUT_FLOAT` => `MRB_NO_FLOAT` - `MRB_USE_FLOAT` => `MRB_USE_FLOAT32` The former is to use `USE_XXX` naming convention. The latter is to make sure `float` is 32bit float and not floating point number in general.
2020-10-12Avoid breaking the `result` array by side-effect in C++.Yukihiro "Matz" Matsumoto
2020-10-12Use functions that take symbols to reduce string litrals in C.Yukihiro "Matz" Matsumoto
2020-10-12Add `MRB_SYM()` for inline symbols.Yukihiro "Matz" Matsumoto
2020-06-29Work around more MSC optimzer bugsRory OConnell
2020-06-26Reduce scope of volatile keyword for MSC bugRory OConnell
2020-06-26Narrower scope working around MSC bugRory OConnell
2020-06-26work around MSC optimization generating non functional codeRory OConnell
2019-09-26Use type predicate macros instead of `mrb_type` if possibleKOBAYASHI Shuji
For efficiency with `MRB_WORD_BOXING` (implement type predicate macros for all `enum mrb_vtype`).
2019-08-27Fix build of `mruby-random` on 32-bit modeKOBAYASHI Shuji
2019-08-26Fix `Array#sample` with `MRB_INT32`KOBAYASHI Shuji
Array index became potentially negative because `uint32_t` is cast to `mrb_int`.
2019-07-24Use `MRB_TT_ISTRUCT` for `Random` to reduce memory.Yukihiro "Matz" Matsumoto
When the size of Xorshift128 seed (`sizeof(uint32)*4`) is bigger than ISTRUCT_DATA_SIZE, `Random` uses Xorshift96 instead.
2019-07-22Switch random generator from Mersenne Twister to Xorshit128.Yukihiro "Matz" Matsumoto
Now `rand` can be used with `MRB_WITHOUT_FLOAT`; ref #4576
2019-07-14Error needed/conflicts configurationdearblue
The purpose is to clarify the error if there is a needed/conflicts configuration at compile time.
2018-11-19Remove implicit conversion using `to_int` method.Yukihiro "Matz" Matsumoto
The ISO standard does not include implicit type conversion using `to_int`. This implicit conversion often causes vulnerability. There will be no more attacks like #4120. In addition, we have added internal convenience method `__to_int` which does type check and conversion (from floats).
2018-09-07Clear terminated spacedearblue
2017-07-27Embed small size array elements in the heap.Yukihiro "Matz" Matsumoto
It reduces the memory consumption and sometimes improve the performance as well. For example, the consumed memory size of `bench/bm_ao_render.rb` is reduced from 1.2GB to 1GB, and its total execution time become 18.795 sec from 22.229 sec.
2017-02-02Fetch arguments earlier to avoid a crash.Clayton Smith
2016-11-23Fix segfault in Array#sampleBouke van der Bijl
2016-01-21mruby-random: fixed wrong fixnum conversionYukihiro "Matz" Matsumoto
2015-11-27include changed from by quotes ("") to by brackets (<>); close #3032Yukihiro "Matz" Matsumoto
2014-09-12constify pointer from RARRAY_PTR to detect potential write barrier bugs.Yukihiro "Matz" Matsumoto
if you see compiler errors due to this commit, you'd better to use array-modifying functions, e.g. mrb_ary_set() or mrb_ary_push(), otherwise you might see nasty GC bugs in the future. if you are sure what you are doing, replace `RARRAY_PTR(ary)` by `mrb_ary_ptr(ary)->ptr`. but be warned.
2014-08-20Add API `mrb_data_init` to initialize `MRB_TT_DATA` tagged instance.take_cheeze
2014-07-12remove spaces after open parensYukihiro "Matz" Matsumoto
2014-05-10Pacify MSVC warnings for random.ckyab
2014-04-30remove trailing spacesNobuyoshi Nakada
2014-03-09add data_type check to mruby-randomYukihiro "Matz" Matsumoto
2014-03-09refactoring mruby-randomYukihiro "Matz" Matsumoto
2014-03-09implement Array#sample in mruby-random gemYukihiro "Matz" Matsumoto
2014-03-05remove unused variablecubicdaiya
GLOBAL_RAND_SEED_KEY is no longer used.
2014-03-04Fix #1793mattn
2014-03-02fix a memory leak in mruby-random.Tatsuya Matsumoto
2014-02-25Implement default Random instance.chasonr
Previously, the default random number generator was implemented using static storage. This storage is common to all instances of mruby in a process, and use of the default random number generator in one instance will perturb the default random number generator in other instances. It is also not thread safe. With this change, the default random number generator is defined as Random::DEFAULT, as it is in CRuby.