As you are going to need millions of datsets, it may be better and will certainly give you a more fairly random selection, to generate all the possible compliant sets and then choose one of them randomly each time you need one.

#! perl -slw use strict; use List::Util qw[ sum ]; sub Nfor (&@) { my( $code, @ranges ) = @_; my @indices = ( 0 ) x @ranges; $indices[ $#indices ]--; my $tumbler = $#indices; my @returns; while( 1 ) { if( ++$indices[ $tumbler ] > $#{ $ranges[ $tumbler ] } ) { $indices[ $tumbler ] = 0; last if --$tumbler < 0; next; } local $SIG{__WARN__} = sub{ warn @_ unless "@_" =~ /Exiting/ } +; if( defined wantarray ) { push @returns, $code->( map $ranges[ $_ ][ $indices[ $_ ] ], 0 .. $#indices ); } else { () = $code->( map $ranges[ $_ ][ $indices[ $_ ] ], 0 .. $# +indices ); } $tumbler = $#indices; } defined wantarray ? wantarray ? @returns : \@returns : (); } sub genPossibles{ my $constraints = shift; my @possibles; Nfor{ my $sum = sum( @_ ); next if $sum > 100; push @possibles, \@_ if $sum == 100; } map { [ $_->{ mid } - $_->{ sd } .. $_->{ mid } + $_->{ sd } ] } @$constraints; return \@possibles; } my @constraints = ( { mid => 20, sd => 15 }, { mid => 30, sd => 25 }, { mid => 50, sd => 10 }, ); my $possibles = genPossibles( \@constraints ); printf "There are %d possible selections\n", scalar @$possibles; print "Here is one of them: @{ @{ $possibles }[ rand @$possibles ] }"; __END__ C:\test>junk7 There are 651 possible selections Here is one of them: 20 39 41 C:\test>junk7 There are 651 possible selections Here is one of them: 27 32 41 C:\test>junk7 There are 651 possible selections Here is one of them: 17 29 54

My Nfor() sub is a not well tested attempt at a Algorithm::Loops::NestedLoops() act-a-like, but you could use that module instead.


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.