Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Bag uniform distribution algorithms

by LanX (Bishop)
on Oct 27, 2013 at 21:38 UTC ( #1059935=note: print w/replies, xml ) Need Help??

in reply to Bag uniform distribution algorithms

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)


code simplified


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1059935]
[holli]: i don't know. meat and tomatoes don't belong anywhere near each other in my book
[holli]: unless the meat is in sausage form and the tomatoes being ketchup, that is
[Discipulus]: no ragł? i'm sorry..;=)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-11-22 09:25 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (316 votes). Check out past polls.