in reply to Re^8: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
in thread Algorithm RFC: fast (pseudo-)random shuffle with no repetition
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^10: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
by LanX (Saint) on Sep 25, 2023 at 00:13 UTC | |
by karlgoethebier (Abbot) on Sep 25, 2023 at 19:47 UTC | |
Re^10: Algorithm RFC: fast (pseudo-)random shuffle with no repetition
by karlgoethebier (Abbot) on Sep 24, 2023 at 23:52 UTC |