Repeatedly randomly sampling without replacement is equivalent to shuffling the list and just splitting the result into groups of the desired size. So do that but don't start over after that...

First, shuffle the list.

Now you can group the list into groups of 5 (or 8 or whatever) items and you've got int(158/5) unique samples. Pick a number that is relatively prime with your list size and you've got another int(158/5) samples, none of which match any of your other samples. Repeat as needed. If you run out of relatively prime numbers before you get enough samples, then you can reshuffle the list at that point (risking repeating a sampling but with even lower odds than those you theorized would be acceptable).

#!/usr/bin/perl -w use strict; # Here are some stubs so you can test my code. # Replace this with a Fisher Yates shuffle: sub PermuteList { my $av= shift @_; print "Not shuffling ( $av->[0] .. $av->[-1] )\n"; } # Replace these with your own code: sub GetPopulation { return( 100..(99+$ARGV[0]) ); } sub ProcessSample { print "( @_ )\n"; } sub Lcd { my( $x, $y )= @_; while( 1 ) { if( $y < $x ) { ( $x, $y )= ( $y, $x ); } if( $x == 0 ) { return $y; } $y %= $x; } } sub GenNextSample { my( $size, @list )= @_; my $step = @list; my( $start, $offset ); return sub { while( 1 ) { if( @list/2 <= $step ) { $step= 1; PermuteList( \@list ); $offset= $start= int rand @list; } if( 1 == Lcd( $step, 0+@list ) ) { my @sample; do { return @sample if $size == @sample; push @sample, $list[$offset]; $offset= ( $offset + $step ) % @list; } while( $offset != $start ); } $step++; } }; } my $size= $ARGV[1]; my $iter= GenNextSample( $size, GetPopulation() ); my $samples = 0; while( $samples++ < 64000 ) { my @sample = $iter->(); ProcessSample( @sample ); }

You can test the code like:

> perl lcd.pl 16 3 | more Not shuffling ( 100 .. 115 ) ( 112 113 114 ) ( 115 100 101 ) ( 102 103 104 ) ( 105 106 107 ) ( 108 109 110 ) ( 112 115 102 ) ( 105 108 111 ) ( 114 101 104 ) ( 107 110 113 ) ( 100 103 106 ) ( 112 101 106 ) ( 111 100 105 ) ( 110 115 104 ) ( 109 114 103 ) ( 108 113 102 ) ( 112 103 110 ) ( 101 108 115 ) ( 106 113 104 ) ( 111 102 109 ) ( 100 107 114 ) Not shuffling ( 100 .. 115 ) ( 102 103 104 ) ...

- tye        


In reply to Re: Sampling from Combination Space (lcd) by tye
in thread Sampling from Combination Space by AdriftOnMemoryBliss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.