How to use shuffle or random function

I need help on using the shuffle, random, or random_shuffle function. I looked at the page on cplusplus, but it just confuses me, a lot. This is kinda linked to my other problem http://www.cplusplus.com/forum/general/131733/ because i have not yet solved it. Some say to use the random_shuffle (http://www.cplusplus.com/reference/algorithm/random_shuffle/) so i try using it, but it confuses me too much.
especially this part
 
random_shuffle(people.begin(), people.end(), something);

Its saying that there's no member for begin and end, or something like that.
so then I do this
 
random_shuffle(people.begin(0), people.end(number));

and then its says expected primary expression before [&int]
number is the number of people there are, that the user inputs.
people[number] is a string array used for storing names of people. I need it to be randomized so it works with my code from my other topic. (http://www.cplusplus.com/forum/general/131733/)
or, just so that it works by itself, and I'll eventually find a way to incorporate it into my code. Please do not use std::, I always use
1
2
3
#include <iostream>
#include <math.h>
using namespace std; 

before every program. The std:: just confuses me.


Edit: To reply to Peter87's post, do I need any other #include with the random_shuffle function, other than iostream and math.h?
Last edited on
people is an array so it doesn't have have member functions like begin() and end(). Instead you will have to use the non-member functions with the same name.
 
random_shuffle(begin(people), end(people));

If you are using an older compiler (pre C++11) the above might not work. You could then do this instead
 
random_shuffle(people, people + n);
where n is the number of elements in people.

If you want to understand why it should be this way you should read up on iterators.
Last edited on
Topic archived. No new replies allowed.