How do I get random swapping in arrays?

Sep 26, 2014 at 9:13pm
I'm trying to figure out how to randomly swap different elements in an array of structures, but I'm stuck. Wondering if anyone could help me out.

This is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void shuffle()
{
   Music temp;
   unsigned int currentTime = (unsigned)time(0);

   srand(currentTime);
   
   for (int i = 0; i < NUM_SONGS; i++)
   {
      songs[i] = temp;
	  i = (rand() % NUM_SONGS) + 1;
	  temp = songs[i];
	  
   }
}


Any help is appreciated!
Sep 26, 2014 at 9:17pm
http://en.cppreference.com/w/cpp/algorithm/random_shuffle
If you don't want to use library function, just look at the implementation example.

Other reference: http://www.cplusplus.com/reference/algorithm/random_shuffle/
Last edited on Sep 26, 2014 at 9:18pm
Topic archived. No new replies allowed.