Jun 22, 2009

There is an array of 52 numbers ? How do you shuffle this array ?

There is an array of 52 numbers ? How do you shuffle this array ?
To this question, how many permutations we have.
Suppose we have 52 numbers, so permutation number is 52!
The permutation for this code is 52*51*…*1=52!

foo(int* arr, int len)//len=52
{
for(int i=0;i < len;i++)
{
int idx=rand()%(len-i)+i;
swap(arr[i],arr[idx]);
}

No comments:

Post a Comment