14,661,413 members
Articles » General Programming » Programming Tips » General
Tip/Trick
Posted 3 Aug 2010

20.7K views
4 bookmarked

# Bell Curve, Gaussian Function, or Normal Distribution

Rate this:
3 Aug 2010CPOL
Simple calculation to determin a value at a percentage along a bell curve. Also with an offcenter curve intersection thing....
The reason i'm posting this little code snippet is because i couldn't find anything like this. only math termonology which i don't understand, i just understand functions B)

at first i was trying to get a non-linear function for a volumn control, then we needed a distribution function for estimating materials we were going to need at work. so i came up with this finally.

VB.net
```Private Function GetBellCurvePoint(ByVal Percentage As Double, ByVal Midpoint As Double) As Double
If Percentage > Midpoint Then
Percentage = 1 - Percentage
Return 1 - ((Percentage - ((1 - Percentage) * Percentage)) * (1 / (1 - Midpoint)))
Else
Return (Percentage - ((1 - Percentage) * Percentage)) * (1 / Midpoint)
End If
End Function```

C#
```private double GetBellCurvePoint(double Percentage, double Midpoint)
{
if (Percentage > Midpoint) {
Percentage = 1 - Percentage;
return 1 - ((Percentage - ((1 - Percentage) * Percentage)) * (1 / (1 - Midpoint)));
} else {
return (Percentage - ((1 - Percentage) * Percentage)) * (1 / Midpoint);
}
}```

just provide the percentage along the curve you want the value for and the midpoint (normally 0.5)

this next function just gives you an even distribution along the curve depending on the iterations count, and i'm calling it threshold instead of midpoint here cause it makes more sense in my application. the value part of the keyvaluepair is the difference from the previous point.

VB.net
```Public Function GetMatrix(ByVal Iterations As Integer, ByVal Threshold As Double) As KeyValuePair(Of Double, Double)()
Dim MatrixList As New List(Of KeyValuePair(Of Double, Double))
For i = 1 To Iterations
Dim ThisPoint = GetBellCurvePoint(i / Iterations, Threshold)
MatrixList.Add(New KeyValuePair(Of Double, Double)(ThisPoint, ThisPoint - MatrixList.Last.Key))
Next
Return MatrixList.ToArray
End Function```

C#
```public KeyValuePair<double, double>[] GetMatrix(int Iterations, double Threshold)
{
List<KeyValuePair<double, double>> MatrixList = new List<KeyValuePair<double, double>>();
for (i = 1; i <= Iterations; i++) {
object ThisPoint = GetBellCurvePoint(i / Iterations, Threshold);
MatrixList.Add(new KeyValuePair<double, double>(ThisPoint, ThisPoint - MatrixList.Last.Key));
}
return MatrixList.ToArray();
}```

i believe this can also be used to calculate a gaussian function.
hope this helps someone that neeeded something like this!