Skip to main content
deleted 157 characters in body
Source Link

I just want add a few points to the other (great) answers.

  • The name RandomGenerator sounds more like a class name rater than a function name. The name should reflect the action / result of the function. A more convenient name would be GenerateRandom.

  • When working with objects implementing IDisposable it's a good practice to use the Using keyword. This way, you are guaranteed that the object is disposed even if an error occurs.

  • I disagree with @ckuhn203 when saying "begging to return a long". The BitConverter.ToInt32 returns an Integer (Int32), so should the function.

  • An Integer is 32-bit (4 bytes), so the array should be sized 3 aka. 4 - 1. (Think of it as the last index: 0,1,2,3)


Result:

Public Function GenerateRandom() As Integer
    Using provider As New RNGCryptoServiceProvider()
        Dim data As Byte() = New Byte(4 - 1) {}
        provider.GetBytes(data)
        Return BitConverter.ToInt32(data, 0)
    End Using
End Function

I just want add a few points to the other (great) answers.

  • The name RandomGenerator sounds more like a class name rater than a function name. The name should reflect the action / result of the function. A more convenient name would be GenerateRandom.

  • When working with objects implementing IDisposable it's a good practice to use the Using keyword. This way, you are guaranteed that the object is disposed even if an error occurs.

  • I disagree with @ckuhn203 when saying "begging to return a long". The BitConverter.ToInt32 returns an Integer (Int32), so should the function.

  • An Integer is 32-bit (4 bytes), so the array should be sized 3 aka. 4 - 1. (Think of it as the last index: 0,1,2,3)


Result:

Public Function GenerateRandom() As Integer
    Using provider As New RNGCryptoServiceProvider()
        Dim data As Byte() = New Byte(4 - 1) {}
        provider.GetBytes(data)
        Return BitConverter.ToInt32(data, 0)
    End Using
End Function

I just want add a few points to the other (great) answers.

  • The name RandomGenerator sounds more like a class name rater than a function name. The name should reflect the action / result of the function. A more convenient name would be GenerateRandom.

  • When working with objects implementing IDisposable it's a good practice to use the Using keyword. This way, you are guaranteed that the object is disposed even if an error occurs.

  • An Integer is 32-bit (4 bytes), so the array should be sized 3 aka. 4 - 1. (Think of it as the last index: 0,1,2,3)


Result:

Public Function GenerateRandom() As Integer
    Using provider As New RNGCryptoServiceProvider()
        Dim data As Byte() = New Byte(4 - 1) {}
        provider.GetBytes(data)
        Return BitConverter.ToInt32(data, 0)
    End Using
End Function
Source Link

I just want add a few points to the other (great) answers.

  • The name RandomGenerator sounds more like a class name rater than a function name. The name should reflect the action / result of the function. A more convenient name would be GenerateRandom.

  • When working with objects implementing IDisposable it's a good practice to use the Using keyword. This way, you are guaranteed that the object is disposed even if an error occurs.

  • I disagree with @ckuhn203 when saying "begging to return a long". The BitConverter.ToInt32 returns an Integer (Int32), so should the function.

  • An Integer is 32-bit (4 bytes), so the array should be sized 3 aka. 4 - 1. (Think of it as the last index: 0,1,2,3)


Result:

Public Function GenerateRandom() As Integer
    Using provider As New RNGCryptoServiceProvider()
        Dim data As Byte() = New Byte(4 - 1) {}
        provider.GetBytes(data)
        Return BitConverter.ToInt32(data, 0)
    End Using
End Function