473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VBA-Random Numbers

I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet point 3. Here is the code I came up with up until now.

Function RandomNormal(Mean As Double, StdDev As Double) As Double
Application.Volatile
Randomize
RandomNormal = (StdDev * Sqr(-2 * Log(Rnd)) * Cos(2 * 3.141596 * Rnd)) + Mean

End Function

I am having trouble understanding how to generate the random numbers into the range to figure out the standard deviation and the mean on the actual spreadsheet. I know this is probably very basic but I am a beginner in VBA and am learning it on my own. Please help!

Joel Justice

�� Change the name of Worksheet2 in your workbook to “RandomNormal”
�� Go to the Visual Basic Editor and create a new function that will generate Random Normal Deviates. These are random numbers with a given Mean and Standard Deviation. Remember your statistics? The function’s name should be “RandomNormal” and have two arguments, the Mean (double) and StdDev (double) and the function should return a Double as the value. The formula to create a random number with the normal (Guassian) distribution is the following:
R1 = Random Number between 0 and 1 (use the Rnd function)
R2 = Random Number between 0 and 1 (use the Rnd function)
S = Standard Deviation (input)
M = Mean (input)
Pi = Pi (3.1415926)
X = Random Number you generate (output)
X = S * Sqr(-2 * Log(R1)) * Cos(2 * Pi * R2) + M
�� You can test your function in the Immediate Window to see that it works properly.
�� On the Worksheet “RandomNormal” put the label “Mean” in cell A1, and the label “StdDev” in cell A2. Name cell B1 “Mean” and name cell B2 “StdDev”. Put an outline around both of these cells as input and unprotect them.
�� On the Worksheet “RandomNormal” create a range from Cells D5:P15 and name this range “RandomNumbers”.
�� In cell C1 insert a formula the computes the actual Mean (Average) of the Range “RandomNumbers”. In Cell C2 insert a formula the computes the actual Standard Deviation of the range “RandomNumbers”. When you perform the next steps those outputs should tell you if you doing things right. These values should be close to (but not exactly equal to) the input Mean and StdDev.
�� Create a subroutine in the Visual Basic Editor called “GenerateNormals”. This Sub will examine the range “RandomNumbers” to determine the number of rows and columns in the range. Using a nested loop, fill this range with Random
Normal Numbers using the “Mean” and “StdDev” that you defined on that page. Note: I will change the definition of the range and fill in values for Mean and StdDev. Place a Command Button on the page to execute the subroutine GenerateNormals.
Mar 3 '06 #1
3 24484
If I understand you correctly then the following will fill the area:

Dim i As Integer, j As Integer, rngArea As Range
Set rngArea = Range("RandomNumbers")
For i = 1 To rngArea.Columns.Count
For j = 1 To rngArea.Rows.Count
rngArea.Item(j, i).Value = RandomNormal(Range("B1").Value ,Range("B2").Value)
Next j
Next i

The formulae in the spreadsheet that you want are probably:
=AVERAGE(RandomNumbers)
and
=STDEV(RandomNumbers)
Mar 10 '06 #2
I am working through a VBA book ...
The formula: X = S * Sqr(-2 * Log(R1)) * Cos(2 * Pi * R2) + M works very nicely. But where did you find it? Even "Numerical Recipes in C" didn't give it.
-Philip
Apr 21 '06 #3
The formula: X = S * Sqr(-2 * Log(R1)) * Cos(2 * Pi * R2) + M works very nicely. But where did you find it? Even "Numerical Recipes in C" didn't give it.
-Philip
It is called the Box-Muller Transformation
Box, G.E.P. and Muller, M. E. "A Note on the Generation of Random Normal Deviates." Ann. Math. Stat. 29, 610-611, 1958.

Note that using Sin(2*Pi*R2) instead of Cos gives a second Normal Deviate, so for two uniform random numbers you get 2 normal ones.

Victor
Sep 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: guy lateur | last post by:
Hi all, I am trying to write some code (macro's, if you like) to glue together our Office applications (mainly Word, Excel and Outlook). We have a lot of different projects going on...
0
by: andreas | last post by:
Hi there, I have the following situation: 1. DOT.NET Application is started 2. DOT.NET Application instantiates Access.Application.8 3. Opens a specified database (MDB) 4. DOT.NET...
1
by: Igor Mendizabal | last post by:
Hello We have a VB6 application that integrates an VBA IDE from which the final user can make modifications. We want to pass the application to VB.NET, and the first step, before rethinking...
22
by: Illya Havsiyevych | last post by:
Hello How easily parse VB/VBA code by VB/VBA code ? Is any ready solutions ? Thank's, illya
41
by: Matt Alanzo | last post by:
Our SOHO 2 person compay sells furniture (not programmers). In '98 we paid $,$$$ for a VBA -Access '97 accounting application, including VBA source code .... an huge investment for us then (and...
6
by: Terry Bell | last post by:
We've had a very large A97 app running fine for the last seven years. I've just converted to SQL Server backend, which is being tested, but meanwhile the JET based version, running under terminal...
1
by: Igor Mendizabal | last post by:
Hello We have a VB6 application that integrates an VBA IDE from which the final user can make modifications. We want to pass the application to VB.NET, and the first step, before rethinking...
18
by: Ann Scharpf via AccessMonster.com | last post by:
I am not sure which would be the best place to post this question, so I'm posing it here with Access general questions. I have reached the point many times in Word and in Access where my ignorance...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
3
MMcCarthy
by: MMcCarthy | last post by:
Although some users find Macros simple and easy to use, there are some major limitations to using them. Although you can use macros to perform tasks, there is limited control on when and how those...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.