in reply to Weighted random numbers generator

If I am correct, all solutions presented so far focus on getting one element. I've been looking for an efficient way to do exactly what spurperl is looking for, only for a number of samples. Ie., I want to say

@indices = DrawSamplesUneven(@elements, 5);

and get five indices. Any ideas?

Update: See my previous question Drawing samples from a set (regarding drawing one sample)

Update: Forgot to mention that of course I want to get five different indices.

Replies are listed 'Best First'.
Re: Re: Weighted random numbers generator
by dga (Hermit) on Mar 13, 2003 at 18:33 UTC

    My code above would work nicely since your 5 samples would use the same weighting and so you get the speed win. Build a tiny wrapper to call it 5 times.

    For Example:

    sub lots_o_indexes { my($count)=@_; my @indexlist; foreach (1..$count) { push @indexlist, weighted_rand(@weights); } return @indexlist; }