558d6c91-f887-4653-98ea-ffe485ac68d5
prompter
How can I iterate through each item in an array in a random order using c++?
aa663ff7-ca9b-4026-8962-8eb577f262e2
558d6c91-f887-4653-98ea-ffe485ac68d5
assistant
I am assuming you only want to access each element once and don't want to modify the original array.

To do this you could start by making a new array of random indexes then using those indexes when accessing the array. This method has the advantage of not changing the original array. For example say you have an array of numbers **[6,8,13]** and you want to access this in random order. This array has a length 3 so you would create a new index array of this size with each possible index **[0,1,2]**.  Then you shuffle this array using the **std::random_shuffle** function to obtain an array like  **[1,2,0]**. Then when you iterate through your loop and access an element, you get the index from the index_array (which is in random order) to access your original array. This could look **my_array[index_array[i]]**.

Here is an example full program
```
#include <iostream> 
#include <algorithm>
#include <ctime>
#include <cstdlib>
                                                                                    
int main(){                                                 
        std::srand( unsigned (std::time(0)));//set seed 

        const int size_of_array = 3;//array length

        int my_array[size_of_array] = {6,8,13};//data

        int index_array[size_of_array];//index array
        int i;
        for(i = 0;i < size_of_array;i++) index_array[i] = i;//initialize indexes
        std::random_shuffle(index_array,index_array+size_of_array);//shuffle indexes

        for(i = 0;i < size_of_array;i++){//access in random order
                int random_index = index_array[i];
                std::cout << my_array[random_index] << std::endl;
        }

}
```
429b3558-3f1f-4679-978d-c4f805da3a3b
aa663ff7-ca9b-4026-8962-8eb577f262e2
prompter
What is the time complexity of this code? Can this be made faster?