in reply to list of random numbers with given avarage

Fun problem, here's a suggestion and a solution.

Your algorithm doesn't work very well if the desired average is near your minimum or maximum, like from 0 - 100, desired average = 10.

If you want random numbers from $start to $end, instead of $number = rand($end) and testing to see if it's bigger than $start, use $number = rand($end - $start) + $start.

This program will either choose a random number from $start to $average or from $average to $end, based on whether the actual average is lesser or greater than the target average.

I have no idea what the distribution will look like, or what you want the distribution to look like.

YuckFoo

#!/usr/bin/perl use strict; my ($MIN, $MAX, $NUM, $AVERAGE) = qw(0 100 64 32); my $small = $AVERAGE - $MIN; my $large = $MAX - $AVERAGE; my ($i, $rand, $total, $actual); for ($i = 1; $i <= $NUM; $i++) { if ($actual < $AVERAGE) { $rand = int(rand($large) + $AVERAGE); } else { $rand = int(rand($small) + $MIN); } $total += $rand; $actual = $total / $i; print "$rand $actual\n"; }