This is nearly determanistic and fast, and I believe all possible outcomes are possible (d'uh!:), and I don't think that there is any particular bias. I'm not sure about the fairness though.

#! perl -slw use strict; use List::Util qw[ sum ]; sub gen { my $constraints = shift; my @rands = map $_->{ mid } - $_->{ sd }, @$constraints; my @limits = map $_->{ mid } + $_->{ sd }, @$constraints; my $remainder = 100 - sum @rands; while( $remainder ) { my $target = int rand @rands; my $addition = 1+ int rand $remainder; next if $rands[ $target ] + $addition > $limits[ $target ]; $rands[ $target ] += $addition; $remainder -= $addition; } return @rands; } my @constraints = ( { mid => 20, sd => 15 }, { mid => 30, sd => 25 }, { mid => 50, sd => 10 }, ); print join ' ', gen( \@constraints ) for 1 .. 20; @constraints = ( { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, { mid => 10, sd => 5 }, ); print join ' ', gen( \@constraints ) for 1 .. 20; __END__ C:\test>598736-2.pl | more 12 47 41 13 29 58 25 35 40 27 33 40 11 33 56 15 27 58 25 35 40 32 18 50 34 23 43 14 35 51 24 16 60 5 43 52 13 47 40 29 14 57 33 8 59 23 37 40 29 11 60 7 33 60 5 41 54 33 9 58 10 10 5 8 12 11 5 15 12 12 7 5 11 14 15 7 6 9 11 15 5 5 9 13 14 7 11 12 10 14 14 13 8 14 9 5 12 5 14 6 13 5 14 15 10 5 15 9 9 5 5 10 5 14 13 11 15 13 5 9 5 10 13 14 14 5 13 12 5 9 12 9 14 11 7 14 8 15 5 5 14 14 5 14 5 14 8 5 12 9 15 14 12 15 7 5 10 8 9 5 14 5 12 10 15 14 5 5 9 11 12 12 10 9 9 6 15 8 14 5 11 15 9 13 10 6 15 6 5 10 15 12 14 8 5 10 11 5 15 5 5 13 14 6 14 12 12 14 5 5 14 5 12 14 15 8 5 7 15 5 14 8 14 13 15 5 5 14 5 7 13 7 8 5 13 10 13 13 13 5 14 15 5 7 14 7 13 14 6 5 14 15 15 8 13 6 7 7 5 10

Update: This is slightly slower, but appears to produce fairer results:

sub gen3 { my $constraints = shift; my @rands = map $_->{ mid } - $_->{ sd }, @$constraints; my @limits = map $_->{ mid } + $_->{ sd }, @$constraints; my $maxChange = min map $_->{ sd }, @$constraints; my $remainder = 100 - sum @rands; while( $remainder ) { my $target = int rand @rands; my $addition = 1+ int rand min( $remainder, $maxChange ); next if $rands[ $target ] + $addition > $limits[ $target ]; $rands[ $target ] += $addition; $remainder -= $addition; } return @rands; }

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Need technique for generating constrained random data sets by BrowserUk
in thread Need technique for generating constrained random data sets by GrandFather

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.