Alex Dueppen Alex Dueppen -4 years ago 140 Question

What's the difference between the Int() method and Math.Floor() methods in VB.NET?

I'm trying to generate positive random integers between 2 values and the MSDN lists this formula as generating random numbers between a certain range:

randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound

The page later lists an example that appears to use Int() intstead of Math.Floor():

Dim value As Integer = CInt(Int((6 * Rnd()) + 1))

Is there any difference between the Int method and Math.Floor in this situation?

Answer Source

Int() is the legacy function that existed in VB before .NET came around.

Source is not included in the Reference Source, but a decent decompiler can show you what it does. Navigate to the Microsoft.VisualBasic.Conversion class to see:

public static double Int(double Number)
    return Math.Floor(Number);

No surprises, use what ever flavor you prefer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download