ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:120857] [Ruby master Bug#21106] The last iteration of the rb_ary_shuffle_bang function is useless
@ 2025-02-02  4:58 usumerican (usumerican _) via ruby-core
  0 siblings, 0 replies; only message in thread
From: usumerican (usumerican _) via ruby-core @ 2025-02-02  4:58 UTC (permalink / raw)
  To: ruby-core; +Cc: usumerican (usumerican _)

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/

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-02-02  4:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-02  4:58 [ruby-core:120857] [Ruby master Bug#21106] The last iteration of the rb_ary_shuffle_bang function is useless usumerican (usumerican _) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).