|
-
Mar 4th, 2013, 09:34 PM
#1
Thread Starter
Addicted Member
Need help building a normal distribution function in JavaScript.
Hello to the members of the most wonderful Windows development community in the world! I have come to seek your help.
I'm building a function that I can use to approximate the normal distribution curve. However, the distribution I get appears to be too lopsided. Below is the code I'm using.
Code:
z-scores. The z-scores are between -4 and 4. This approximation of the normal distribution is good enough for most purposes.
function zScoreDistribution()
{
var averages = new Array();
for (i=1;i<=2000;i++)
{
var x=0;
var currentTotal = 0;
for (x=1;x<=200;x++)
{
var newValue = Math.floor(Math.random() * 5);
var negative = (Math.round(Math.random() * 1) == 1);
if (negative == true)
{
newValue = newValue * -1;
}
currentTotal = currentTotal + newValue;
};
var currentAverage = currentTotal / x;
averages.push(currentAverage);
};
return averages;
}
I think the code is pretty self-explanatory. The statistician & programmer in me say that that's because either the size of each sample is too small or JavaScript always rounds 0.5 to 1, resulting in the probability of picking a number that JavaScript would round to 1 being > 50%, thereby causing JavaScript to be biased towards negative numbers.
-
Mar 5th, 2013, 05:01 PM
#2
Thread Starter
Addicted Member
Re: Need help building a normal distribution function in JavaScript.
-
Mar 5th, 2013, 07:57 PM
#3
Re: Need help building a normal distribution function in JavaScript.
Why would it not always round .5 to 1??
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|