zoukankan      html  css  js  c++  java
  • 随机洗牌

    一、随机数洗牌

    //pArray 要打乱的数组

    //uCount 数组长度

    //uItemSize 数组中每个元素的大小

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

    {

      if(uCount<=1)

        return;

      //记录下标

      std::vector<ushort> IndexArray;    //原数组

      IndexArray.resize(uCount);  

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

        IndexArray[i]=i;

      std::vector<ushort> NewIndexArray;  //新的数组

      NewIndexArray.resize(nCount);

      //打乱下标

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

      ushort uRandCount=0,uPosition=0;

       do

      {

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

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

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

      }while(uRandCount<uCount);

    }

    本算法的思想是先随机生成一个符合规定范围的随机数,将原数组中下标为随机数的元素,赋值到新的数组,然后将原数组最后一个元素覆盖掉下标为随机数的地方。

  • 相关阅读:
    访存模型
    Petri网
    Forward secrecy
    TensorFlow训练神经网络cost一直为0
    解决tensorflow在训练的时候权重是nan问题
    tf.argmax
    Keras教程
    z-score
    隐马尔可夫(HMM)、前/后向算法、Viterbi算法
    受限玻尔兹曼机基础教程
  • 原文地址:https://www.cnblogs.com/socks/p/11766097.html
Copyright © 2011-2022 走看看