Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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) ; } }

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://151309]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-07-27 13:08 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (413 votes). Check out past polls.