dcsimg
Results 1 to 3 of 3

Thread: Need help building a normal distribution function in JavaScript.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    183

    Need help building a normal distribution function in JavaScript.

    Hello to the members of the most wonderful Windows development community in the world! I have come to seek your help.

    I'm building a function that I can use to approximate the normal distribution curve. However, the distribution I get appears to be too lopsided. Below is the code I'm using.

    Code:
    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.round(Math.random() * 1) == 1);
    if (negative == true)
    {
    newValue = newValue * -1;
    }
    
    currentTotal = currentTotal + newValue;
    };
    var currentAverage = currentTotal / x;
    averages.push(currentAverage);
    };
    return averages;
    }
    I think the code is pretty self-explanatory. The statistician & programmer in me say that that's because either the size of each sample is too small or JavaScript always rounds 0.5 to 1, resulting in the probability of picking a number that JavaScript would round to 1 being > 50%, thereby causing JavaScript to be biased towards negative numbers.
    Do not read this sentence.
    You read that last one, didn't you?
    Darn. You now read the previous two.

    Check out my pins on Pinterest!

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    183

    Re: Need help building a normal distribution function in JavaScript.

    Bump!
    Do not read this sentence.
    You read that last one, didn't you?
    Darn. You now read the previous two.

    Check out my pins on Pinterest!

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,873

    Re: Need help building a normal distribution function in JavaScript.

    Why would it not always round .5 to 1??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width