Code:
// The function below generates a list of approximately normally distributed 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.floor(Math.random() * 2) == 1);
if (negative == true)
{
newValue = newValue * -1;
}
currentTotal = currentTotal + newValue;
};
var currentAverage = currentTotal / x;
averages.push(currentAverage);
};
return averages;
}
// The function below finds the probability that a number that is randomly generated from a normal distribution will be less than or equal to x.
function normCumProb(x,mean,stdDev,zScoreDist)
{
// First, convert x to a z-score.
var zScore = (x - mean) / stdDev;
// Next, find the number of entries in zScoreDist that are less than or equal to the z-score of x. Divide this number by the length of the distribution array to get the percent of all z-scores that are less than or equal to the z-score of x. This is the probability you want to calculate.
var valueCount = 0;
for (var z=0; z<zScoreDist.length; z++)
{
if (zScoreDist[z] <= zScore)
{
valueCount = valueCount + 1;
};
};
var normProbability = valueCount / zScoreDist.length;
return normProbability;
}
EDIT: I should note that I need help! The latter function works, but when I test it using X=2.4, mean=2 and stdDev = 2.5, I consistently get an 80% probability. My TI-84 says the answer is approximately 56%.