Click here to Skip to main content
14,661,413 members
Articles » General Programming » Programming Tips » General
Posted 3 Aug 2010


4 bookmarked

Bell Curve, Gaussian Function, or Normal Distribution

Rate this:
5.00 (2 votes)
Please Sign up or sign in to vote.
5.00 (2 votes)
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.
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)))
            Return (Percentage - ((1 - Percentage) * Percentage)) * (1 / Midpoint)
        End If
    End Function

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.
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))
        MatrixList.Add(New KeyValuePair(Of Double, Double)(0, 0))
        For i = 1 To Iterations
            Dim ThisPoint = GetBellCurvePoint(i / Iterations, Threshold)
            MatrixList.Add(New KeyValuePair(Of Double, Double)(ThisPoint, ThisPoint - MatrixList.Last.Key))
        Return MatrixList.ToArray
    End Function

public KeyValuePair<double, double>[] GetMatrix(int Iterations, double Threshold)
    List<KeyValuePair<double, double>> MatrixList = new List<KeyValuePair<double, double>>();
    MatrixList.Add(new KeyValuePair<double, double>(0, 0));
    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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

Comments and Discussions

-- There are no messages in this forum --