【问题标题】:Generating random numbers went wrong生成随机数出错了
【发布时间】:2016-09-04 18:18:24
【问题描述】:

我有这个代码。我正在生成 2 个带有随机数的数组,然后使用 arrayToString 函数从这些数组中创建 2 个字符串,但我的输出很奇怪。

class job1Instance : public pp::Instance {
public:
        explicit job1Instance(PP_Instance instance): pp::Instance(instance) {}
        virtual ~job1Instance() {}

    virtual void HandleMessage(const pp::Var& message) {
        // declare all the zises
        int32_t minNum = 1;
        int32_t maxNum = 100;
        int32_t arrayElements = maxNum;

        // the arrays
        int32_t unsorted1[arrayElements/2];
        int32_t unsorted2[arrayElements/2];

        // fill the arrays with random numbers
        unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum);
        std::string outRes1, outRes2, jsonStr;
        arrayToString(unsorted1, arrayElements/2, outRes1);
        arrayToString(unsorted2, arrayElements/2, outRes2);
        PostMessage(pp::Var(outRes2));
    }



private:
    // function to create a random number between min and max
    int32_t rangeRandomAlg (int32_t min, int32_t max) {
        int32_t num = max - min + 1;
        int32_t remainder = RAND_MAX % num;
        int32_t x;
        do {
            x = rand();
        } while (x >= RAND_MAX - remainder);
        return min + x % num;
    }

    // function to create arrays with random numbers
    void unsortedArrays (int32_t unsorted1[], int32_t unsorted2[], int32_t arrayElements, int32_t &minNum, int32_t &maxNum) {
        for(int32_t i = 0; i < arrayElements; i++) {
            if (i < arrayElements/2) {
                //unsorted1[i] = rangeRandomAlg(minNum, maxNum);
                unsorted1[i] = rand() % maxNum + minNum;
            } else {
                //unsorted2[i] = rangeRandomAlg(minNum, maxNum);
                unsorted2[i] = rand() % maxNum + minNum;
            }
        }
    }


    // convert the arrays to string
    void arrayToString (int32_t array[], int32_t arraySize, std::string& arrayString) {
        for (int i = 0; i <= arraySize; ++i){
            arrayString+= std::to_string(array[i]);
            if (i != arraySize) {
                arrayString+= ',';
            }
        }
    }

谁能告诉我为什么我的outRes2 输出有这些数字?

-18700984,-18701112,8,0,2,0,-66124,0,-66124,0,267757568,0,-65608,0,1,0,-65608,0,266960448,0,- 66124,0,1,0,-18699984,0,-66124,0,-18699984,0,266959840,0,7,-66124,-18699984,0,-66124,0,-68200,0,-18699984, 0,266959360,0,1,0,536870911,0,-18700016,0,91

它们显然不在我的 minNummaxNum 定义的 1 到 100 之间,我找不到问题。

【问题讨论】:

  • 您在调试和单步调试代码时看到了什么?
  • 当您尝试将其缩减为 minimal complete example 时会发生什么?

标签: c++ google-nativeclient


【解决方案1】:

您声明了两个数组,每个数组的大小为arrayElements/2

    int32_t unsorted1[arrayElements/2];
    int32_t unsorted2[arrayElements/2];

您的循环将它们初始化如下:

        if (i < arrayElements/2) {
            //unsorted1[i] = rangeRandomAlg(minNum, maxNum);
            unsorted1[i] = rand() % maxNum + minNum;
        } else {
            //unsorted2[i] = rangeRandomAlg(minNum, maxNum);
            unsorted2[i] = rand() % maxNum + minNum;
        }

因此,例如,当i 的值达到arrayElements/2 时,if 语句的else 部分将执行:

            unsorted2[arrayElements/2] = rand() % maxNum + minNum;

由于unsorted2 的大小为arrayElements/2,因此该数组仅包含unsorted2[0]unsorted2[arrayElements/2-1] 的值,并且该赋值超出了数组的末尾,导致未定义的行为。

【讨论】:

  • 我不知道为什么我自己没有看到它:)。另一个问题,你知道为什么每次执行后生成的数字都保持不变吗?
  • 因为你初始化随机数种子失败,srand().
  • 这成功了time_t time;srand((unsigned) time(&amp;time)); 来初始化srand() 函数。
【解决方案2】:

请不要使用 rand(),使用现代版本... 它让一切变得容易多了!! 这是一个简单的示例代码:

#include <random>
#include <iostream>
#include <string>

int main(){

    std::random_device now;
    std::mt19937 engine(now());
    std::uniform_real_distribution<double> r(0, 100); //!

    std::string str = std::to_string( r(engine) );

    std::cout << str << std::endl;

    system("pause");
    return 0;
}

【讨论】:

  • 如果 OP 使用 std::uniform_real_distribution,而不是 rand(),那将没有任何区别。因为那不是错误所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
  • 2013-05-10
相关资源
最近更新 更多