Dave's Brain

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; } }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.