patternMinor
Cryptographic Random Number Generator function implementation
Viewed 0 times
randomnumberfunctiongeneratorcryptographicimplementation
Problem
While surfing and through some experimentation, I have a new method to generate random numbers without using
We need to import
Suggestions and tips to improve this function are invited.
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 SubSuggestions and tips to improve this function are invited.
Solution
- 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 vb6. 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.
Code Snippets
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 SubPublic 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 SubContext
StackExchange Code Review Q#59286, answer score: 7
Revisions (0)
No revisions yet.