in reply to Need technique for generating constrained random data sets

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."