Hi John,
recently did just that exact scenario...
the following function takes an array of objects and returns a shuffled
arraylist.
this ofcourse can be modified to recieve an arraylist instead of array, and
to return an array instead of an arraylist.
this might not be efficient or finished, but it works and I had it here
so...
p.s. - you can use the ArrayList.ToArray() for easy conversion back to array
of a givven type.
public static ArrayList Randomize(object[] source)
{
if (source==null)
return null;
if (source.Length<2)
return new ArrayList(source);
int length = source.Length;
int[] positions = new int[length];
object[] target = new object[length];
for (int i=0;i<positions.Length;i++)
positions[i] = -1;
Random rnd = new Random();
for (int i=0;i<positions.Length;i++)
{
int position = -1;
bool alreadyAssigned = false;
bool first = true;
while (first || alreadyAssigned)
{
first = alreadyAssigned = false;
position = rnd.Next(length);
foreach (int assigned in positions)
{
if (assigned==position)
{
alreadyAssigned = true;
break;
}
}
}
positions[i] = position;
}
for (int i=0;i<positions.Length;i++)
target[positions[i]] = source[i];
ArrayList retval = new ArrayList(target);
return retval;
}
"John Slate" <ph*******@yahoo.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Not sure exactly how to ask this, but I have a situation where I need to
'shuffle' the order of a list of test questions, so that the questions
appear in a random order each time the test is taken. I am not sure how
to go about this. The questions and multiple choice answers are stored
in SQL Server. Any suggestions?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!