# JavaScript normal distribution

Discussion in 'Web Design and Development' started by moonman239, Mar 5, 2013.

1. ### moonman239 macrumors 68000

Joined:
Mar 27, 2009
#1
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%.

2. ### SrWebDeveloper macrumors 68000

Joined:
Dec 7, 2007
Location:
Alexandria, VA, USA
#2
You made no mention here of passing the value of zScoreDist to your function which is an array, apparently. Explain?

Why did you post the first function?

Your question seems to be about the latter function which you say works, but returns unexpected results. And what is a TI-84 ?! Are you a student being tasked to write some JS code for a course?

Please be specific as to what you need help with in terms of JS syntax or functions to help you accomplish your goal. We help you with that stuff, it's up to you to resolve the math.

3. Mar 7, 2013
Last edited: Mar 7, 2013

### 960design macrumors 68030

Joined:
Apr 17, 2012
Location:
Destin, FL
#3
A pretty awesome calculator. I prefer the TI-89 Titanium. Blondes Vs Brunettes, age old story.

You need to step through your variables using a dev tool.

What I noticed immediately is that your top function returns an average that is not used anywhere. Exactly what SrWeb was stating.

Your second function starts looking something like this:
normCumProb(2.4, 2, 2.5, ???). We are missing something.

4. ### SrWebDeveloper macrumors 68000

Joined:
Dec 7, 2007
Location:
Alexandria, VA, USA
#4
Ah, of course. These days I use the Mac calculators from the app store, plenty of really cool free ones.

Anyway, if you (OP) does not have a dev tool to step through simply add print or echo statements after *each* math statement to print the result and compare with your TI-84 each step. Simple as that. That will tell you if you goofed up syntax or in the math department. The usual culprits are forgetting about divisible by zero, not casting properly, looping through arrays without checking values for each iteration, unexpected negative values, not rounding off, etc. which means learn how PHP math functions work.

http://php.net/manual/en/ref.math.php