Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: list of random numbers with given average

by I0 (Priest)
on Mar 13, 2002 at 04:17 UTC ( #151309=note: print w/ replies, xml ) Need Help??


in reply to list of random numbers with given avarage

#!/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 num +bers to generate my $count = 50; # How many numbers should be generated my $my_average = 50; # What is the decided avarage of the generate +d numbers my $number; my @number; my $summ = 0; my $i = 0; my $real_average; print "\nList of random values:\n"; for $i(1.. $count ){ my $start = $mystart; my $end = $myend; my $more = ($my_average*$count-$summ); my $average = $more/(1+$count-$i); if( ($count-$i)*$myend < $more-$mystart ){ $start = $more-($count-$i)*$myend; } if( ($count-$i)*$mystart > $more-$myend ){ $end = $more-($count-$i)*$mystart; } $number = random($start,$end,$average); $summ += int $number; $real_average = $summ / $i; print "Random number: $number\n"; print "Average: $real_average\n\n"; } sub random{ my($start,$end,$average) = @_; # print "$start,$end,$average\n"; if( $start >= $end ){ return $average; } my $b = ($average-$start)/($end-$start); if( 0 ){ my $p = $b<=.5?1/$b-1:(1-$b)/$b; #Update: this was silly return $start + ($end-$start)*(rand)**$p; }else{#Update: smoother skew function return $b >= 0.5 ? $start + ($end-$start)*(rand)**(1/$b-1) : $end + ($start-$end)*(rand)**(1/(1-$b)-1) ; } }


Comment on Re: list of random numbers with given average
Download Code
Re: Re: list of random numbers with given avarage
by LupoX (Pilgrim) on Mar 13, 2002 at 23:00 UTC

    A GREAT THANK YOU to all perlmonks for your input. Again and again I am impressed by the Perl Community!

    scribe LupoX

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://151309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-07-24 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (158 votes), past polls