Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random
Seed, Class and shared Random Seed, Class and shared Random
Seed, Class and shared Random
Go Back  Xtreme Visual Basic Talk > > > Seed, Class and shared Random


Reply
 
Thread Tools Display Modes
  #1  
Old 11-30-2014, 08:32 AM
naton naton is offline
Freshman
 
Join Date: Aug 2004
Posts: 30
Default 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.

Last edited by naton; 12-01-2014 at 02:29 AM.
Reply With Quote
  #2  
Old 11-30-2014, 10:16 AM
snarfblam's Avatar
snarfblamSeed, Class and shared Random snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

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
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 12-01-2014, 02:47 AM
naton naton is offline
Freshman
 
Join Date: Aug 2004
Posts: 30
Default

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.
Reply With Quote
  #4  
Old 12-01-2014, 07:07 AM
PlausiblyDamp's Avatar
PlausiblyDampSeed, Class and shared Random PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,056
Default

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.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 12-03-2014, 02:41 AM
naton naton is offline
Freshman
 
Join Date: Aug 2004
Posts: 30
Default

So shared variables are stored in the stack and not in the heap. Isn't it?
Reply With Quote
  #6  
Old 12-03-2014, 03:57 AM
PlausiblyDamp's Avatar
PlausiblyDampSeed, Class and shared Random PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,056
Default

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.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 12-09-2014, 02:27 AM
naton naton is offline
Freshman
 
Join Date: Aug 2004
Posts: 30
Default

snarfblam and PlausiblyDamp, thank you.
Best.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random Seed, Class and shared Random
Seed, Class and shared Random
Seed, Class and shared Random
 
Seed, Class and shared Random
Seed, Class and shared Random
 
-->