Random Number generator (For Perlin noise etc.) and conversion from C# to VB.NET

DecemberChild

Newcomer
Joined
Jul 27, 2004
Messages
4
Location
Scania, Sweden
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!
 
Which question are you trying to solve, how to get Random numbers - but repeatable for testing - or how to get the given function to work without overflow?

If you want repeatable random numbers, use the Random class. Here's some C#:
C#:
Random rnd = new Random(1);
for(int i=0; i<100; i++)
{
	Debug.WriteLine(rnd.Next(10));
}

Passing in a value to the constructor of Random forces the seed, which will result in "random" numbers that always follow the same rules.

As far as I know, C# and VB.NET should behave the same. C# doesn't have any built-in "auto-truncate on overflow without exception" logic that I know. I would force some extra casting on some numbers to narrow scope. I'm not sure where that function came from, but it's doing some "big" math on numbers that may not hold everything. For example, pass in Integer.MaxValue for both x and y and what happens?

Anyway, let us know more so we can help.

-ner
 
You haven't specified a return value in your example.

You need either "Return res" or "Noise2d = res" at the end - otherwise the default value of the Single type is returned (0.0).
 
There are only two differences that I can see between the two functions (C# and VB): that pointed out by Jaco (not returning res), and the overflow check issue pointed out by Nerseus. Otherwise, they seem perfectly identical.

I believe that by default C# does not perform overflow checks, whereas VB does by default, causing overflow errors in VB where one expects a value to be truncated in C#. Since VB is all or none in overflow checking, you can disable it in the project settings, which will probably resolve the overflow exception. Returning res will probably help alot once the overflow exception issue is resolved.
 
Back
Top