CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Randomizing multidimensional vectors/arrays

1. Junior Member
Join Date
Aug 2011
Posts
5

## Randomizing multidimensional vectors/arrays

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

Code:
```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;```

2. Senior Member
Join Date
Oct 2008
Posts
1,456

## Re: Randomizing multidimensional vectors/arrays

the case of a multidimensional array is simple: given that C++ arrays are stored row-wise in a contiguos memory region, it's simply a matter of shuffling the array spanning its elements:

Code:
```int a[2][3][4] = { ... };

std::random_shuffle( &(a[0][0][0]), &(a[0][0][0]) + sizeof(a) / sizeof(int) );```
otherwise, it depends on what you mean by "multimensional vector": do you mean an std::vector<std::vector<T>>, a std::vector<T> wrapped in a class with a subscript operator, or storing pointers to rows, etc ... ?

3. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Randomizing multidimensional vectors/arrays

The definition of "randomize" is important here. What exactly are your requirements?

4. Junior Member
Join Date
Aug 2011
Posts
5

## Re: Randomizing multidimensional vectors/arrays

Well i'll give you an example:

I create a 4x4 vector

Code:
`std::vector< std::vector<int> > a2(4, std::vector<int>(4));`
Give each row the numbers 0123

Code:
```int iX = 0;
int iY = 0;
int iNumb = 0;

while (iY < 4)
{
a2[iX][iY] = iNumb;
++iX;
if (iX > 3)
{
iX = 0;
++iY;
++iNumb;
}

}```
So now I want to randomize them, so I could have like, 0000, 1122, 2211, 3333.

5. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Randomizing multidimensional vectors/arrays

The relevant question is, do you need to have 4 0s, 4 1s, 4 2s, and 4 3s at the end? Or do you not care how many of each you have as long as your matrix is filled with the numbers 0-3?

You won't be able to do it simply (as with random_shuffle) in the former case using a vector of vectors, because the memory isn't contiguous. But it's possible.

6. Junior Member
Join Date
Aug 2011
Posts
5

## Re: Randomizing multidimensional vectors/arrays

I want to randomize exactly the numbers i have stored, both their digit and their ammount. So yes at the end I want all 4 of each, I don't want to end up with like seven 0s and one 1.

7. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Randomizing multidimensional vectors/arrays

Okay, so it's random rearrangement rather than random selection.

There are two primary approaches you can use:

1) Define a2 in such a way that its memory is contiguous (it is not presently), then use random_shuffle on the entire range. A boost::multi_array is one easy way to do this, or you could use a vector<int> for the underlying data and a vector<int*> to represent the rows. There's lots of material on different ways to define 2D arrays around.

2) Keep a2 as-is, but shuffle a 1D array of 2D indexes into it. For instance,
Code:
```vector<pair<int,int>> inds;
inds.push_back(make_pair(0,0));
inds.push_back(make_pair(0,1));
...
inds.push_back(make_pair(3,2));
inds.push_back(make_pair(3,3));

random_shuffle(inds.begin(),inds.end());

for (unsigned i = 0; i < inds.size(); ++i)
{
unsigned row = i/4;
unsigned col = i&#37;4;
a2[row][col] = a[inds[i].first][inds[i].second];
}```
(not tested)

8. Junior Member
Join Date
Aug 2011
Posts
5

## Re: Randomizing multidimensional vectors/arrays

Ahh so there's no classes designed for randomizing vectors like with arrays, shame. Thanks for the help.

9. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## Re: Randomizing multidimensional vectors/arrays

Originally Posted by Bottledbread
Ahh so there's no classes designed for randomizing vectors like with arrays, shame. Thanks for the help.
That's not correct. random_shuffle() works just as well on a vector as it would on an array. The trouble is, that random_shuffle (and all other STL algorithms) works on a linearly ordered set of iterators, and there is no iterator in the standard library which knows how to traverse over all the T elements in a vector<vector<T>>. Iterating over the elements of a vector is trivial, but iterating over the elements of many vectors as if they're all a single range is not.

That's not to say that such an iterator couldn't be designed, of course. That would be possible. It would need to be a Random Access Iterator to meet the requirements of random_shuffle, of course, but that's doable too with a little more work, especially if you can restrict it to the case when all vectors it operates on are the same length.

My suggestions above were meant to be more intuitive to use than designing a whole new type of iterator, but if you really want to use random_shuffle directly on the object, that's how you do it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width