Normal distribution from a random function.
The sum of an set identically distributed random variable approaches a normal distribution as the number of summands gets larger.
See http://en.wikipedia.org/wiki/Central_limit_theorem
ruveyn
Until I can work on it, that is what I am doing now. After taking the sum of a random number between 0 and 1 and subtracting 0.5 twenty times, I multiply that sum by 0.7748. That final factor was arrived at by trial and error.
I finally came up with
do {
x1 = 2.0 * Math.random() - 1.0;
x2 = 2.0 * Math.random() - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );
w = Math.sqrt( (-2.0 * Math.log( w ) ) / w );
return x1 * w;
// y2 = x2 * w;
min value = -5.252272050947282
max value = 5.012418628286076
Std deviation = 1.000017643101745
Thanks for the suggestions!
You need to feed your random number (between 0 and 1) into the inverse of the cumulative distribution function of the standard Gaussian (w/ mean 0 and variance 1).
I'm a newbie here and just discovered I'm not allowed to post exact addresses of Web pages. I cannot even write the three-letter abbrevation for such addresses that begins with "U". But Wolfram has a page on its "InverseCDF" function with a nice graph. Wolfram doesn't give an explicit formula for it in terms of regular functions or even other special functions, so I'm guessing there isn't one. That page also has the Mathematica command for it, so if you're using Mathematica, you're done.
I'd guess most scientific or mathematical programming languages have this function built-in. For Matlab, for example, see the mathworks online documentation for its command "norminv".
_________________
DRzero
The math functions in JavaScript are a little limited and I like knowing what is "under the hood". That was part of a script which will implement rubato in a music notation program.
Speaking of music, do you play the viola or some other instrument where middle C can get reassigned? I note your icon is a C-clef.
It looks like you're using C++. Have you tried C++11, yet? The newest version of C++ has an extensive <random> library now.
Here is an example of the new std::normal_distribution class included in <random>:
http://www.cplusplus.com/reference/rand ... tribution/
In its constructor, you can set the mean and the standard deviation:
http://www.cplusplus.com/reference/rand ... tirbution/
Note that you will also need to use a generator class--these are also included in <random> to generate the numbers; the std::normal_distribution object will then apply normal distribution to the numbers generated.
I find <random> to be a very useful new addition to the C++ standard library.
From the second link, code that accomplishes what you want with the mean set to 0.0 and the standard deviation set to 1.0:
#include <iostream>
#include <chrono>
#include <random>
int main()
{
// construct a trivial random generator engine from a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::normal_distribution<double> distribution (0.0,1.0);
std::cout << "some Normal-distributed(0.0,1.0) results:" << std::endl;
for (int i=0; i<10; ++i)
std::cout << distribution(generator) << std::endl;
return 0;
}
It seeds the generator, of std::default_random_engine class with a seed obtained from the current time, to produce pseudo-random numbers. Then a std::normal_distribution object, specialized with numbers of real type double, is created with the mean set to 0.0 and the standard deviation set to 1.0. It then generates some results.
On the first page, the main page for std::normal_distribution, it shows how, with the mean set to 5.0 and the standard deviation set to 2.0, pseudo-random numbers are generated in a normal distribution, in the output:
0-1: *
1-2: ****
2-3: *********
3-4: ***************
4-5: ******************
5-6: *******************
6-7: ***************
7-8: ********
8-9: ****
9-10: *
I hope this is helpful to you.
| Similar Topics | |
|---|---|
| Creating a random point under the standard distribution curv |
05 Dec 2013, 8:15 pm |
| A Million Random Digits with 100,000 Normal Deviates |
17 Jan 2009, 12:58 am |
| Aversion to opposite sex normal? Plus random rant/questions |
18 Nov 2014, 1:27 am |
| Diet and Brain Function: Eating Your Way to Higher Function |
09 Mar 2009, 10:20 pm |
