Home All Groups Group Topic Archive Search About

Problems with Random Numbers

Author
13 Aug 2006 3:54 PM
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

Author
13 Aug 2006 4:39 PM
Ken Tucker [MVP]
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
-----------------------

Show quoteHide quote
"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
>
>
Author
13 Aug 2006 7:05 PM
iwdu15
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
Author
14 Aug 2006 6:25 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"iwdu15" <jmmgoalsteratyahoodotcom> schreef in bericht
news:85F2DF1B-DF04-4178-9417-7C003AF93F48@microsoft.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
Author
14 Aug 2006 3:26 PM
AMDRIT
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


Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:uUxS9o2vGHA.3364@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" <jmmgoalsteratyahoodotcom> schreef in bericht
> news:85F2DF1B-DF04-4178-9417-7C003AF93F48@microsoft.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
>
>