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

Problems with Random Numbers

P: n/a
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

Aug 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

You need to seed your random numbers.

Dim RandomClass As New Random(now.ticks)

http://msdn2.microsoft.com/en-us/library/ctssatww.aspx

Ken
-----------------------

"Ricardo" wrote:
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

Aug 13 '06 #2

P: n/a
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
Aug 13 '06 #3

P: n/a
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

"iwdu15" <jmmgoalsteratyahoodotcomschreef in bericht
news:85**********************************@microsof t.com...
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

Aug 14 '06 #4

P: n/a
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
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uU**************@TK2MSFTNGP02.phx.gbl...
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

"iwdu15" <jmmgoalsteratyahoodotcomschreef in bericht
news:85**********************************@microsof t.com...
>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


Aug 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.