View Single Post
 
Old 06-19-2014, 02:07 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,399
Default

And, here is the code for the SUB version. Declare two double variables in your calling code, then use them as parameters to Normal2, as per below. As always, never call randomize more than once in the life of your app.

Code:
Dim Return1, Return2 as Double, Mean as Double = 100.0, Sig as Double = 2.5 Randomize Normal2(Return1, Return2, Sig, Mean) ' Return1 and Return2 now contain a pair of normally distributed random numbers, ' weighted to have a mean of 100 with a sigma of 2.5
Code:
' ******************************************************************************************************************* ' *** GetGausse2 written as a sub, to return 2 values per call, instead of declaring static variables and *** ' *** storing second value for the alternate calls as in GetGausse. This results in more than 40% improvement *** ' *** in performance (Normal: 0.160 seconds per million calls, Normal2: 0.094 seconds per million calls on my *** ' *** current PC, VB.Net v12) - loquin *** ' ******************************************************************************************************************* Public Sub Normal2(ByRef Norm1 As Double, ByRef Norm2 As Double, Optional ByRef Sigma As Double = 1.0#, Optional ByRef Mean As Double = 0.0#) Dim t1, t2 As Double Call GAUSSE.GetGausse2(t1, t2) Norm1 = t1 * Sigma + Mean Norm2 = t2 * Sigma + Mean End Sub Private Sub GetGausse2(ByRef Value1 As Double, ByRef Value2 As Double) Dim Work1 As Double = 0.0#, Work2 As Double = 0.0#, Work3 As Double = 0.0# Const Two As Double = 2.0#, One As Double = 1.0# Work3 = Two Do Until Work3 < One Work1 = Two * Rnd() - One Work2 = Two * Rnd() - One Work3 = Work1 * Work1 + Work2 * Work2 Loop Work3 = Sqrt((-(Two) * Log(Work3)) / Work3) Value1 = Work1 * Work3 Value2 = Work2 * Work3 End Sub
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote