0

我正在生成随机值并解析为数组。知道哪个部分导致了问题吗?

谢谢大家!:)

for(x=0; x<numberOfSboxes; x++)
{
    vecCheck.clear();
    for (y=0;y<sbox_Size; y++)
    {       
        int secondTemp = y;
        int firstTemp;
        firstTemp = rand() % numberRange;

        int mycount = (int) std::count (vecCheck.begin(), vecCheck.end(), y); // count if number is in vector
        if( (mycount==1) || (firstTemp==y)  )
        {
            continue;
        }
        else
        {
            if(vecCheck.size()==0)
            {
                vecCheck.push_back(firstTemp); // first number
                sBox[x][y] = firstTemp;
                sBox[x][firstTemp] = secondTemp;
                vecCheck.push_back(secondTemp); //second number 
            }
            else
            {                   
                int mycount = (int) std::count (vecCheck.begin(), vecCheck.end(), firstTemp); // count if number is in vector
                if(mycount==1)
                {
                    //if number generated is found, then break loop and restart
                    --y;
                    continue;
                }
                else
                {
                    //if number generated is not found
                    sBox[x][y] = firstTemp; // first number generated
                    sBox[x][firstTemp] = secondTemp;
                    vecCheck.push_back(firstTemp); //push back to record
                    vecCheck.push_back(secondTemp); //push back to record
                }
            }
        }
    }
}

它并不总是生成垃圾值,但有时会。下面将显示一个示例。

  sBox[0][16] = {9,12,15,5,7,3,12765952,4,13,0,11,10,1,8,12688216,2};
  sBox[1][16] = {6,11,3,2,8,10,0,15,4,134514593,5,1,14,-1075,0,78827,12,7};
  sBox[2][16] = {3,4,7,0,1,13,11,2,10,14,8,6,15,5,9,12};

我认为它在 rand() 上没有问题。

编辑:

firstTemp 一代没有问题。数字始终在 0-15 的范围内。任何人都知道为什么这些值的输出往往是上述的?

4

0 回答 0