I need a method which takes an array of element than returns it with elements which are in randomized order.
For that purpose I developed following method
string[] shuffle(string[] a)
{
for (int i = 0; i < a.Length; i++)
{
string tmp = a[i];
int randomnumber = rnd.Next(i, a.Length);
a[i] = a[randomnumber];
a[randomnumber] = tmp;
}
return a;
}
but it returns sometimes the duplicate elements, which is wrong.
I need help.

VulpesPosted Oct 29, 2012, 7:12 AM
I ran it 10 million times just to make sure and no duplicates were found.
Are you sure that you haven't changed the code since you found the duplicate element which has now corrected it?
Turkalp KucurPosted Oct 29, 2012, 11:32 AM
The problem is, I call the function inside of the loop, that results to re-evaluate the method, resulting the duplicates or triplicates.
When I take back it from the loop, the problem is solved.
Thank you!.