Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm not sure how "as uniformly distributed as possible" can be qualified ...

Do you have a test-code to check the "quality" of a solution?

But sorting according to a weighting function gives similar results like shown by you.

DB<171> %h=( A => 4, B => 2, C => 3, D => 1 ) => ("A", 4, "B", 2, "C", 3, "D", 1) DB<172> $sum=0; $sum+=$_ for values %h; => "" DB<173> @list=map { my ($k,$v)=($_,$h{$_}); my $int=$sum/$v; map { + [ $k => $int*($_-.5)] } 1..$v } keys %h => ( ["A", "1.25"], ["A", "3.75"], ["A", "6.25"], ["A", "8.75"], ["D", 5], ["C", "1.66666666666667"], ["C", 5], ["C", "8.33333333333333"], ["B", "2.5"], ["B", "7.5"], ) DB<174> map {$_->[0]} sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b-> +[0]} @list => ("A", "C", "B", "A", "C", "D", "A", "B", "C", "A")

Changing the weighting function would also allow to repeat the pattern in a way that joined sequences are still equally distributed ( that is A doesn't neighbor A )

An iterator-version shouldn't be too difficult.

Cheers Rolf

( addicted to the Perl Programming Language)

update

code simplified

update

well after second thought it's quite easy to find input where this approach fails ... never mind! :(


In reply to Re: Bag uniform distribution algorithms by LanX
in thread Bag uniform distribution algorithms by davido

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (20)
    As of 2014-08-27 14:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (239 votes), past polls