Random vector index chosen from subset of indices

Jun 3, 2016 at 10:21pm
Hi, I'm new here and new to C++. My background is in math, not programming.

I have a vector of ints. I'd like to choose a (pseudo) random index out of the indices whose entry is an even integer. What's the easiest way to do this?

My first thought was to create two new vectors which essentially split the original vector into two parts: one vector containing the odd entries and one containing the even entries. This would allow me to choose an even integer at random using the new vector of evens, but I really need the index corresponding to that even integer in the original vector. I suppose I could keep track of indices when creating the even/odd vectors, but this seems cumbersome and not very elegant. Surely there is an easier approach.
Jun 3, 2016 at 11:00pm
Update: I solved my problem. I'll do exactly as I said above, but instead of defining the two new vectors by the even/odd entries of the original, I'll just use the index corresponding to the even/odd entries. Then I have one vector consisting of the vertices for the even entries, and another vector consisting of the vertices for the odd entries. For my particular program, this is exactly what I need.

Not sure why I didn't think of this slight modification before.
Topic archived. No new replies allowed.