Sunday 29 September 2013

How do I choose random objects of the same type from a vector array of differing types?

How do I choose random objects of the same type from a vector array of
differing types?

Let's say for example a vector is composed of some objects, one of type
rectangle, some of type triangle, and then circles.
v = [rectangle, triangle, triangle, circle, circle]
The vector's size can change. I can add another circle as so:
v.addElement(circle);
and..
v = [rectangle, triangle, triangle, circle, circle, circle]
but each object type is clustered together like above. It can't be like:
v = [rectangle, circle, circle, triangle, circle, triangle] //<-- can't be.
I know I explained it pretty horrible but hopefully, it's enough to
understand my scenario. Now, I want to randomly choose, for example, an
object of type circle.
My thought process is to make a separate method that 1, finds the
beginning index and 2, find the ending index and then use random functions
off of that. Is there a more elegant way to solve this problem of only
choosing randomly off of circles?

No comments:

Post a Comment