Hello acolytes, scribes, monks, friars, abbots, bishops, pontiffs and saints,

The Problem:

I want to generate a list of random values. I want to specify 4 things:

  • How many numbers to generate
  • The minimum value of the generated numbers
  • The maximum value of the generated numbers
  • An average number that should match the avarage of the generated values
  • This is my basic approach:

    #!/usr/bin/perl -w use strict; my $mystart = 0; # What is the minimum value of random numbers you +want to generate my $myend = 100; # What is the maximum value of the random numbers +to generate my $count = 50; # How many numbers should be generated my $my_average = 50; # What is the decided avarage of the generated + numbers my $number; my @number; my $summ = 0; my $i = 0; my $real_average; print "\nList of random values:\n"; my $start = $mystart; my $end = $myend; while ($i < $count) { $number = rand ($end); if ($number >= $start) { @number = split(/\./, $number); # get integer of float $i++; $summ = $summ + $number[0]; $real_average = $summ / $i; print "Random number: $number[0]\n"; print "Average: $real_average\n\n"; } if ($my_average < $real_average) { # if real avarage is bigger than decided avarage # allow only smaller random numbers $end = $myend / 2; $start = $mystart; } elsif ($my_average > $real_average) { # if real avarage is smaller than decided # allow only bigger random numbers $end = $myend; $start = $myend / 2; } }

    All critic, input, code samples, good wishes, etc.. is highly welcome!

    All the best for you...LupoX


    In reply to 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.