随机解析垃圾值

时间:2013-02-08 04:15:59

标签: arrays random multidimensional-array

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

提前谢谢大家! :)

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的范围内。 谁知道为什么值的输出往往是如上所述?

0 个答案:

没有答案