Needed: RNGCryptoServiceProvider function to return integer within range

  • Thread starter Thread starter Mark Jones
  • Start date Start date
M

Mark Jones

I need a little hlep.

Can anyone show me how to use RNGCryptoServiceProvider
to return an integer value within a specified range using vb.net?
The function would look something like this

Private Function RandInt(loVal as Integer, hiVal as integer) as Integer
'[INSERT CODE TO USE RNGCryptoServiceProvider Here]
End Function

I know I need to import System.Security.Cryptography
I know I need to use a 4 byte array as a buffer.
I just can't quite get it to work with the convert object.

TIA
 
Function RandInt(ByVal loVal As Int32, ByVal hiVal As Int32) As Int32

Static r As Random = Nothing

If r Is Nothing Then
Dim seed() As Byte = New Byte(3) {}
Dim rng As New RNGCryptoServiceProvider
rng.GetBytes(seed)
r = New Random(BitConverter.ToInt32(seed, 0))
End If

Return r.Next(loVal, hiVal + 1)

End Function

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


Mark Jones said:
I need a little hlep.

Can anyone show me how to use RNGCryptoServiceProvider
to return an integer value within a specified range using vb.net?
The function would look something like this

Private Function RandInt(loVal as Integer, hiVal as integer) as Integer
'[INSERT CODE TO USE RNGCryptoServiceProvider Here]
End Function

I know I need to import System.Security.Cryptography
I know I need to use a 4 byte array as a buffer.
I just can't quite get it to work with the convert object.

TIA
 
Excellent. That is exactly the routine I was noodling with.
When I was reading the helpfile on random, I saw the crypto
routine mentioned and figured it might be useful as an alternative
to using the clock directly as a seed to random.

I think I was getting stung by not using the {} at the end
of the byte array declaration.

I know I TIA'ed but thank you again anyway.

Mick Doherty said:
Function RandInt(ByVal loVal As Int32, ByVal hiVal As Int32) As Int32

Static r As Random = Nothing

If r Is Nothing Then
Dim seed() As Byte = New Byte(3) {}
Dim rng As New RNGCryptoServiceProvider
rng.GetBytes(seed)
r = New Random(BitConverter.ToInt32(seed, 0))
End If

Return r.Next(loVal, hiVal + 1)

End Function

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


Mark Jones said:
I need a little hlep.

Can anyone show me how to use RNGCryptoServiceProvider
to return an integer value within a specified range using vb.net?
The function would look something like this

Private Function RandInt(loVal as Integer, hiVal as integer) as Integer
'[INSERT CODE TO USE RNGCryptoServiceProvider Here]
End Function

I know I need to import System.Security.Cryptography
I know I need to use a 4 byte array as a buffer.
I just can't quite get it to work with the convert object.

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top