Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 7 of 7

01252012, 03:32 PM #1
 Join Date
 Mar 2011
 Posts
 20
 Thanks
 5
 Thanked 0 Times in 0 Posts
Generating a random number taken from a normal distribution
Hello,
I need help creating a function in javascript that produces random numbers generated from a normal curve distribution with a mean and standard deviation that I can specify (and easily change). I would like only whole numbers and the ability to set reasonable maximum and minimums.
Thanks,
Adrian
01252012, 05:36 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,675
 Thanks
 208
 Thanked 2,596 Times in 2,574 Posts
Code:<script type = "text/javascript"> function rnd_snd() { return (Math.random()*21)+(Math.random()*21)+(Math.random()*21); } // Three random numbers between 1 and 1 added together. That will give a normal distribution with mean = 0 and standard deviation = 1. // The function will return a decimal with an average value of 0. function rnd(mean, stdev) { return Math.round(rnd_snd()*stdev + mean); } // Multiply the result by the standard deviation that you want, then add your desired mean. alert (rnd(38,2)) // mean, standard deviation </script>
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
Last edited by Philip M; 01252012 at 05:51 PM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
ac11ca (02032012)
01262012, 04:06 PM
#3
 Join Date
 Jan 2010
 Location
 Behind the Wall
 Posts
 4,071
 Thanks
 14
 Thanked 428 Times in 424 Posts
The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.André Behrens, NY Times Software Developer
01262012, 07:26 PM
#4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,675
 Thanks
 208
 Thanked 2,596 Times in 2,574 Posts
Not quite sure what you mean, but all I have done here is added three random numbers between 1 and 1 together. That will give a point (almost +3 to almost 3) on a normal distribution with mean = 0 and standard deviation = 1. This is also called standard normal distribution. Try it yourself:
For more info Google for BoxMuller transform.Code:<script type = "text/javascript"> var tot = 0; var its = 500; // iterations for (var i = 0; i<its; i++) { var randy = (Math.random()*21)+(Math.random()*21)+(Math.random()*21); tot += randy; document.write(randy); document.write("<br>") } alert (tot/its); // close to zero, especially if the number of iterations is increased to (say) 10000 </script>
Last edited by Philip M; 01262012 at 08:04 PM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
01262012, 10:08 PM
#5
 Join Date
 Jan 2010
 Location
 Behind the Wall
 Posts
 4,071
 Thanks
 14
 Thanked 428 Times in 424 Posts
The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.André Behrens, NY Times Software Developer
02032012, 09:43 PM
#6
 Join Date
 Mar 2011
 Posts
 20
 Thanks
 5
 Thanked 0 Times in 0 Posts
Thank you! Very useful, and I will have a play around with it soon.
Now things get a little more tricky: What about a chi square distribution (with 3 degrees of freedom)?
Cheers,
Adrian
02042012, 07:32 AM
#7
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,675
 Thanks
 208
 Thanked 2,596 Times in 2,574 Posts
Well, what about it? Google to find for example
http://www.fourmilab.ch/rpkp/experim...s/chiCalc.html
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
ac11ca (02062012)