Random numbering

  • Thread starter Thread starter Barry
  • Start date Start date
B

Barry

I am looking for opinions on using random number
generation in vb.net, I am using randomize and then using
the rnd function, I have done a test and it seems to be
pretty much random, what I'd like to know if anybody does
know is if it is truly random.

The code i am using is

Private Sub btnRandom_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnRandom.Click
Dim MyValue As Integer
Dim iCount(6) As Long
Dim i As Integer
Dim iMinimum As Integer
Dim iMaximum As Integer
Dim iLowestVal As Long
Dim iHighestval As Long

For i = 1 To 1000000
Randomize()
MyValue = CInt(Int((6 * Rnd()) + 1))
iCount(MyValue) += 1
Next i
iMinimum = 1
iMaximum = 1
iLowestVal = iCount(1)
For i = 1 To 6
If iCount(i) > iCount(iMaximum) Then
iHighestval = iCount(i)
iMaximum = i
End If
If iCount(i) < iCount(iMinimum) Then
iMinimum = i
iLowestVal = iCount(i)
End If
Next

MessageBox.Show("The lowest value is for " & iMinimum
& " equaling " & _
iLowestVal & vbCrLf & _
"The highest value is for " & iMaximum & " equaling "
& _
iHighestval)
End Sub
 
You are still using the VB 6.0 way of generating random numbers. In .NET,
there is a random class:

Dim x as new Random()
x.Next(args)
 
* "Barry said:
I am looking for opinions on using random number
generation in vb.net, I am using randomize and then using
the rnd function, I have done a test and it seems to be
pretty much random, what I'd like to know if anybody does
know is if it is truly random.

There is no "truly random". The pseudo-random numbers are calculated using a
formula which expects a seed value. Using the same seed will result in
the same sequence of pseudo-random numbers. Often, the current time is
used as seed.
 
Dim x as new Random()
x.Next(args)

To get more random numbers, you should seed the random number generator:

Dim x As New Random(Environment.TickCount)
x.Next(args)
 
Back
Top