in reply to mapping 10 -> 4 equally.

This should do it -- not using any advanced algorithms, just a simple, two-pass distribution:

#!/usr/bin/perl use strict; use List::Util; # -----[ Constants ]-------------------------------------------------- +---------- my @ORGLTR = qw /a b c d e f g h i j/; my @REQNUM = qw /1 2 3 4/; my $LTRREP = 10; # Number of times we repeat ea +ch letter # -----[ Generate unrandomized list ]--------------------------------- +---------- my @basgen = (); foreach my $curltr (@ORGLTR) { for (my $currep = 0; $currep < $LTRREP; $currep++) { push @basgen, $curltr; } } my $basdsp = join '', @basgen; # -----[ Randomize ]-------------------------------------------------- +---------- my @rndstr = &List::Util::shuffle(@basgen); # -----[ Break it down ]---------------------------------------------- +---------- my $ltrcnt = @ORGLTR; # How many distinct letters in + original set? my $numcnt = @REQNUM; # How many numbers in requeste +d results? my $rescnt = @rndstr; # How many letters in full set +? my $basdst = int($ltrcnt / $numcnt); # Base integral distribution my $remltr = $ltrcnt % $numcnt; # What isn't covered by the ba +se my $supdst = $numcnt / $remltr; # Supplemental distribution if ($numcnt % $remltr) { print "Sorry -- $LTRREP x $ltrcnt letters cannot be redistributed +equally as $numcnt numbers.\n"; } else { my @wrkltr = @ORGLTR; # Letters to work on my $rndstr = join '', @rndstr; # The randomized string print "Original String: $rndstr\n"; # Perform base distribution { foreach my $curdst (1 .. $basdst) { foreach my $curnum (@REQNUM) { my $curltr = shift @wrkltr; $rndstr =~ s/$curltr/$curnum/g; } } } # Perform supplemental distribution { my $curnum = 0; my $supcnt = $ltrcnt * $remltr; foreach my $curdst (1 .. $supcnt) { foreach my $curltr (@wrkltr) { $curnum++; if ($curnum > $numcnt) { $curnum = 1; } $rndstr =~ s/$curltr/$curnum/; } } } # Display result print " Final string: $rndstr\n"; } # -----[ Cleanup ]---------------------------------------------------- +---------- exit; __END__ # -----[ Fini ]------------------------------------------------------- +----------

Results:

D:\PerlMonks>remap-nodebug.pl Original String: ibcdhjfifhiabahgejedgihgacchdijaedgihcifjejfghecgdae +cjdaibcgacaegjcbegghfeibbfdhhjbjbdadbacfdffbijfe Final string: 1234422324112143141433431334412114334312412234133411 +344132331311323213342112224444222414213242223421 D:\PerlMonks>

Update: Clarified reason for apology message.

Replies are listed 'Best First'.
Re^2: mapping 10 -> 4 equally.
by BrowserUk (Patriarch) on Jun 21, 2015 at 20:03 UTC

    FWIW: this is what I came up with yesterday after anonymonk prompted me into getting my mind around what I was trying to describe in the OP:

    $x = join'', shuffle map{ ($_)x10 } 'a'..'j'; print $x;; ahgbjgbcjjgbbfdiadeehgdhcijjebaaegehccdfdbejcgdhehcbehdgcihiffgefjijab +fbjadfaaiiiahfegihdgdaccicffjb $y = $x; $y =~ tr[abcdefgh][12341234]; print $y;; 1432j323jj32224i141143443ijj121113143342421j3344143214433i4i22312jij12 +22j14211iii14213i4434133i322j2 $i=0; $j=0; $y =~ s[([ij])]{ $1 eq 'i' ? ( ($i^=1) ? '1' : '2' ) : ( ($j^=1) ? '3' + : '4') }ge; print $y;; 1432332343322241141143443243121113143342421433441432144331422231231412 +223142112121421314434133232242

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!