435,264 Members | 1,237 Online
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

# Random Numbers?!

 P: n/a Is this the correct way to generate random number? Dim myRandomObject As Random myRandomObject = New Random myRandomObject.Next(1, 4) Nov 21 '05 #1
5 Replies

 P: n/a "Hareth" schrieb: Is this the correct way to generate random number? Dim myRandomObject As Random myRandomObject = New Random myRandomObject.Next(1, 4) 'Next' will /return/ the number: \\\ Dim Generator As New Random() Dim RandomValue As Integer = Generator.Next(1, 5) /// .... will return a pseudo-random number between 1 and 4 (including 1 and 4). -- M S Herfried K. Wagner M V P V B Nov 21 '05 #2

 P: n/a It totally depends upon the usage of that Random #. If you are using that # as ID for some records and its to large # of data is excepted, then use GUID which is again one kind of Random #. But if you are using for some temporary logic and which is short, then use Random class. Mayur "Hareth" wrote in message news:O7**************@TK2MSFTNGP09.phx.gbl... Is this the correct way to generate random number? Dim myRandomObject As Random myRandomObject = New Random myRandomObject.Next(1, 4) Nov 21 '05 #3

 P: n/a You should supply a seed value when you instantiate the random class so that you get different sequences of random numbers each time the routine runs. Dim myRandomObject As New Random(Now.GetHashCode) Dim randomNumber as Integer = myRandomObject.Next(1, 4) Be aware that the code above will never return "4". In other words, the upper limit you specify will never actually be reached. If you want to get a random number that could be 1, 2, 3 or 4, you should supply "5" as the upper limit). "Hareth" wrote in message news:O7**************@TK2MSFTNGP09.phx.gbl... Is this the correct way to generate random number? Dim myRandomObject As Random myRandomObject = New Random myRandomObject.Next(1, 4) Nov 21 '05 #4

 P: n/a "Scott M." schrieb: You should supply a seed value when you instantiate the random class so that you get different sequences of random numbers each time the routine runs. Dim myRandomObject As New Random(Now.GetHashCode) Dim randomNumber as Integer = myRandomObject.Next(1, 4) The parameterless ctor of 'Random' will use a time-dependent seed value, which makes it unlikely that you get the same sequence of pseudo-random numbers twice. There is no benefit in using 'Now.GetHashCode' over using the parameterless constructor. -- M S Herfried K. Wagner M V P V B Nov 21 '05 #5

 P: n/a ACK "Herfried K. Wagner [MVP]" wrote in message news:uh**************@TK2MSFTNGP10.phx.gbl... "Scott M." schrieb: You should supply a seed value when you instantiate the random class so that you get different sequences of random numbers each time the routine runs. Dim myRandomObject As New Random(Now.GetHashCode) Dim randomNumber as Integer = myRandomObject.Next(1, 4) The parameterless ctor of 'Random' will use a time-dependent seed value, which makes it unlikely that you get the same sequence of pseudo-random numbers twice. There is no benefit in using 'Now.GetHashCode' over using the parameterless constructor. -- M S Herfried K. Wagner M V P V B Nov 21 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.