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

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»

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

Replies are listed 'Best First'.
Re^9: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
by hv (Prior) on Sep 24, 2023 at 21:17 UTC

    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

        Yes.

        «The Crux of the Biscuit is the Apostrophe»

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

      «The Crux of the Biscuit is the Apostrophe»

Re^9: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
by LanX (Saint) on Sep 24, 2023 at 18:05 UTC