6
\$\begingroup\$

While surfing and through some experimentation, I have a new method to generate random numbers without using Randomize or Random or RND. It uses RNGCryptoServiceProvider Class for generating random numbers. We can generate a wide range of positive and negative random numbers with this. I built a function which generate positive and negative random numbers.I wish to share that code with you to make it much better.

We need to import Imports System.Security.Cryptography file to work with the function. Here is the code:

Public Sub Randomgenerator()
    Dim byte_count As Byte() = New Byte(6) {}
    Dim random_number As New RNGCryptoServiceProvider()
    random_number.GetBytes(byte_count)
    Dim Output As Integer = BitConverter.ToInt32(byte_count, 0)
    MsgBox(Output)
End Sub

Suggestions and tips to improve this function are invited.

\$\endgroup\$
7
\$\begingroup\$
  • This will be a better random number generator than the other options you mentioned, yes.
  • The code is nicely indented, but some whitespace between variable declarations and logic would be nice.

    Public Sub Randomgenerator()
        Dim byte_count As Byte() = New Byte(6) {}
        Dim random_number As New RNGCryptoServiceProvider()
    
        random_number.GetBytes(byte_count)
        Dim Output As Integer = BitConverter.ToInt32(byte_count, 0)
    
        MsgBox(Output)
    End Sub
    
  • Method names should be PascalCased, so Randomgenerator should be RandomGenerator. Don't use underscore in variable/parameter names. They should be camelCased. Please see the official MSDN capitalization recommendations for more information.

  • This is begging to be a function that returns a Integer. There should be a main routine that calls and displays the generated number.

    Public Function RandomGenerator() As Integer
        Dim byteCount As Byte() = New Byte(6) {}
        Dim randomNumber As New RNGCryptoServiceProvider()
    
        randomNumber.GetBytes(byteCount)
        Return BitConverter.ToInt32(byteCount, 0)
    End Function
    
    
    Sub Main()
        MsgBox(RandomGenerator())
    End Sub
    
  • MsgBox is depreciated and left only for backwards compatibility with . Replace it with a MessageBox.

  • RNGCryptoServiceProvider implements IDisposable. This means that you need to properly dispose randomNumber. From the documentation link you provided:

    This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

\$\endgroup\$
4
\$\begingroup\$

byte_count is not a good name for a variable of type Byte(), and why does the array have 6 elements? If you are going to return an Int32 you need 4.

\$\endgroup\$
  • 1
    \$\begingroup\$ AFAICT the OP's array has 7 elements, not 6. VB.net is silly like that ;) \$\endgroup\$ – CodesInChaos Aug 7 '14 at 16:52
4
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ I have updated my answer. I slipped up. Thank you for catching that. Please don't hesitate to comment on answers when they're wrong. It gives dolts like me a chance to correct themselves. =) \$\endgroup\$ – RubberDuck Aug 7 '14 at 10:14
  • 1
    \$\begingroup\$ @ckuhn203 Great! Ah, I'm sorry. I actually thought you genuinely meant it should return a Long. I'll post a comment if I encounter similar "issues" FNO. \$\endgroup\$ – Bjørn-Roger Kringsjå Aug 7 '14 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.