test
Page 1 of 1 [ 10 posts ] 

wbport
Snowy Owl
Snowy Owl

User avatar

Joined: 16 Sep 2012
Posts: 125

19 May 2014, 7:37 am

The "random" function in various programming languages returns a value between 0 and 1. I would like to get a random number whose mean value is zero and has a standard deviation of 1.

Any suggestions? TIA



michael517
Velociraptor
Velociraptor

User avatar

Joined: 3 Nov 2013
Age:52
Posts: 472

19 May 2014, 8:02 am

The function gasdev() has a Gaussian distribution, just Google that with "Numerical Recipes" and you should be able to find it.



LoveNotHate
Veteran
Veteran

User avatar

Joined: 12 Oct 2013
Age:44
Posts: 2,002
Location: Northern USA

19 May 2014, 12:11 pm

Programming languages usually have a "math library" that will have specific math functions.



eric76
Veteran
Veteran

User avatar

Joined: 31 Aug 2012
Posts: 9,878
Location: In the heart of the dust bowl

19 May 2014, 3:53 pm

"Normal Distribution" as opposed to "Poisson Distribution"?



ruveyn
Veteran
Veteran

User avatar

Joined: 21 Sep 2008
Age:78
Posts: 31,726
Location: New Jersey

19 May 2014, 6:48 pm

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



wbport
Snowy Owl
Snowy Owl

User avatar

Joined: 16 Sep 2012
Posts: 125

19 May 2014, 10:06 pm

ruveyn wrote:
The sum of an set identically distributed random variable approaches a normal distribution as the number of summands gets larger.


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.



wbport
Snowy Owl
Snowy Owl

User avatar

Joined: 16 Sep 2012
Posts: 125

24 May 2014, 7:09 am

I finally came up with

Quote:
var x1, x2, w, y1, y2;

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;
for Javascript. When I checked it out 5 million times (other tests were just as close):
Quote:
The mean is 0.00015259487781002997
min value = -5.252272050947282
max value = 5.012418628286076
Std deviation = 1.000017643101745


Thanks for the suggestions!



DRzero
Blue Jay
Blue Jay

User avatar

Joined: 10 Jun 2014
Age:45
Posts: 92
Location: USA

12 Jun 2014, 2:24 pm

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


wbport
Snowy Owl
Snowy Owl

User avatar

Joined: 16 Sep 2012
Posts: 125

12 Jun 2014, 6:02 pm

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.



beneficii
Veteran
Veteran

User avatar

Joined: 10 May 2005
Age:31
Posts: 4,766

13 Jun 2014, 8:57 pm

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:

Code:
// normal_distribution example
#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:

Code:
normal_distribution (5.0,2.0):
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.


_________________
My blog:

http://beneficii.blogspot.com/