Public Class RandomNumber
Private Shared rngProvider As New RNGCryptoServiceProvider()
Private Shared bytes As Byte() = New Byte(3) {}
Public Shared Function [Next](ByVal max As Integer) As Integer
If max <= 0 Then
Throw New ArgumentOutOfRangeException("max")
End If
rngProvider.GetBytes(bytes)
Dim value As Integer = BitConverter.ToInt32(bytes, 0) Mod max
If value < 0 Then
value = -value
End If
Return value
End Function
End Class