• Post Reply Bookmark Topic Watch Topic
  • New Topic

best way to randomly select objects  RSS feed

 
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

lets say i have a horse class...
it has a constructor that has
energy
speed
luck

and i wanted to have 50 horses instantiated from this class,then have say 8 horses selected from the pool of horses
whats the best way to do this?

the horses stats will never change,so they remain the same.

would i create all the horses and then assign them to an array? or an enum?

or should the objects be instantiated on the fly?

i understand how random selection works and have written small programs regarding this...just wondered on the overall project what the best way to do this was.

any guidance appreciated to push me in the right direction

 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:the horses stats will never change,so they remain the same.
You mean all horses will have the same energy, speed and luck? Or you mean once horse object is constructed, the state will remain the same during objects lifecycle?

jon ninpoja wrote:would i create all the horses and then assign them to an array?
Or list. But yes, in some kind of data structure where you could hold them.

jon ninpoja wrote:or an enum?
Don't see a need of it here, at least at the moment.

jon ninpoja wrote:...should the objects be instantiated on the fly?
That is preferred way, unless instructions require specific horses stats which you couldn't replicate during iteration.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again,

jon ninpoja wrote:
the horses stats will never change,so they remain the same.
You mean all horses will have the same energy, speed and luck? Or you mean once horse object is constructed, the state will remain the same during objects lifecycle?
yes once created horse a and b will have different stats but they will never change (maybe down the road,if i do more with this...but not for now)

jon ninpoja wrote:
would i create all the horses and then assign them to an array?
Or list. But yes, in some kind of data structure where you could hold them.
so all the horses will have to be instantiated and then placed in a list,then have a method select say 8 horses from the list depending on the race.

jon ninpoja wrote:
or an enum?
Don't see a need of it here, at least at the moment.
when would you use an enum vs a list? ...do they do a similar function?

jon ninpoja wrote:
...should the objects be instantiated on the fly?
That is preferred way, unless instructions require specific horses stats which you couldn't replicate during iteration.
so it sounds like the latter? right?

thanks for relying,it helps a lot!!!
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:when would you use an enum vs a list? ...do they do a similar function?

Not really.  A List is a container.  It can contain anything.  You create an empty list and add to it (in your case, horses).

An enum is a set of constant values, for instance, the days of the week.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . .
A List is a container.  It can contain anything
. . . .
An enum is a set of constant values . . . .
But both can be used to select one object at “random”. It is really quite simple. But don't use Math#random. Use a Random object instead. Remember that every enum implicitly implements a values() method which allows you to get all the constants in that enum as an array. For Lists try the get() method but use ArrayList for that not LinkedList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!