How would you shuffle an array? Like say we have an array-
How would I mix up the numbers so that I have it randomly mixed up-Code:int array[4] = { 1, 2, 3, 4, 5 }
Could I use something like XOR?Code:{ 2, 4, 5, 1, 3 }
Printable View
How would you shuffle an array? Like say we have an array-
How would I mix up the numbers so that I have it randomly mixed up-Code:int array[4] = { 1, 2, 3, 4, 5 }
Could I use something like XOR?Code:{ 2, 4, 5, 1, 3 }
>How would I mix up the numbers so that I have it randomly mixed up
There is a function called random_shuffle() in <algorithm>.
-PreludeCode:#include <iostream>
#include <algorithm>
int main()
{
int array[5] = { 1, 2, 3, 4, 5 };
std::random_shuffle ( array, array + 5 );
for ( int i = 0; i < 5; i++ )
std::cout<< array[i] <<std::endl;
}