in reply to Randomly generating histograms
Well, there may be other things that bound you (this example just uses integers and can have dupes) but a simplistic brute force approach would be:
#!/usr/bin/perl -wd use strict; my $tot = 0; my $i = 0; my $x = 0; my @array; while( $tot < 100 ) { my $x = int rand( 100 ); $x = ( $tot + $x > 100 ) ? 100 - $tot : $x; $array[$i] = $x; $i++; $tot += $x; } $tot = 0; foreach( @array ) { print $_, "\n"; $tot += $_; } print "Total is ", $tot, "\n";
But I'm sure there are prettier approaches. But start with rand and perlfaq4 for ideas.
<code> -derby
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Randomly generating histograms
by narse (Pilgrim) on Mar 25, 2002 at 16:13 UTC | |
by derby (Abbot) on Mar 25, 2002 at 16:56 UTC | |
|
Re: Re: Randomly generating histograms
by narse (Pilgrim) on Mar 26, 2002 at 22:12 UTC |