in reply to Re^6: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
in thread Algorithm RFC: fast (pseudo-)random shuffle with no repetition

there is only one possible solution

I count 7! = 5,040 solutions.

Trying 1e15 shuffles

There are 15!/8! = 32,432,400 permutations of the set (and for that matter 15! is only about 1.3e12). I don't see mention in Algorithm::Permute of how it handles duplicate elements, but I know that Algorithm::Loops handles them correctly.

That said I do agree with the thrust of your point. :)

  • Comment on Re^7: Algorithm RFC: fast (pseudo-)random shuffle with no repetition

Replies are listed 'Best First'.
Re^8: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
by karlgoethebier (Abbot) on Sep 24, 2023 at 17:45 UTC

    I briefly looked at Algorithm::Loops by tye this morning but didn't quite understand it yet. Not quite intuitive I would say. BTW, my mathematical understanding is poor. Why is it 15!/8!?

    .

    «The Crux of the Biscuit is the Apostrophe»

      NextPermute() and NextPermuteNum() modify the input array in place, and (to traverse all permutations) require that the input starts off sorted. So minimally adapting your previous code to use Algorithm::Loops would look (untested) something like this:

      #!/usr/bin/env perl use strict; use warnings; use Algorithm::Loops qw(NextPermuteNum); use Data::Dump; use feature qw(say); my @n = sort { $a <=> $b } ( 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, + 11, 12, 13, 14, 14, 15 ); my $m3 = qr/(.)\1\1/; my $m2 = qr/(.)\1/; do { my $s = pack( "(A*)*", @n ); if ( $s =~ /($m3)/ ) { say $1; sleep 1; goto PERMUTE; } if ( $s =~ /($m2)/ ) { goto PERMUTE; say $1; sleep 1; } say join " ", @n; PERMUTE: ; # empty statement to attach the label to } while (NextPermuteNum( @n )); __END__

      As for the 15!/8! figure: imagine each of the 1s is a different colour, then there are 15! distinguishable ways to arrange the numbers. Take any one of those (valid solution or not) and it has 8 coloured 1s in some order; and each of the 8! ways to order those colours will appear once in what is otherwise the same arrangement of numbers.

        >
        if ( $s =~ /($m2)/ ) { goto PERMUTE; say $1; sleep 1; }

        Looks like dead code after the goto

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

        Thanks, very nice. I wasn’t aware of the empty statement.

        «The Crux of the Biscuit is the Apostrophe»

Re^8: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
by LanX (Saint) on Sep 24, 2023 at 17:42 UTC
    Darn!1!

    > I count 7! = 5,040 solutions.

    Of course you are right, what my my brain saw was that there is only one possible way to position the 1s.°

    What I should better have shown is

    (1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2)

    which has only one solution

    (1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)

    and 15! divided by 7! is still a very big number.

    > I don't see mention in Algorithm::Permute of how it handles duplicate elements,

    Assuming worst case means it's not handling duplicates differently. Many hear just use shuffle which certainly doesn't care.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    °) off by one permutation group ;)

      karl@h3002993:~/src/perl$ time ./pmute.pl 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 real 0m0,064s user 0m0,044s sys 0m0,005s

      «The Crux of the Biscuit is the Apostrophe»

        The 15 came from the OP,

        But as I said

        > If this doesn't convince you that try-and-error isn't a good idea, try adding even more 1s and 2s.

        for instance ((1)x 51, (2) x 50)

        ==>

        ((101!) / (51!)) / (50!) = 1.99804427433e+29

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery