Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Communications (http://www.xtremevbtalk.com/-net-communications/)
-   -   Seed, Class and shared Random (http://www.xtremevbtalk.com/-net-communications/327410-seed-class-shared-random.html)

naton 11-30-2014 08:32 AM

Seed, Class and shared Random
 
Hi there,

Let's say that a have a Car class and a FleetOfCars class. I want each car in the fleet to have a different engine power. For this I added to the class Car a member variable rnd of type Random.

In order to have the same behavior whenever I run my program (mainly for debugging reason), I did the following :

Code:

Class Car
  private power as Double
  private shared rnd as Random = new Random (1234)

  Public Sub New(Byval maxPower as Double)
      power = rnd * maxPower
  End Sub

  Public Function MyPower() as String
      Return power.ToString
  End Function
End Class

Class FleetOfCar
  private cars() as Car
  private maxPower as Double = 300
 
  Public Sub New(Byval numberOfCars as Integer)
      ReDim cars(numberOfCars - 1)

      For i as Integer = 0 to numberOfCars - 1
        cars(i) = New Car(maxPower)
      next
  End Sub

  Public Sub PrintCarsSpecs()
      Dim str as String

      For i as Integer = 0 to Cars.GetUpperBound(0)
        str = str + "Car" + i.ToString + " : power " + Cars(i).MyPower + VbNewLine
      Next

      MsgBox(str)
  End Sub
End Class

Public Sub Main()
  Dim MyFleetOfCars as FleetOfCars = New FleetOfCars(20)
  MyFleetOfCars.PrintCarsSpecs()
End Sub

Every time I run the sub Main() of the above code I will have the same sequence of power. Now and here is where I'm struggling, I want to automate the code so it runs several times without having to re-execute Main() each time. How can I get the program to print the same sequence of cars' power if I change the sub Main() into the one below:

Code:

Public Sub Main()
  For i = 0 to 10
      Dim MyFleetOfCars as FleetOfCars = New FleetOfCars(20)
      MyFleetOfCars.PrintCarsSpecs()
  Next
End Sub

Thanks.

snarfblam 11-30-2014 10:16 AM

You would want to add a constructor to FleetOfCars that accepts a random seed, which you can use to create a new Random object. If a new random seed is not specified, you would want to continue to use the shared Random object rather than creating a new one so you get a unique random value.

Your example would then be modified like so:
Code:

Public Sub Main()
  For i = 0 to 10
      Dim MyFleetOfCars as FleetOfCars = New FleetOfCars(20, 1234)
      MyFleetOfCars.PrintCarsSpecs()
  Next
End Sub


naton 12-01-2014 02:47 AM

Thanks snarfblam.

But I'm trying to automate the reset of the seed too, and if possible hide it from Main() as well as FleetOfCars. The idea I came up with is below.

My Main() had an error which has been corrected below. Questions:

1- Is there a more elegant way of automating the reset of the seed than the one below?

2- In Main(), Why is MyFleetOfCars not destroyed and removed by the garbage collection after each loop?
Before testing the code I was under the impression that the process will start from the beginning every time a FleetOfCars instance is created and thus this line would be called (private shared rnd as Random = new Random (1234)) every time a new instance of FleetOfCars is created.

Code:

Class Car
  private power as Double
  private shared rnd as Random = new Random (1234)

  Public Sub New(Byval maxPower as Double)
      power = rnd * maxPower
  End Sub

  Public Function MyPower() as String
      Return power.ToString
  End Function

  Public Shared Sub ResetSeed()
      rnd = New Random(1234)
  End Sub

End Class

Class FleetOfCar
  private cars() as Car
  private maxPower as Double = 300
 
  Public Sub New(Byval numberOfCars as Integer)
      ReDim cars(numberOfCars - 1)

      Car.ResetSeed()

      For i as Integer = 0 to numberOfCars - 1
        cars(i) = New Car(maxPower)
      next
  End Sub

  Public Sub PrintCarsSpecs()
      Dim str as String

      For i as Integer = 0 to Cars.GetUpperBound(0)
        str = str + "Car" + i.ToString + " : power " + Cars(i).MyPower + VbNewLine
      Next

      MsgBox(str)
  End Sub
End Class

Public Sub Main()
  Dim MyFleetOfCars as FleetOfCars
  For i as Integer = 0 to 10
      FleetOfCars = New FleetOfCars(20)
      MyFleetOfCar.PrintCarsSpecs()
  Next
End Sub

Thanks.

PlausiblyDamp 12-01-2014 07:07 AM

Every time you go through the loop it will be creating a new instance of the FleetOfCars class, however because the variable is defined outside of the loop it will not be destroyed just because the loop is restarting.

naton 12-03-2014 02:41 AM

So shared variables are stored in the stack and not in the heap. Isn't it?

PlausiblyDamp 12-03-2014 03:57 AM

Being Shared doesn't affect where the variable is stored, is simply alters the lifetime of the variable.

If a class level variable is Shared then only a single copy exists in memory and this copy is shared between all instances of the class.

naton 12-09-2014 02:27 AM

snarfblam and PlausiblyDamp, thank you.
Best.


All times are GMT -6. The time now is 03:56 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.