Help me remove the Goto Statement

Tkeller

Board Regular
Joined
Jul 23, 2003
Messages
143
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I am not so great at VBA and have been trying to do something very simple" replace this GOTO for some kind of conditional loop, but for some reason I keep getting error messages.
VBA Code:
Function gauss()
        Dim fac As Double, R As Double, V1 As Double, V2 As Double
10    V1 = 2 * Rnd - 1
        V2 = 2 * Rnd - 1
        R = V1 ^ 2 + V2 ^ 2
        If (R >= 1) Then GoTo 10
        fac = Sqr(-2 * Log(R) / R)
        gauss = V2 * fac
End Function

I know this is simple to some of you here!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Use a "Do While" loop, i.e.
VBA Code:
Function gauss()
    
    Dim fac As Double, R As Double, V1 As Double, V2 As Double
        
    Do While R >= 1
        V1 = 2 * Rnd - 1
        V2 = 2 * Rnd - 1
        R = V1 ^ 2 + V2 ^ 2
    Loop
        
    fac = Sqr(-2 * Log(R) / R)
    gauss = V2 * fac
    
End Function
 
Upvote 0
Thank you but somehow that gives me an error message when I run it. The one with Goto works.
 
Upvote 0
I think it is because R=0 at the beginning of the loop, so it would never enter the loop. Try this version instead:
VBA Code:
Function gauss()
 
    Dim fac As Double, R As Double, V1 As Double, V2 As Double
     
    Do
        V1 = 2 * Rnd - 1
        V2 = 2 * Rnd - 1
        R = V1 ^ 2 + V2 ^ 2
        If R < 1 Then Exit Do
    Loop
     
    fac = Sqr(-2 * Log(R) / R)
    gauss = V2 * fac
 
End Function
 
Upvote 0
Excel already has a GAUSS() function, so not a good idea to name a UDF with the same name.
I think it is because R=0 at the beginning of the loop, so it would never enter the loop.
So the check could just be moved to the end of the loop.
(I've also included a Randomize)

VBA Code:
Function mygauss()
  Dim fac As Double, R As Double, V1 As Double, V2 As Double
  
  Randomize
  Do
    V1 = 2 * Rnd - 1
    V2 = 2 * Rnd - 1
    R = V1 ^ 2 + V2 ^ 2
  Loop While R >= 1
  fac = Sqr(-2 * Log(R) / R)
  mygauss = V2 * fac
End Function
 
Upvote 0

Forum statistics

Threads
1,215,504
Messages
6,125,183
Members
449,212
Latest member
kenmaldonado

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top