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.


In reply to Re: mapping 10 -> 4 equally. by marinersk
in thread mapping 10 -> 4 equally. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.