By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

Needed: RNGCryptoServiceProvider function to return integer within range

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <no**************@devnullbucket.org> wrote in message
news:41**********************@news.twtelecom.net.. .
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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.805 / Virus Database: 547 - Release Date: 03/12/2004
Nov 21 '05 #2

P: n/a
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" <no**************@devnullbucket.org> wrote in message
news:41**********************@news.twtelecom.net.. .
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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.805 / Virus Database: 547 - Release Date: 03/12/2004
Nov 21 '05 #3

P: n/a
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" <EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:On*************@TK2MSFTNGP14.phx.gbl...
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" <no**************@devnullbucket.org> wrote in message
news:41**********************@news.twtelecom.net.. .
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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.805 / Virus Database: 547 - Release Date: 03/12/2004

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.