Random shuffle

First, the random number shuffling

// pArray to disrupt array

// uCount array length

// uItemSize size of each element in the array

void CGameConvenient::Shuffle(void *pArray,ushort uCount,ushort uItemSize)

{

  if(nCount<=1)

    return;

  // record subscript

  std :: vector <ushort> IndexArray; // original array

  IndexArray.resize(uCount);  

  for(ushort i=0;i<nCount;i++)

    IndexArray[i]=i;

  std :: vector <ushort> NewIndexArray; // new array

  NuwIndexArray.resize(nCount);

  // upset subscript

  srand((unsigned int)(time(0)));

  ushort uRandCount=0,uPosition=0;

   do

  {

    uPosition=rand()%(uCount-nRandCount);

    NuwIndexArray[uRandCount++]=IndexArray[uPosition];

    IndexArray[uPosition]=IndexArray[uCount-uRandCount];

  }while(uRandCount<uCount);

}

The idea of ​​this algorithm is to randomly generate a random number in a range of compliance, the original array subscript a random number of elements assigned to the new array, and then the last element of the original array covering the fall of random numbers marked place .

Guess you like

Origin www.cnblogs.com/socks/p/11766097.html