Hi.
I've experimented a little with Perlin Noise and noise generation in general the last few months. When I started, I mostly used the Rnd() statement to randomize numbers. The first problem that occured was that I wanted the number generator to return the same number when I passed the same variables to the function. I found the following function in a Perlin Noise tutorial, which I have been trying to convert from C# to VB.NET:
float noise::Noise2d(int x, int y)
{
int n;
n = x + y * 57;
n = (n<<13) ^ n;
float res = (float)( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff ) / 1073741824.0);
return res;
}
My VB.NET-translation looks like this:
Private Function Noise2D(ByVal x As Integer, ByVal y As Integer) As Single
Dim n As Integer
n = x + y * 57
n = (n << 13) Xor n
Dim res As Single = CSng(1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) And &H7FFFFFFF) / 1073741824.0)
End Function
The code works as it's supposed to in C#, but I keep getting Overflow error in VB.NET all the time. I can't honestly say that I've managed to figure out exactly *why* the code works in C#. I can understand the code and all used commands, but I'm not sure why VB.NET gets an overflow error, while C# don't. Could it be that the purpose of the generator is to cause an intentional overflow, but that C#, instead of returning an error, returns some kind of truncated number instead? I'm not sure myself, and if someone could maybe explain this to me, and explain how I might get it to work in VB.NET, or at least give me some kind of advice, I would be very grateful.
Thanks!