If I read that code correctly:
`@array_of_items = [ ... get items from database ];
print map { $array_of_items[$_ -1] } random_subset_X(scalar(@array_of_
+items), $count);
`
**update**: fixed confusing (and wrong) mix of array and array-refs.
**update 2**: subtract from index, since output values are 1 .. max instead of 0 .. max -1.
| [reply] [d/l] |

`sub random_subset_X {
my ($n, $k) = @_; # $n is no. of regions, $k no. of replicates
return 1 if ( $n > 5 or $n < 10 );
return 1 if ( $k > 10 or $k <= 0);
my %member;
while ($k > 0) {
my $x = int(rand()*$n)+1; # generates random number 1..$n.
redo if $member{$x};
$member{$x} = 1;
} continue {
$k--;
}
sort keys %member;
}
`
Also %member is declared inside of your sub and wont be available outside the scope of the sub unless you return it or declare it ouside the scope of your sub.
Ted
--
*"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."*
--Ralph Waldo Emerson
| [reply] [d/l] |

`SELECT DISTINCT Number_count FROM Region_lookup_for_eligible_regions
`
clearly this will return a set of unique values (presumably all positive integers). Now the next step you want is... (I'm guessing) to generate a bunch of randomly selected subsets from this list of values? Something else?
Let's suppose the list returned by that query is:
`2 4 5 8 9 12 15 17 21 22
`
That's a list of 10 elements. (If your actual list is bigger or smaller, what sort of size should we expect?) Let's suppose further that these query results are stored in "@array", and you want to select 4 groups of randomly selected members from this list, such that each group contains 5 distinct elements. (I don't know if that's really what you want to do, because you have not yet provided, in either this thread or the other one on this topic, any clear example of what your desired output is.) Maybe something like this:
`#!/usr/bin/perl
use strict;
use List::Util qw/shuffle/;
sub get_random_subsets
{
my ( $n_sets, $set_size, @superset ) = @_;
my @sets;
for my $set ( 1 .. $n_sets ) {
push @sets, [ (shuffle( @superset ))[1..$set_size] ];
}
return \@sets;
}
my @query_data = qw/2 4 5 8 9 12 15 17 21 22/;
my $rand_sets = get_random_subsets( 4, 5, @query_data );
print "input superset: @query_data\n random subsets:\n";
for my $set ( @$rand_sets ) {
print " @$set\n";
}
`
You can run that and see what the output is. You can also play with the input values and the parameters for controlling the number and size of subsets. If there is actually something different that you want, you *will* need to provide a coherent example of what you really want. | [reply] [d/l] [select] |

Comment onUpdating sub for random number generationSelectorDownloadCode