How do I get random swapping in arrays?

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!
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
Topic archived. No new replies allowed.