|
|
The RNGCryptoServiceProvider Class implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). Here’s how to generate random numbers using this class. Make sure you import the System.Security.Cryptography namespace before using this sample code
C#
static void GenerateRandomNumber()
{
byte[] byt = new byte[4];
RNGCryptoServiceProvider rngCrypto =
new RNGCryptoServiceProvider();
rngCrypto.GetBytes(byt);
int randomNumber = BitConverter.ToInt32(byt, 0);
Console.WriteLine(randomNumber);
}
VB.NET
Shared Sub GenerateRandomNumber()
Dim byt As Byte() = New Byte(4) {}
Dim rngCrypto As New RNGCryptoServiceProvider()
rngCrypto.GetBytes(byt)
Dim randomNumber As Integer = BitConverter.ToInt32(byt, 0)
Console.WriteLine(randomNumber)
End Sub
'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |




comments
0 Responses to "Generate Stronger Random Numbers using C# and VB.NET"Post a Comment