Just look at the codes
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<std::string> v = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
std::vector<std::string>::iterator it;
for (it = v.begin(); it != v.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
std::random_shuffle(v.begin(), v.end());
for (it = v.begin(); it != v.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
std::vector<int> v2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int>::iterator it2;
for (it2 = v2.begin(); it2 != v2.end(); ++it2)
{
std::cout << *it2 << " ";
}
std::cout << std::endl;
std::random_shuffle(v2.begin(), v2.end());
for (it2 = v2.begin(); it2 != v2.end(); ++it2)
{
std::cout << *it2 << " ";
}
std::cout << std::endl;
return 0;
}
Corresponding results
a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2
The above codes cannot achieve the results will change with every iteration of running
The codes to achieve that you can get different results after you run your codes
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
int main()
{
std::vector<std::string> v = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
std::vector<std::string>::iterator it;
for (it = v.begin(); it != v.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
std::random_shuffle(v.begin(), v.end());
for (it = v.begin(); it != v.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
std::vector<int> v2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int>::iterator it2;
for (it2 = v2.begin(); it2 != v2.end(); ++it2)
{
std::cout << *it2 << " ";
}
std::cout << std::endl;
std::random_shuffle(v2.begin(), v2.end());
for (it2 = v2.begin(); it2 != v2.end(); ++it2)
{
std::cout << *it2 << " ";
}
std::cout << std::endl;
// make the result of the random_shuffle unpredictable
srand(time(NULL));
std::vector<int> v3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int>::iterator it3;
for (it3 = v3.begin(); it3 != v3.end(); ++it3)
{
std::cout << *it3 << " ";
}
std::cout << std::endl;
std::random_shuffle(v3.begin(), v3.end());
for (it3 = v3.begin(); it3 != v3.end(); ++it3)
{
std::cout << *it3 << " ";
}
std::cout << std::endl;
return 0;
}
The corresponding results:
first_try
a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2
1 2 3 4 5 6 7 8 9 10
3 2 4 8 6 9 1 5 10 7
second_try
a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2
1 2 3 4 5 6 7 8 9 10
8 10 9 4 5 6 1 3 7 2
third_try
a b c d e f g h i j k l m n o p q r s t u v w x y z
e k l p y q r b g j d w t c a x z s n i f v h m o u
1 2 3 4 5 6 7 8 9 10
9 6 8 4 3 7 1 5 10 2
1 2 3 4 5 6 7 8 9 10
7 3 1 10 4 6 9 8 5 2