Browse - Programming Tips - How do I randomize the order (shuffle) an MFC CStringArray ?

Date: 2009oct11
Language: C/C++
Framework: MFC

Q.  How do I randomize the order (shuffle) an MFC CStringArray ?

A.  Here's a function that does it.

// Algorithm from Dr Dobbs, January 2000, page 113 void ShuffleCStringArray(CStringArray &a) { CString str; int dest, src, n; srand((unsigned)time(NULL)); n = a.GetSize(); for (dest = n - 1; dest > 0; dest--) { src = rand() % (dest+1); // swap str = a[src]; a[src] = a[dest]; a[dest] = str; } }