Issue #4147 has been updated by yhara (Yutaka HARA). Target version changed from 2.0.0 to next minor ---------------------------------------- Feature #4147: Array#sample で重みを指定したい https://bugs.ruby-lang.org/issues/4147#change-31575 Author: oj (Yoji Ojima) Status: Assigned Priority: Normal Assignee: shyouhei (Shyouhei Urabe) Category: Target version: next minor =begin Array#sample にブロックを渡したとき、ブロックの戻り値を要素の重みとして使用するのはいかがでしょうか。 下記のサンプルで、"大吉" が "凶" の 1000 倍の確率で選択されるようにしたいです。 omikuji_box = [ {:name => "大吉", :weight => 1000}, {:name => "中吉", :weight => 100}, {:name => "小吉", :weight => 10}, {:name => "凶", :weight => 1} ] omikuji = omikuji_box.sample {|v| v[:weight] } puts omikuji[:name] =end
on 2012-10-25 16:53
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.