Random selection from vector elements

Hi,

I have a vector SP but the number of elements (size of the vector is not constant) are changing.

I want to select one of the elements randomly.

For Example;

1
2
3
4
5
6
vector<int> SP;
int selection;
SP[0]=2;
SP[1]=4;
SP[2]=8;
selection=2 or 4 or 8 (Randomly)
Last edited on
use rand() with remainder division (modolus %) of the size of the array. And access that element.

int randomIndex = rand() % SP.size();
Topic archived. No new replies allowed.