473,378 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Needed: RNGCryptoServiceProvider function to return integer within range

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
3 7027
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: EsC | last post by:
Hy! is it possible to pass function-arguments by reference? (for example in PHP you can use the "&" operator ... ) thx iolo
3
by: ma740988 | last post by:
Consider the template member function that generates a random number within a range. #include <cstdlib> #include <ctime> #include <iostream> template<typename T> T Random(T bottom, T top) {
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
1
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h hi iam done with my hw i still have to do one function which is...
18
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. ...
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
0
by: Mark Jones | last post by:
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 ...
5
by: Buchwald | last post by:
hello group, I have a long (large) script that shows a random picture when a webpage is refreshed. It's long because i have a lot of pictures: 246 Here is some code:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.