Update2: I don't think this is exactly how I did it before, but it does appear to work.

#! perl -slw use strict; use List::Util qw[ sum min max ]; our $N ||= 10_000; our $MEAN ||= 75; sub skewedRnd { my( $start, $end, $skewedMean ) = @_; my $low = $skewedMean - $start; my $high = $end - $skewedMean; return ( rand() < 1 - ( $skewedMean / ( $end - $start ) ) ) ? $start + rand( $low ) : $skewedMean + rand( $high ); } my @values = map skewedRnd( 0, 100, $MEAN ), 1 .. $N; printf "range 0 .. 100; Min: %f Max:%f Mean:%f \n", min( @values ), max( @values ), sum( @values ) / $N; __END__ [23:52:06.41] P:\test>425761 -N=1000000 -MEAN=99 range 0 .. 100; Min: 0.003021 Max:99.999969 Mean:98.999126 [23:52:28.58] P:\test>425761 -N=1000 -MEAN=99 range 0 .. 100; Min: 5.154236 Max:99.997009 Mean:98.727916 [23:52:41.92] P:\test>425761 -N=1000 -MEAN=20 range 0 .. 100; Min: 0.029907 Max:99.995117 Mean:19.924415 [23:52:51.27] P:\test>425761 -N=1000 -MEAN=10 range 0 .. 100; Min: 0.008850 Max:97.561035 Mean:10.563889 [23:52:55.64] P:\test>425761 -N=1000 -MEAN=75 range 0 .. 100; Min: 0.144196 Max:99.998474 Mean:74.406415 [23:53:01.49] P:\test>425761 -N=1000 -MEAN=90 range 0 .. 100; Min: 1.606750 Max:99.976501 Mean:90.610480 [23:53:08.42] P:\test>425761 -N=1000 -MEAN=51 range 0 .. 100; Min: 0.049805 Max:99.974579 Mean:50.391814 [23:53:33.88] P:\test>425761 -N=1000 -MEAN=51 range 0 .. 100; Min: 0.221008 Max:99.923737 Mean:50.118810 [23:53:36.24] P:\test>425761 -N=10000 -MEAN=51 range 0 .. 100; Min: 0.001556 Max:99.998505 Mean:51.276380 [23:53:40.10] P:\test>425761 -N=100000 -MEAN=51 range 0 .. 100; Min: 0.000000 Max:99.997009 Mean:51.018693 [23:53:44.44] P:\test>425761 -N=1000000 -MEAN=51 range 0 .. 100; Min: 0.000000 Max:99.998505 Mean:50.992506

UPDATE: Below here is wrong--

it produces values beyond the desired range. I tried to reproduce my code from a year ago (on the hard disk of a dead portable) from memory and gotit wrong. I'll try to update with a correct version when I remember it.

Warning: IANMathematician. But a year or so ago I wanted a similar skewed random distirbution, and after reading a lot of stuff I didn't understand, I came up with this, which sufficed for my purposes.

As far as I can tell, all the values in the range are possible, and there is no particular bias towards any one or range of values, but the distribution does tend towards the prechosen mean quite strongly:

#! perl -slw use strict; our $N ||= 10_000; our $MEAN ||= 75; sub skewedRnd { my( $start, $end, $skewedMean ) = @_; my $skewFactor = $skewedMean - ( ( $end - $start ) / 2 ); return $start + rand( $end - $start ) + rand( $skewFactor * 2 ); } my $total; $total += skewedRnd( 0, 100, $MEAN ) for 1 .. $N; print $total / $N; __END__ [21:56:57.38] P:\test>425761 -N=100000 -MEAN=75 75.060163192749 [21:57:13.74] P:\test>425761 -N=100000 -MEAN=32 32.1195869335938

I'll let the math guys pick it apart, and offer it on the basis that it is simple, understandable and served my purpose. Maybe it will serve yours too.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Re: Skew normally distributed random numbers (UPDATED!) by BrowserUk
in thread Skew normally distributed random numbers by Anonymous Monk

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.