this conforms to what you were talking about. gets flakier the closer avg gets to min || max. comments?
perl -e'@a=split//," \n/)\"={\"";print $a[$_] for split//,"00002731000223155264555102401"'#!/usr/bin/perl -w use strict; use Data::Dumper; my ($acceptable_laziness) = 1.0e-15; sub rand_list { my ($howmany, $min, $max, $avg, @a) = (@_, ()); print STDERR "ERROR: Make sure (min < avg < max)\n", return if ($avg + <= $min || $avg >= $max); push @a, rand($max-$min)+$min for 1..$howmany; # nudge list toward avg w/o pushing numbers above/below min/max while (avg_list(@a) != $avg){ my ($nudge, $tmp) = (avg_list(@a)-$avg, undef); last if (abs $nudge < $acceptable_laziness); for (@a){ $tmp = ($_ - $nudge); $_ = $tmp if ($min <= $tmp && $max >= $tmp); } } @a; } sub avg_list { my ($c, $t) = (0, 0); $t+=pop, $c++ while @_; ($t / $c) if $c } # 20 nums between 1 and 5 that average to 3 my @b = rand_list(20, 1, 5, 3); print Dumper(\@b);
In reply to Re: list of random numbers with given avarage
by pizza_milkshake
in thread list of random numbers with given avarage
by LupoX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |