From: Robert Dober Date: 2007-02-26T19:13:43+09:00 Subject: Re: extract a random number of items from an array ------=_Part_102172_11867717.1172484819021 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 2/25/07, Mingquan Chen wrote: > > If you use this way, then how we you get exactly n values randomly from > the > given array? > > I doubt the correct of this way. > > My solution should meet your requirement. :-) > > On 2/26/07, Josselin wrote: > > > > On 2007-02-25 17:41:58 +0100, James Edward Gray II > > said: > > > > > On Feb 25, 2007, at 10:20 AM, Josselin wrote: > > > > > >> given an array of values, how should I extract an random number of > > >> these values > > >> > > >> a = [13, 15, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, > > >> 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, > > >> 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, > > >> 75, 76, 77, 78, 79, 81, 82, 83, 84, 85, 86, 88, 89, 90, 91, 92, 93, > > >> 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106] > > > > > > Just give a random chance that a value is added to the result set: > > > > > > >> a.select { rand(2).zero? } > > > => [13, 15, 23, 24, 25, 26, 27, 28, 29, 31, 41, 42, 44, 45, 46, 47, > > > 49, 51, 52, 53, 55, 58, 60, 61, 62, 66, 67, 68, 69, 70, 71, 72, 73, > > > 75, 76, 78, 81, 82, 83, 89, 90, 91, 94, 95, 98, 99, 100, 101, 103, > > > 104, 105, 106] > > > >> a.select { rand(2).zero? } > > > => [13, 18, 19, 20, 21, 24, 26, 27, 28, 29, 30, 31, 41, 42, 44, 48, > > > 49, 51, 52, 53, 55, 57, 58, 59, 62, 63, 64, 66, 68, 70, 79, 81, 82, > > > 83, 86, 89, 90, 91, 92, 94, 97, 98, 102, 104, 105, 106] > > > >> a.select { rand(2).zero? } > > > => [15, 17, 18, 19, 20, 21, 23, 25, 30, 41, 44, 45, 46, 49, 50, 51, > > > 52, 53, 54, 57, 59, 63, 64, 66, 68, 70, 72, 73, 76, 78, 79, 81, 82, > > > 90, 91, 95, 97, 101, 102, 104] > > > > > > Hope that helps. > > > > > > James Edward Gray II > > > > thanks James that's exactly what I need > > I made various tests like a.select { rand(10).zero? } > > and I got different results.. as I want > > > > I try to understand the logic behind your solution (not to dry > stupid...) > > > > select from array a ... { for each item. 0#no 1#yes } ok for > > rand(2) , if I use rand(10) less chances to get it > > is that right ? > > > > > > > Actually James' solution does not give you n elements, maybe you want to try this an_ary.sort_by{ rand }[0..n-1] # Might use a second or two of CPU for millions of elements though ;) should do the trick unless you want a *stable* subarray. Stable means that if x occurs on the LHS of y in an_ary it also does in your subarray, but if I read you correctly you did not need this. Cheers Robert -- We have not succeeded in answering all of our questions. In fact, in some ways, we are more confused than ever. But we feel we are confused on a higher level and about more important things. -Anonymous ------=_Part_102172_11867717.1172484819021--