```On Mon, Feb 26, 2007 at 01:20:07AM +0900, 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]
>
> def extract_values( an_array, n)
> 	...
> 	return another_array of n values extracted randomly from an_array
> end
>
> I don't know where to start ..
> - create an array of n random indexes from 0 to an_array.length, and
> use it to extract the value

Are you trying to do pick 'n' elements from 'a' like lottery balls? i.e.
once a value has been picked once, it cannot be picked again?

Short but inefficient:

result = a.sort_by{rand}[0...n]

More efficient (but deletes elements from 'a', so dup it first if necessary):

result = []
n.times { result << a.slice!( rand(a.size) ) }

Or if I misunderstood the question, and duplicate picks are allowed:

result = []
n.times { result << a[ rand(a.size) ] }

HTH,

Brian.

```