[ruby-core:120857] [Ruby master Bug#21106] The last iteration of the rb_ary_shuffle_bang function is useless
From:
"usumerican (usumerican _) via ruby-core" <ruby-core@...>
Date:
2025-02-02 04:58:34 UTC
List:
ruby-core #120857
Issue #21106 has been reported by usumerican (usumerican _).
----------------------------------------
Bug #21106: The last iteration of the rb_ary_shuffle_bang function is useless
https://bugs.ruby-lang.org/issues/21106
* Author: usumerican (usumerican _)
* Status: Open
* ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-darwin21]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
The number of iterations in the Fischer-Yates shuffle is (N-1) times.
In the following verification code, rand function is called N times.
```
class R
def rand(n)
puts n
return 0
end
end
a = [1,2,3,4,5,6,7,8,9,10]
puts a.shuffle(random: R.new).join(",")
```
The while conditional expression in the rb_ary_shuffle_bang function can be changed from while(n) to while(n > 1).
https://github.com/ruby/ruby/blob/db02a6b3ab4cabbdf492c26dcb1929b4ef0370a1/array.c#L6704
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/