HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Normal distribution array function

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraydistributionfunctionnormal

Problem

I have a function that produce an array with values that are Gaussian (normal) distribution. If you plot the values on a graph, it would look something like this:

Is there any way to improve on the code?

private static IComparable[] NonUniformDistributionsGaussian(int startNumber,int arraySize)
    {
        IComparable [] arr = new IComparable[arraySize];
        Double[] intervals = { ((1.00 / 6.00) * arraySize), ((2.00 / 6.00) * arraySize),
                                 ((3.00 / 6.00) * arraySize),((4.00 / 6.00) * arraySize),
                                 ((5.00 / 6.00) * arraySize), ((6.00 / 6.00) * arraySize) };

        for (var i = 0; i < arraySize; i++)
        {

            if (i <= (int)intervals[0])
            {
                startNumber = startNumber + 1;                   
            }

            else if ( i <= (int)intervals[1])
            {
                 startNumber = startNumber  + 2;                    
            }
            else if (i <= (int)intervals[2])
            {
                startNumber = startNumber + 3;
            }
            else if (i <= (int)intervals[3])
            {
                startNumber = startNumber - 3;
            }
            else if (i <= (int)intervals[4])
            {
                startNumber = startNumber - 3;
            }
            else 
            {
                 startNumber = startNumber  - 2;                     
            }
            arr[i] = startNumber;
        }
        return arr;
    }

Solution

As the conditions of the if statements only depending on some fixed numbers and the arraysize input parameter these values can be precalculated.

int oneThirdArraySize = (int)((1.00 / 3.00) * arraySize);


and then used in the loop like

for (var i = 0; i < arraySize; i++)
{

    if (i <= oneThirdArraySize)
    {
        startNumber = startNumber + 1;
        arr[i] = startNumber;
    }
    // and so on
}


By precalculation of the right side values of the if conditions outside of the loop you can speed this up because right now you do these calculation up to two times for every iteration.

But we can do better because you are using some magic numbers here which we can hide behind some meaningful const variables.

private static const double oneThird = 1d / 3d;
private static const double twoThird = 2d / 3d;


In this way the calculations need to be changed to

int oneThirdArraySize = (int)(oneThird * arraySize);

Code Snippets

int oneThirdArraySize = (int)((1.00 / 3.00) * arraySize);
for (var i = 0; i < arraySize; i++)
{

    if (i <= oneThirdArraySize)
    {
        startNumber = startNumber + 1;
        arr[i] = startNumber;
    }
    // and so on
}
private static const double oneThird = 1d / 3d;
private static const double twoThird = 2d / 3d;
int oneThirdArraySize = (int)(oneThird * arraySize);

Context

StackExchange Code Review Q#69969, answer score: 5

Revisions (0)

No revisions yet.