I rewrote your code and made a stab at my own.

Some of the changes I made to your code:

In my code, instead of only choosing numbers from part of the range, I weigh the results smaller or larger as needed. This is an advantage if the desired average is close to the real average, but a disadvantage if the desired average is radically different. The reason I consider it an advantage when the desired average is close to the real average is that you still could get the entire ranger of numbers; not a sequence of numbers below the average, then a sequence of numbers above the average, etc.

use strict; use warnings; # Comment out the tests you don't want to run. my @choices; @choices = (0, 100, 50, 50); @choices = (25, 75, 50, 50); @choices = (0, 100, 50, 10); my @numbers = his_method(@choices); #my @numbers = my_method(@choices); print "Results:\n@numbers\n"; exit; sub his_method { # re-written my $mystart = shift; my $myend = shift; my $count = shift; my $my_average = shift; my ($start, $end) = ($mystart, $myend); my ($number, @numbers); my ($sum, $real_average) = 0; print "\nList of random values:\n"; for (my $i = 1; $i <= $count; $i++) { $number = int(rand($end+1)) + $start; push @numbers, $number; $sum += $number; $real_average = $sum/$i; print "Random number $i: $number ($start, $end)\n"; print "Average: $real_average\n\n"; ($start, $end) = ($real_average < $my_average) ? # make larger ( $my_average, $myend-$my_average) : ($real_average > $my_average) ? # make smaller ( $mystart, $my_average - $mystart) : # if no adjustment needed. ( $mystart, $myend); } return @numbers; } sub my_method { my $mystart = shift; my $myend = shift; my $count = shift; my $my_average = shift; my ($number, @numbers); my ($sum, $real_average) = 0; my $next_adj = 1; print "\nList of random values:\n"; for (my $i = 1; $i <= $count; $i++) { $number = int(rand()**$next_adj * ($myend+1)) + $mystart; push @numbers, $number; $sum += $number; $real_average = $sum/$i; print "Random number $i: $number ($next_adj)\n"; print "Average: $real_average\n\n"; $next_adj = ($real_average < $my_average) ? # make larger .5 # so take square root of fraction : ($real_average > $my_average) ? # make smaller 2 # so take power of 2 : # if no adjustment needed. 1; } return @numbers; }

In reply to Re: list of random numbers with given avarage by johannz
in thread list of random numbers with given avarage by LupoX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.