Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Generating a random number taken from a normal distribution
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-25-2012, 04:32 PM PM User | #1 ac11ca New Coder   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
 01-25-2012, 06:36 PM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts Code: `````` All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit. __________________ 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. Last edited by Philip M; 01-25-2012 at 06:51 PM..
 Users who have thanked Philip M for this post: ac11ca (02-03-2012)
01-26-2012, 05:06 PM   PM User | #3
Dormilich
Senior Coder

Join Date: Jan 2010
Location: Behind the Wall
Posts: 3,086
Thanks: 11
Thanked 327 Times in 323 Posts
Quote:
 Originally Posted by Philip M Code: ```// 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.```
out of interest, do you have any detailed info about that statement?
__________________
Quote:
 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

01-26-2012, 08:26 PM   PM User | #4
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,473
Thanks: 200
Thanked 2,469 Times in 2,447 Posts
Quote:
 Originally Posted by Dormilich out of interest, do you have any detailed info about that statement?
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:-

Code:
```<script type = "text/javascript">

var tot = 0;
var its = 500; // iterations
for (var i = 0; i<its; i++) {
var randy = (Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1);
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>```
__________________

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.

Last edited by Philip M; 01-26-2012 at 09:04 PM..

01-26-2012, 11:08 PM   PM User | #5
Dormilich
Senior Coder

Join Date: Jan 2010
Location: Behind the Wall
Posts: 3,086
Thanks: 11
Thanked 327 Times in 323 Posts
Quote:
 Originally Posted by Philip M For more info Google for Box-Muller transform.
thanks, that was the bit of info I wanted.
__________________
Quote:
 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

 02-03-2012, 10:43 PM PM User | #6 ac11ca New Coder   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
02-04-2012, 08:32 AM   PM User | #7
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,473
Thanks: 200
Thanked 2,469 Times in 2,447 Posts
Quote:
 Originally Posted by ac11ca Now things get a little more tricky: What about a chi square distribution (with 3 degrees of freedom)?
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 (02-06-2012)

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:57 AM.