生成随机数是错误的

时间:2016-09-04 18:18:25

标签: c++ google-nativeclient

我有这个代码。我正在使用随机数生成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之间,我找不到问题。

2 个答案:

答案 0 :(得分:2)

您声明了两个大小为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时,else语句的if部分将会执行:

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

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

答案 1 :(得分:-1)

请不要使用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;
}