Problems with Random Numbers

  • Thread starter Thread starter Ricardo
  • Start date Start date
R

Ricardo

Hi wizards, I have an object , this object generate a set of 9 random
numbers. When I generate for example 10 object his 9 random numbers
are equals. How can I make this 9 numbers are different for each object


I use this code in Sub New :

Dim RandomNumber As Integer
Dim RandomClass As New Random

For i = Weights.GetLowerBound(0) To Weights.GetUpperBound(0)
For j = Weights.GetLowerBound(1) To
Weights.GetUpperBound(1)
RandomNumber = RandomClass.Next()
Weights(i, j) = Convert.ToDouble((RandomNumber /
(Integer.MaxValue / 6)) - 3)


Next
Next
 
not necessarily, you can also specifiy the lower bound (inclusive) and the
upper bound (non-inclusive)

Dim rnd As New Random
Dim intRnd as Integer = rnd.Next(0, 501)


that would get a random number between 0 and 500
 
Iwdu15

And why is that than in any way better than using the Time?

What you show is fixed and every fixed part is always less randomized than
using the time now.

Just my idea,

Cor
 
So as a mild musing, I made a shared class for making random numbers. Only
I am using the hashcode of a guid as my seed.

Public NotInheritable Class SimpleRandomizer

Private Shared pintSeed As Integer

Shared Sub New()
Dim g As Guid

g = System.Guid.NewGuid

pintSeed = g.GetHashCode

Randomize(CType(pintSeed, Double))
End Sub

Public Shared Function GetRandom(ByVal MaxValue As Integer, ByVal MinValue
As Integer) As Integer

'Test values
If MaxValue < 0 Then Throw New RandomizerException("Max value must be
greater than 0.", New RandomizerException.RandomizerInputs(pintSeed,
MaxValue, MinValue))
If MaxValue < 0 Then Throw New RandomizerException("Min value must be
greater than 0.", New RandomizerException.RandomizerInputs(pintSeed,
MaxValue, MinValue))
If MaxValue < MinValue Then Throw New RandomizerException("Min value
must be greater than 0.", New RandomizerException.RandomizerInputs(pintSeed,
MaxValue, MinValue))

Try
Return CInt(Int((MaxValue - MinValue + 1) * Rnd() + MinValue))
Catch ex As Exception
Throw New RandomizerException("Failed to generate random number", ex,
New RandomizerException.RandomizerInputs(pintSeed, MaxValue, MinValue))
End Try

End Function

Public Class RandomizerException
Inherits System.ApplicationException

Public Structure RandomizerInputs
Private pintSeed As Integer
Private pintUpperbound As Integer
Private pintLowerbound As Integer
Public Sub New(ByVal Seed As Integer, ByVal Upperbound As Integer,
ByVal Lowerbound As Integer)
pintSeed = Seed
pintUpperbound = Upperbound
pintLowerbound = Lowerbound
End Sub

Public ReadOnly Property Seed()
Get
Return pintSeed
End Get
End Property

Public ReadOnly Property Upperbound() As Integer
Get
Return pintUpperbound
End Get
End Property

Public ReadOnly Property Lowerbound() As Integer
Get
Return pintLowerbound
End Get
End Property

Public Overrides Function ToString() As String
Return String.Format("Seed: {0}, Upperbound: {1}, Lowerbound: {0}",
Seed, Upperbound, Lowerbound)
End Function

End Structure

Private pobjInputs As RandomizerInputs

Public Sub New()
MyBase.New()
End Sub

Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub

Public Sub New(ByVal message As String, ByVal innerException As
Exception)
MyBase.New(message, InnerException)
End Sub

Public Sub New(ByVal Inputs As RandomizerInputs)
MyBase.New()
pobjInputs = Inputs
End Sub

Public Sub New(ByVal message As String, ByVal Inputs As
RandomizerInputs)
MyBase.New(message)
pobjInputs = Inputs
End Sub

Public Sub New(ByVal message As String, ByVal innerException As
Exception, ByVal Inputs As RandomizerInputs)
MyBase.New(message, innerException)
pobjInputs = Inputs
End Sub

Public ReadOnly Property Inputs() As RandomizerInputs
Get
Return pobjInputs
End Get
End Property

Public Overrides Function ToString() As String
Return MyBase.ToString & vbCrLf & "Inputs:" & vbCrLf & Inputs.ToString
End Function

End Class

End Class
 
Back
Top