Does anyone know how to truly select random element from a vector v?

I tried this:

int rnd;
rnd = v[ (int)floor( (double)rand()*( (double)v.size() / (double)RAND_MAX ) ) ];
cout<< "Random element: "<<rnd;


But every time it's selecting only one particular element from my vector. It should be random right. I am not getting around this. You can suggest alternate code to display random element from a vector. But I just need single line function call to make efficient program. Don't want 3 or 4 line code for this.

Thanks,

did you seed the random-generator with srand() ?

Something like:

srand(time(NULL));
cout << rand()%v.size();

No. But my prof used the same stuff like
" srand(time(NULL)); "

What is this used for?
What is your first line of command? and what does second line do?

But you did exactly what my prof did. I donno though. Plz explain.

thanks,

No. But my prof used the same stuff like
" srand(time(NULL)); "

What is this used for?

As I said, to seed the randomgenerator

If you have time, you should read this

Thanks dude. you know lot of stuff. I ll go thru your article. Thnx again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.