Randomizing multidimensional vectors

Could someone show me an example of randomizing multidimensional vectors? I found a tutorial for one dimensional vectors but can't figure it out.

1
2
3
4
5
6
7
8
9
10
11
int a1[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   std::vector< int > v( a1, a1 + 10 ); // copy of a1
   std::ostream_iterator< int > output( std::cout, " " );

   std::cout << "Vector v before random_shuffle: ";
   std::copy( v.begin(), v.end(), output );

   std::random_shuffle( v.begin(), v.end() ); // shuffle elements of v
   std::cout << "\nVector v after random_shuffle: ";
   std::copy( v.begin(), v.end(), output );
   std::cout << std::endl;
That code is silly. You don't need to put things in a vector to random_shuffle them. Try replacing "v.begin()" with "a1" and "v.end()" with "a1+10".
On topic, if you just had "int a1[10][10]" it would be simple. For multidimensional vectors, not so simple. It can be done, but I have a feeling that you don't really need an MD array here at all.
So how would i do it with a1[10][10]?
You would random_shuffle( a1[0], a1[0]+100 );. There is a million other way to write the same thing. That this corks because a1[10][10], unlike a vector, is stored an contiguous memory, so it can be treated like a one dimensional array.
Note that if you only use a 2d array for the [][] syntax, you might have less problems with a 1d array.
Topic archived. No new replies allowed.