Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I finally did something that solves the problem partially.

The two following functions do that. The first function, rands will return a "standard" randomized integer. It has two input values: The first input variables denote the range of the output. (i.e. 0 to input) The second one will denote the precision: Higher it is, closer the result will be to the normal distribution. 5 is more than enough for many purposes.

The second function (random) is a little bit more tricky. The first input is the range, just like in the first function. The second input is a string which can either be "low" or "high". The third input is a limit. If the second input is "low", then the third input denotes the lowest possible number. If it is high, then it denotes the highest possible number. All the results that are outside either the lowest or the highest frequency will be ignored and "re-rolled".

Here is the snippet:
sub rands{ my $t=0; $t = 0; $t+= ( rand()* ($_[0]/$_[1]) ) for(1..$_[1]); return int($t+1); } sub random{ my ($no,$type,$limit) = @_; $t = rands($no,5); if($type eq "low"){ while ($t<$limit){ $t = rands($no,5) } } elsif($type eq "high"){ while ($t>$limit){ $t = rands($no,5) } } }
I was not able to "skew" my results, so I used this _harsh_ method of cutting off instead.

One final note: You can set the second input variable of rands to a low number (2,3) to get a more homogenous distribution, i.e. a distribution with "high variance".

Sinan

In reply to Re: How do I get random numbers that follow standard distribution? by sinan
in thread How do I get random numbers that follow standard distribution? by sinan

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-03-28 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found