
July 29th, 2004, 07:56 AM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I use this to randomize all arrays and other lists
///<summary>
/// Randomizes the order of thr elements in any object that implements the IList interface.
/// If the object doesn't implement IList it is returned unchanged.
///</summary>
///<param name="org">Any object</param>
///<returns>object with elements in randomized order</returns>
publicstaticobject Shuffle (object org)
{ if (org is IList)
{Random rnd = new Random();
IList arr = (IList)org;
int newPos;
object tempObj;
int index = arr.Count;
while (--index >= 0)
{// new position for element at index
newPos = rnd.Next(arr.Count);
// swap the elements at newPos and index
tempObj = arr[index];
arr[index] = arr[newPos];
arr[newPos] = tempObj;
}
return arr;
}
return org;
}
Quote: | Originally Posted by pcyopick Ive made an online quiz in asp.net(vb), with the questions in an external xml document.
What the client wants is to randomize the questions. I currently loop through all the questions once the xml is parsed, so I thought making a randomized array of number of questions and going through the xml that way.
How would one make this randomized array, with each number appearing only once? Or is there any better ways of approaching this??
Thanks;
Paul Cyopick
cyopick@yahoo.com |
|