From: Benoit Daloze Date: 2011-01-07T21:29:55+09:00 Subject: [ruby-core:34219] Re: [Ruby 1.9-Feature#4247][Open] New features for Array#sample, Array#choice Hi, On 7 January 2011 12:08, Yusuke ENDOH wrote: > Hi, > > 2011/1/7 Yoji Ojima : >> We are planning to add the following features of the random sampling to Array. >> >> 1. Weighted random sampling. >> 2. Sampling with replacement. >> 3. Iteration. >> >> It is discussed in ruby-dev (Feature #3647 and #4147). > > > I'm adding complementary information. > > There are two reasons why the name "choice" is selected: > > �- For backward compatibility. �Once upon a time, trunk (before 1.9.0) > � �had provided Array#choice. �It was backported to 1.8.7. �Then, in > � �trunk, the name was changed to Array#sample (see the thread from > � �[ruby-core:18036]). �But 1.8.7 still provides Array#choice because > � �it cannot remove Array#choice for compatibility reason. > � �Note that 1.8.7's Array#choice does not receive any argument, so > � �there is no compatibility problem. > > �- Mathematica provides RandomSample and RandomChoice for SRSWOR and > � �SRSWR, respectively > � �http://reference.wolfram.com/mathematica/ref/RandomSample.html > � �http://reference.wolfram.com/mathematica/ref/RandomChoice.html > > > There are some algorithms [1] [2] for fast multiple sampling. > > [1] Pavlos S. Efraimidis, Paul G. Spirakis > � �Weighted random sampling with a reservoir > � �Information Processing Letters > � �Volume 97, Issue 5 (16 March 2006) > � �(Ruby implementation is in [ruby-dev:42844]) > > [2] A. J. Walker > � �An Efficient Method for Generating Discrete Random Variables with > General Distributions > � �ACM Transactions on Mathematical Software, 3 (1977), 253-256. > > > Matz roughly approved this suggestion. �But he said that the method name > of "each_sample" and "each_choice" are a bit awkward, and that he want > to hear opinions of ruby-core folks. > Of course, we appreciate any comments about the feature itself rather > than the name. > > -- > Yusuke Endoh > > Thanks for the name clarification. I think 'choice' and 'each_choice' are weird. Sample is "a subset of a population", and so it seems logical to have multiple elements in return. But choice seems like 'singular', only meant for one element. Is it correct to say "Array#choice returns a choice of some random elements" ? It does not seems right to me. To this idea, #choice should always return one element, and #sample could be the enumerator (the form which returns an Array would then be Array#sample.take(size)). However, I guess that would break too much compatibility with current versions. And I really like 1.9.2 name of #sample for 'a single random element' (even if it might be incorrect to statistics). Did you consider having another option to {each_,}sample to allow replacement ? Such as: ary.sample 3, replace: true I see the title of the feature is "Array#sample(n, replace=false)", so I guess the idea was there. How was it decided to instead go for another set of methods ? (Just because of Mathematica's choice ? Compatibility seems against users' interest if it is to use a wrong method name) Or to follow combinations/permutations: ary.repeated_sample 3 In http://en.wikipedia.org/wiki/Simple_random_sample, the terms used are "simple random sampling with/without replacement", that comfort me to think choice is not the right .. choice (at least for multiple elements). I am not a native English speaker, so I might be not accurate about this. If this is the case, please ignore what I said. About the feature, I wonder if an Hash would be a good idea for the weight option. It can be very similar to a Proc with #default_proc, except much faster for already stored values. Off-topic: I think it would be nice if some ruby-dev/ruby-core discussions could be merged. Even if I cannot currently understand Japanese, I can at least read their "coded" ideas. I would see that as using the same topic, with both languages.