From: Thomas Enebo Date: 2008-08-02T03:40:17+09:00 Subject: [ruby-core:18066] Re: New array methods cycle, choice, shuffle (plus bug in cycle) Gregory Brown wrote: > On Fri, Aug 1, 2008 at 2:01 PM, Jim Weirich wrote: > >> On Aug 1, 2008, at 1:53 PM, Thomas Enebo wrote: >> >>> I would assume a randomized array based on name. Picking a good name is >>> tough. Picking a better name is not so easy. Array.give_me_something >>> >> What about: >> >> array.random_choice >> > > This would be a suitable analogy to Python's random.choice(array) ... > but is almost as long and only slightly more readable than: > > array[rand(array.length)] > > (as Thomas suggested). > > I like the idea of Array#sample with an option argument to specify how > many random elements you want. > Would arr.sample(3) mean 'pick 3 elements from arr at random' or would > it mean 'randomly pick an element from arr 3 times, then give the > results'. > I think the former could be pretty useful. > Actually 'sample(n=1)' seems like it make a lot more sense than 'choice' (if you assume that it is useful that Array has this method in the first place). sampling data is a pretty common phrase. Perhaps a good name does exist... -Tom