Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Generating a random gaussian number

  1. Jan 6, 2008 #1

    tony873004

    User Avatar
    Science Advisor
    Gold Member

    I want write a short block of computer code that allows me to generate a random gassuian number. For example, if the inputs are mean = 100 and sd = 10, if I generage 1000 random numbers, I'd like 67% of them to be between 90 and 110, 98% of them to be be between 80 and 120. But I only know how to generate random numbers between 2 different values. For example I can generate random numbers between 90 and 110, but 90 would have just as as much probability of being generated as 100. How can I generate numbers that would make a nice bell curve if I generated enough of them?

    Thanks.
     
  2. jcsd
  3. Jan 6, 2008 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Something like this? I don't know what language you are looking for, but Googling "generate gaussian random number" gives me tons of results (about 1.6 million, actually)
     
  4. Jan 6, 2008 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The standard approach to generating normal deviates is the Box-Müller algorithm. The polar form of the algorithm is very robust and has withstood many tests of normality.

    Here is a non-reentrant version of the algorithm. It assumes a function random_uniform that returns a random deviate drawn from U(0,1).

    Code (Text):

    extern double random_uniform();
    double random_gaussian() {
       static int have_deviate = 0;
       static double u1, u2;
       double  x1, x2, w;

       if (have_deviate) {
          return u2;
          have_deviate = 0;
       } else {
          do {
             x1 = 2.0 * random_uniform() - 1.0;
             x2 = 2.0 * random_uniform() - 1.0;
             w  = x1 * x1 + x2 * x2;
          } while ( w >= 1.0 );
          w  = sqrt ((-2.0 * ln (w))/w);
          u1 = x1 * w;
          u2 = x2 * w;
          return u1;
          have_deviate = 1;
       }
    }
     
     
  5. Jan 6, 2008 #4

    tony873004

    User Avatar
    Science Advisor
    Gold Member

    Thanks, CompuChip and DH.

    In the above code, what is the purpose of computing u2 if only u1 is returned and u2 isn't used again? I took CompuChip's advice and Googled this method to read a little more. It seems it assumes a mean of 0 and a standard deviation of 1. How can I modify this so I can specify the mean and standard deviation? And I'm only expecting one output. Is that in u1, u2, or do I have to perform an additional step once this function has been executed?

    Also, if I'm getting data from a table and it reads mean=100, uncertainty (1sd)= 10, does 10 represent the distance from the mean to 1sd, or the distance from -sd to sd. It's been years since I've taken a stats class. Thanks!!
     
  6. Jan 6, 2008 #5

    tony873004

    User Avatar
    Science Advisor
    Gold Member

    I figured it out. Multiply u1 by standard deviation and adding mean to it gives me my random output. I'm still not sure what good it did to compute u2, but if I ignore it everything works fine. Now I can plot beautiful gaussian curves. Thanks for your help!
     
  7. Jan 7, 2008 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The routine I supplied computes two guassian deviates, u1 and u2 and returns one of them (u1) on the first time it is called. On the second call, it simply returns u2 without computing anything. On the third call it computes two new gaussian deviates and returns one of them.
     
  8. Jan 7, 2008 #7

    EnumaElish

    User Avatar
    Science Advisor
    Homework Helper

    The general formula for generating a random variable distributed F is y = F-1(x) where x is the uniform random variable 0 < x < 1; i.e. fx(x) = 1. This follows from a theorem that states that for any random variable y distributed F, the random variable x = F(y) is the uniform random variable 0 < x < 1.
     
  9. Jan 8, 2008 #8
    Maybe, I can add another question here.

    What if I want to generate a multivariate Gaussian with singular covariance matrix...in this case there does not even exist a probability density function, hence no integral representation for the cumulative distribution function.

    Does somebody know what kind of algorithms is used to tackle this problem?

    Thanks

    Pere
     
  10. Jan 9, 2008 #9

    EnumaElish

    User Avatar
    Science Advisor
    Homework Helper

    This reduces to a vector of N independent random Gaussian variates (N being the "multi" in "multivariate"); so you can use the univariate algorithm N times.
     
  11. Jan 9, 2008 #10
    Does "singular covariance matrix" imply "independent"...?? I don't think so, but I may be mistaken ...
     
  12. Jan 9, 2008 #11

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    A singular covariance matrix means some of your random variables aren't random. Instead, some of your parameters are either deterministic or can be expressed as a linear combination of other parameters. I will assume your NxN covariance matrix has rank M > 1. (If the rank is one, you only have one random variable. If the rank is zero, you have no random variables.) This means that you can find some subset of size M of your N parameters such that the covariance matrix of these M parameters has rank M. Call this covariance matrix [itex]S_M[/itex]. This matrix by definition is positive definite so it has a (non-unique) square root, [itex]\sqrt{S_M}[/itex].

    Generate an M-vector [itex]x_M[/itex] of standard gaussian deviates by drawing each element from N(0,1). The M-vector [itex]x_M = \sqrt{S_M}\,u_M[/itex] will have covariance [itex]S_M[/itex]. Simply add the mean vector to this M-vector if the selected variables have non-zero mean.

    What about the remaining N-M parameters? They are completely determined by the already computed M random variables.
     
  13. Jan 9, 2008 #12

    EnumaElish

    User Avatar
    Science Advisor
    Homework Helper

    You are right; I interpreted (read) "singular covariance matrix" as "diagonal covariance matrix" (Cov[X,Y] = 0 for X [itex]\ne[/itex] Y).
     
    Last edited: Jan 9, 2008
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Generating a random gaussian number
  1. Random Number generator? (Replies: 12)

Loading...