in reply to Non-Repetitive Random Numbers

Here's a technique to generate non-repetitive random numbers:

$ perl -Mstrict -Mwarnings -E ' my @nums = 0 .. 100000; say splice @nums, int(rand @nums), 1 for 1 .. 10; '

Sample output:

10228 67730 84390 58034 22572 34234 43334 38920 5934 82290

Depending on how you want to use this, you may need to check whether @nums becomes exhausted.

The "script" you provided is only a subroutine. You don't show the context in which it is called; you don't show how the global variable $samples is declared or what sort of value(s) are assigned to it. An explanation of the "script" will actually require the script.

-- Ken

Replies are listed 'Best First'.
Re^2: Non-Repetitive Random Numbers
by Anonymous Monk on Apr 05, 2013 at 06:29 UTC

    The full script is as follows:

    my @pop = (1..100); my $samples = 30; my @sample = rand_samp(30,@pop); print "@sample"; sub rand_samp { my ($n,@n) = (shift,@_); return 0 unless ($n < scalar @n);# see note below my %seen = (); until (keys %seen == $samples) { $seen{$pop[rand @pop]}=1; } return(keys %seen); }