"This item does not exist" and "Syntax error"

I’m new to programming, so I apologize in advance if I’m phrasing this question naively. Anyway, I’m trying to implement the following code (see below) that I found online for two functions to generate a pseudorandom Gaussian-distributed number. But when I try to use the code, I get the following errors:

App.Normal, line 1 This item does not exist: Normal = GetGausse*Sigma+Mean
App.GetGausse, line 17 Syntax error: Const Two = 2#, One = 1#
App.GetGausse, line 20 This item does not exist: GetGausse = dblReturn2
App.GetGausse, line 22 This item does not exist: Work3 = Two

and a number of similar errors with App.GetGausse

What am I doing wrong? How can I fix it?

[code]Private Function Normal(Optional Sigma As Double = 1, Optional Mean As Double = 0) As Double
Normal = GetGausse * Sigma + Mean
End Function

Private Function GetGausse() As Double
’ This Function returns a standard Gaussian random number
’ based upon the polar form of the Box-Muller transform.

’ since this calc is capable of returning two calculations per
’ call, it’s been set up to save the second calc for the next
’ pass through the function, saving some time.

’ Call the randomize function once (and ONLY once) in the life of the project.

Static blReturn2 As Boolean ’ Flag to calc new values, or return
’ previously calculated value. It defaults
’ to False on the first pass.
Static dblReturn2 As Double ’ Second return value

Dim Work1 As Double, Work2 As Double, Work3 As Double

Const Two = 2#, One = 1#

If blReturn2 Then ’ On odd numbered calls
GetGausse = dblReturn2
Else
Work3 = Two
Do Until Work3 < One
Work1 = Two * Rnd - One
Work2 = Two * Rnd - One
Work3 = Work1 * Work1 + Work2 * Work2
Loop
Work3 = Sqr((-(Two) * Log(Work3)) / Work3)
GetGausse = Work1 * Work3
’ a second valid value will be returned by Work2 * Work3.
’ Calculate it for the next pass. This will save some processing
dblReturn2 = Work2 * Work3
End If

blReturn2 = Not blReturn2 ’ and toggle the return value flag

End Function[/code]

Welcome to Xojo. You have copied non-xojo code into your project. Some programming languages use the convention that to return a function’s value, you use the function name. Xojo does not do it that way. In Xojo, you use the RETURN keyword to explicitly return a value. That accounts for 2 of your errors. The other 2 come from the fact that in Xojo, you cannot combine constant definitions the way the code you copied does.

[quote]App.Normal, line 1 This item does not exist: Normal = GetGausse*Sigma+Mean
[/quote]

Private Function Normal(Optional Sigma As Double = 1, Optional Mean As Double = 0) As Double return GetGausse * Sigma + Mean End Function

[quote]App.GetGausse, line 17 Syntax error: Const Two = 2#, One = 1#
[/quote]
Split this into 2 lines. Drop the pound signs.

Const two = 2 Const one = 1

[quote]App.GetGausse, line 20 This item does not exist: GetGausse = dblReturn2
[/quote]
Again, use return.

return dblReturn2

[quote]App.GetGausse, line 22 This item does not exist: Work3 = Two
[/quote]
This error is caused by the earlier Const error.

HTH,
Tim

Thanks so much, Tim! One follow-up question: why is the scope of these functions “private”? Would their functionality change if I changed the scope to “public”? It doesn’t seem to change much (if at all) when I do.

Remember that this wasn’t Xojo code. I’m not sure what the difference would have been in the original language, but for Xojo it controls where you can call the code from. Public means anywhere. Private is more restricted, but as you have seen, it doesn’t affect your current project.