Generating a normal random variable

Click For Summary

Discussion Overview

The discussion revolves around generating normal random variables in Excel, particularly focusing on methods to efficiently produce these variables with specified mean and standard deviation during Monte Carlo simulations. Participants explore various algorithms and approaches, including the Box-Muller method and the central limit theorem, while addressing issues encountered in implementation.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant seeks a method to generate normal random variables using a random number between 0 and 1, noting performance issues with Excel's built-in functions during simulations.
  • Another participant mentions Knuth's algorithm for generating normal variates but does not provide details.
  • A participant describes a method involving polar coordinates and the transformation of uniform random variables into normal variates using the Box-Muller method.
  • Another approach is suggested, utilizing the central limit theorem by summing several uniform random variables to approximate a Gaussian distribution.
  • A participant shares their Excel code for generating normally distributed numbers but initially reports unexpected results, questioning the reliability of Excel's random number generator.
  • After identifying a mistake in their code, the same participant reports improved results, indicating that they are now obtaining values consistent with expected statistical properties.
  • Another suggestion is made to implement the proposed algorithm without an inner loop, emphasizing the need for certain mathematical functions.

Areas of Agreement / Disagreement

Participants express various methods for generating normal random variables, but there is no consensus on a single best approach. Some methods are discussed in detail, while others remain more speculative or less explored.

Contextual Notes

Participants note limitations in their implementations, including potential issues with Excel's random number generator and the need for careful coding to achieve the desired statistical properties. There are also unresolved mathematical steps in some proposed methods.

Physics_wiz
Messages
227
Reaction score
0
I'm trying to write a program in excel to generate random variables with mean mu and standard deviation sigma. I can simply refer to the worksheet function for it but it takes forever when I have it inside a loop doing a monte carlo simulation. There is one function in excel that returns a random number between 0 and 1 and I can refer to that function in the loop as many times as I want because it's not expensive (ie. doesn't take much time).

So, here's the problem:
I have mu, sigma, and a random number between 0 and 1.
I need to generate a random number from a normal distribution with mean mu and standard deviation sigma.

Anyone know how to do this?

Thanks.
 
Physics news on Phys.org
I know that Knuth has an algorithm but don't remember any details.

Anyway, I googled on "Normal Distribution" and "algorithm" and got a number of hits. One you might find interesting is this:
http://www.seio.es/test/Archivos/t62/t62bun.pdf .
 
Last edited by a moderator:
The "standard" method (I used it years ago) is to generate normal variates in pairs.
If you use exp(-(x2+y2)/2) as the integrand, convert to polar coordinates (r,a), then let u=r2/2, you can then choose u= -ln(X), a=2pi*Y, where X and Y are random numbers, and the normal variates will be rcos(a) and rsin(a), where r=(2u)1/2.
 
Use the central limit theorem. If you add several uniform random variables, the result is (roughly) Gaussian.

Here's an example from a (Verilog) program:

Code:
//
	// Calculate roughly Gaussian noise by applying the central limit theorem
	// to four [0,1] uniform random numbers. Multiply by sqrt(3) to fix the
	// variance at 1.
	//
	
	noise = ( ( ($random / 4294967295.0 + 0.5) +
		    	($random / 4294967295.0 + 0.5) +
		    	($random / 4294967295.0 + 0.5) +
		    	($random / 4294967295.0 + 0.5) ) - 2.0 ) * 1.732050808 * NOISE_AMPLITUDE;

The example given by mathman is the Box-Muller method, which is faster, but more difficult to understand.

http://en.wikipedia.org/wiki/Box-Muller_transform

Here's an excellent page with code for both methods:

http://www.dspguru.com/howto/tech/wgn2.htm

- Warren
 
Ok, so I made a small program in excel to test it, but it doesn't look right. Either I'm doing something wrong or excel's random number generator isn't good enough (probably the former).

Here's the code:
Code:
Sub mac()
With Sheets("Sheet1")

For counter = 1 To 1000

            For counter2 = 0 To 100
                Randomize
                U1 = Rnd
                Randomize
                U2 = Rnd
                V1 = 2 * U1 - 1
                V2 = 2 * U2 - 1
                S = V1 * V1 + V2 * V2
                If S >= 1 Then counter2 = 100
            Next
             
            X = Sqr(2 * Log(S) / S) * V1
            Y = Sqr(2 * Log(S) / S) * V2
            
            .Cells(counter, 1) = X
            .Cells(counter, 2) = Y
Next
End With
            
End Sub

This generates 2000 numbers in two columns that are supposed to be normally distrubuted with mean 0 and standard deviation 1. Right?

The problem is that the maximum number in the 2000 generated is .812241 and the minimum is -.82961. With a standard deviation of 1, I should see about 32% of the data outside of the range [-1, 1] but it looks like it's all in the range [-.8, .8]. What's going on here?
 
I found my mistake! :biggrin:

Here's how the code should look:
Code:
Sub mac()
With Sheets("Sheet1")

For counter = 1 To 1000

            For counter2 = 0 To 100
                Randomize
                U1 = Rnd
                Randomize
                U2 = Rnd
                V1 = 2 * U1 - 1
                V2 = 2 * U2 - 1
                S = V1 * V1 + V2 * V2
                If S < 1 Then counter2 = 100
            Next
             
            X = Sqr(-2 * Log(S) / S) * V1
            Y = Sqr(-2 * Log(S) / S) * V2
            
            .Cells(counter, 1) = X
            .Cells(counter, 2) = Y
Next
End With
            
End Sub

Now I'm getting 4 sigma events just like I should. :biggrin:
 
Suggestion: try coding the algorithm I proposed. You need log, sin, cos, and sqrt functions. However, you won't have an inner loop.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K