Ruby - 以随机顺序返回数组

发布于 2024-09-13 17:22:08 字数 136 浏览 4 评论 0原文

在 Ruby 中以随机顺序返回数组的最简单方法是什么? 任何可以在 IRB 会话中使用的简短内容,例如

[1,2,3,4,5].random()
# or 
random_sort([1,2,3,4,5])

What is the easiest way to return an array in random order in Ruby?
Anything that is nice and short that can be used in an IRB session like

[1,2,3,4,5].random()
# or 
random_sort([1,2,3,4,5])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-09-20 17:22:08

数组.shuffle

array.shuffle

寂寞花火° 2024-09-20 17:22:08

如果您没有 [].shuffle,[].sort_by{rand} 的工作方式如 sepp2k 所指出的那样。 .sort_by 出于排序目的暂时用某些元素替换每个元素,在本例中是随机数。

[].sort{rand-0.5} 但是,无法正确洗牌。如果您对数组进行随机排序,某些语言(例如某些 Javascript 实现)将无法正确地对数组进行洗牌,有时会产生相当公开的后果。

JS 分析(带图!): http://www .robweir.com/blog/2010/02/microsoft-random-browser-ballot.html

Ruby 也不例外!它也有同样的问题。 :)

#sort a bunch of small arrays by rand-0.5
a=[]
100000.times{a <<  [0,1,2,3,4].sort{rand-0.5}}

#count how many times each number occurs in each position
b=[]
a.each do |x|
    x.each_index do |i|
        b[i] ||=[]
        b[i][x[i]] ||= 0
        b[i][x[i]] += 1
    end
end
p b

=>

[[22336, 18872, 14814, 21645, 22333],
 [17827, 25005, 20418, 18932, 17818],
 [19665, 15726, 29575, 15522, 19512],
 [18075, 18785, 20283, 24931, 17926],
 [22097, 21612, 14910, 18970, 22411]]

每个元素应该在每个位置出现大约 20000 次。 [].sort_by(rand) 给出了更好的结果。

#sort with elements first mapped to random numbers
a=[]
100000.times{a <<  [0,1,2,3,4].sort_by{rand}}

#count how many times each number occurs in each position
...

=>

[[19913, 20074, 20148, 19974, 19891],
 [19975, 19918, 20024, 20030, 20053],
 [20028, 20061, 19914, 20088, 19909],
 [20099, 19882, 19871, 19965, 20183],
 [19985, 20065, 20043, 19943, 19964]]

同样对于 [].shuffle (这可能是最快的)

[[20011, 19881, 20222, 19961, 19925],
 [19966, 20199, 20015, 19880, 19940],
 [20062, 19894, 20065, 19965, 20014],
 [19970, 20064, 19851, 20043, 20072],
 [19991, 19962, 19847, 20151, 20049]]

If you don't have [].shuffle, [].sort_by{rand} works as pointed out by sepp2k. .sort_by temporarily replaces each element by something for the purpose of sorting, in this case, a random number.

[].sort{rand-0.5} however, won't properly shuffle. Some languages (e.g. some Javascript implementations) don't properly shuffle arrays if you do a random sort on the array, with sometimes rather public consequences.

JS Analysis (with graphs!): http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html

Ruby is no different! It has the same problem. :)

#sort a bunch of small arrays by rand-0.5
a=[]
100000.times{a <<  [0,1,2,3,4].sort{rand-0.5}}

#count how many times each number occurs in each position
b=[]
a.each do |x|
    x.each_index do |i|
        b[i] ||=[]
        b[i][x[i]] ||= 0
        b[i][x[i]] += 1
    end
end
p b

=>

[[22336, 18872, 14814, 21645, 22333],
 [17827, 25005, 20418, 18932, 17818],
 [19665, 15726, 29575, 15522, 19512],
 [18075, 18785, 20283, 24931, 17926],
 [22097, 21612, 14910, 18970, 22411]]

Each element should occur in each position about 20000 times. [].sort_by(rand) gives much better results.

#sort with elements first mapped to random numbers
a=[]
100000.times{a <<  [0,1,2,3,4].sort_by{rand}}

#count how many times each number occurs in each position
...

=>

[[19913, 20074, 20148, 19974, 19891],
 [19975, 19918, 20024, 20030, 20053],
 [20028, 20061, 19914, 20088, 19909],
 [20099, 19882, 19871, 19965, 20183],
 [19985, 20065, 20043, 19943, 19964]]

Similarly for [].shuffle (which is probably fastest)

[[20011, 19881, 20222, 19961, 19925],
 [19966, 20199, 20015, 19880, 19940],
 [20062, 19894, 20065, 19965, 20014],
 [19970, 20064, 19851, 20043, 20072],
 [19991, 19962, 19847, 20151, 20049]]
濫情▎り 2024-09-20 17:22:08

这又如何呢?

Enumerable、Array、Hash 和 String 的辅助方法
让您可以选择随机项目或打乱项目顺序。

http:// /raa.ruby-lang.org/project/rand/

What about this?

Helper methods for Enumerable, Array, Hash, and String
that let you pick a random item or shuffle the order of items.

http://raa.ruby-lang.org/project/rand/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文