in reply to Random data generation.

I tried a variation on a card shuffle.
#!/usr/bin/perl use strict; use warnings; my @random = (qw/a b c d e f/) x 2; foreach my $i (reverse 0..$#random) { my $r = int rand ($i+1); @random[$i, $r] = @random[$r, $i] unless ($i == $r); } print @random, "\n";

Replies are listed 'Best First'.
Re^2: Random data generation.
by BrowserUk (Patriarch) on Jun 27, 2010 at 15:11 UTC

    This suffers the same limitation as jwkrahn (that initially had me fooled), in that:

    1. it can only deal with strings up to twice the length of the input set;

      Whilst I gave 2 & 12 as an examples of M & N, I thought it was clear that other values were possible.

    2. it will only generate a small fraction of the possible legal values;

      Even at length 12, it won't generate abbabbabbabb.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.