Here's a C++11 version:
#include <random>
#include <algorithm>
#include <iostream>
#include <cstddef>
#include <vector>
// helper to get the size (extent) of a raw array
template<class T, std::size_t N>
constexpr std::size_t get_extent(T&)
{ return std::extent<T>::value; }
int main()
{
std::string const chars[] = {"A","B","C"};
std::string const digits[] = {"1","2","3","4"};
// vector containing the resulting strings
std::vector<std::string> result;
// create the "ordered" sequence A1, A2, A3, A4, B1, ..
for(auto const& eChar : chars)
for(auto const& eDigit : digits)
result.push_back( eChar+eDigit );
// shuffle the sequence
{
// random device to seed the PRNG
std::random_device rd;
// create a MT19937 PRNG, seeded
std::mt19937 g(rd());
std::shuffle(begin(result), end(result), g);
}
// output result
for(auto const& e : result) std::cout << e << ", ";
std::cout << "\n";
}
A C++03 version:
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstddef>
#include <vector>
// helper to get the size (extent) of a raw array
template<class T, std::size_t N>
std::size_t get_extent(T (&)[N])
{ return N; }
int main()
{
std::string const chars[] = {"A","B","C"};
std::string const digits[] = {"1","2","3","4"};
// vector containing the resulting strings
typedef std::vector<std::string> result_t;
result_t result;
// create the "ordered" sequence A1, A2, A3, A4, B1, ..
for(std::string const* pChar = chars;
pChar != chars + get_extent(chars);
++pChar)
for(std::string const* pDigit = digits;
pDigit != digits + get_extent(digits);
++pDigit)
result.push_back( *pChar+*pDigit );
// shuffle the sequence
{
std::srand( std::time(NULL) );
std::random_shuffle(result.begin(), result.end());
}
// output result
std::copy(result.begin(), result.end(),
std::ostream_iterator<std::string>(std::cout, ", "));
std::cout << "\n";
}
rand()results using the modulus function to your specific ranges.for(int j=0; j=10; j++)Should befor(int j=0; j==10; j++). The first one is assigning and not comparing.A1, A2, ...and thenstd::random_shuffleit. Be aware thatrandom_shuffleusesrandby default, which is a) not very good (use C++11's PRNGs if possible) and b) requires seeding bysrand.