in reply to Re^4: Need technique for generating constrained random data sets
in thread Need technique for generating constrained random data sets
Using the example of two 6-sided dice. If we set a limit of 10 for the sum, then some values will be rejected. Which I think is analogous to this problem. Accumulating the frequency for each value by simply rejecting those over our limit still gives a flat distribution:
#! perl -slw use strict; use List::Util qw[ sum ]; my %stats; for( 1 .. $ARGV[ 0 ] || 1e3 ) { my @dice = map 1 + int( rand 6 ), 1 .. 2; next if sum( @dice ) > 10; ++$stats{ "@dice" }; } my $mean = sum( values %stats ) / scalar values %stats; printf "%12s %d dev( %d )\n", $_, $stats{ $_ }, abs( $stats{ $_ } - $mean ) for map unpack( 'x[C2] A*', $_ ), sort map pack( 'C2 A*', ( reverse split( ' ', $_ ) ), $_ ), keys %stats; __END__ C:\test>junk7 1e6 1 1 27534 dev( 247 ) 2 1 27640 dev( 141 ) 3 1 27742 dev( 39 ) 4 1 27878 dev( 96 ) 5 1 28161 dev( 379 ) 6 1 27556 dev( 225 ) 1 2 28018 dev( 236 ) 2 2 27426 dev( 355 ) 3 2 27650 dev( 131 ) 4 2 28099 dev( 317 ) 5 2 27949 dev( 167 ) 6 2 27607 dev( 174 ) 1 3 27752 dev( 29 ) 2 3 28033 dev( 251 ) 3 3 27767 dev( 14 ) 4 3 27730 dev( 51 ) 5 3 27802 dev( 20 ) 6 3 27760 dev( 21 ) 1 4 27791 dev( 9 ) 2 4 28065 dev( 283 ) 3 4 27894 dev( 112 ) 4 4 27487 dev( 294 ) 5 4 27778 dev( 3 ) 6 4 27935 dev( 153 ) 1 5 27708 dev( 73 ) 2 5 27668 dev( 113 ) 3 5 27854 dev( 72 ) 4 5 27810 dev( 28 ) 5 5 27654 dev( 127 ) 1 6 27600 dev( 181 ) 2 6 27700 dev( 81 ) 3 6 28029 dev( 247 ) 4 6 27717 dev( 64 )
Whether a flat distribution is correct for this application is GrandFather's call. But even if some other distribution (poisson or whatever), is required, it would be much easier to arrive at that once you have a generator that produces a known distribution. I'm also unsure how you extend those other distributions to 4 or more dimensions?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Need technique for generating constrained random data sets
by xdg (Monsignor) on Feb 08, 2007 at 22:47 UTC | |
by GrandFather (Saint) on Feb 09, 2007 at 00:17 UTC |