随机数字列表的生成器在运行时并不总是有效

时间:2017-12-01 19:53:58

标签: random generator srand

我写了一段代码,生成从0到9的MAX_SIZE数字,并将它们存储在一个名为 random 的向量中,这样相同的数字就不会出现两次,最大值为10数字。 代码似乎工作,但很少(或重复打开它),它似乎停止工作和锁定,直到有人关闭它。 我想这与随机数生成有关,但我不确定,我还在学习。 这是完整的代码:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

vector<int> random;
constexpr int MAX_SIZE = 10;    //can be up to 10, try and change it over the limit

int generate() {    //generates a casual number between 0 and 9
    int a = 0;
    a = rand() % 10;
    return a;
}

int rand_elem(int a) {  //makes sure the arg "int a" is not already contained in the vector<int> random
    for (unsigned o = 0; o<MAX_SIZE; ++o)
        if (a==random[o]) {
            return 1;
            break;  //returns 1 and interrupts the check if there is a match (aka if there is already that number in the vetcor<int> random
        }
    return 0;   //else returns 0 aka success
}

void random_fill() {    //"fills" vector<int> random with different random numbers from 0 to 9
    for (unsigned i = 0; i<MAX_SIZE; ) {
        int num = generate();
        if (i==0 || rand_elem(num)==0) {    //checks if the newly generated number with generate() doesn't already belong to vector<int> random
            random.push_back(num);
            ++i;    //if int num doesn't belong to vector<int> random, it is being added to it and the loop can proceed (++i) until the condition i<4 is met
        }
    }
}

int main()
try {
    if (MAX_SIZE > 10) {
        throw (runtime_error("MAX_SIZE too big\n"));
    }
    srand(time(NULL));  //generates a seed for the random number generator generate() using system time
    random_fill();
    for (unsigned h = 0; h<random.size(); ++h)  //prints the content of the vector
        cout<< random[h] << '\t';
} catch (runtime_error& e) {    //error catching 
    cout <<"runtime error: "<< e.what()<<'\n';
    return 1;
}

希望这似乎不是一个多余的问题,但我还没有通过在网站上搜索找到任何答案。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

我建议您重新组织代码,如下例所示。这个循环你有一个普遍的问题:

for (unsigned o = 0; o<MAX_SIZE; ++o)

矢量的大小不是MAX_SIZE。事实上,大小将随着你将放在向量中的每个数字而改变。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>

using namespace std;

vector<int> numbers;

/**
* Can be up to 10, try and change it over the limit.
*/
const int MAX_SIZE = 10;

/**
* Generates a casual number between 0 and 9.
*/
int generate() {
    return rand() % 10;
}

/**
* Makes sure the arg "int a" is not already contained in the vector<int> random.
*/
bool rand_elem(int a) {
    /*
    * When vector is empty there is no way to has duplications.
    */
    if(numbers.size() == 0) {
        return false;
    }

    /*
    * Check previous elements for duplications.
    */
    for (int o = 0; o<numbers.size(); ++o) {
        if (a == numbers[o]) {
            /*
            * Returns 1 and interrupts the check if there is a match (aka if there is already that number in the vetcor<int> random.
            */
            return true;
        }
    }

    /*
    * Else returns 0 aka success.
    */
    return false;
}

/**
* Fills vector<int> random with different random numbers from 0 to 9.
*/
void random_fill() {
    int num;
    for (unsigned i = 0; i<MAX_SIZE; i++) {
        do {
            num = generate();

            /*
            * Checks if the newly generated number with generate() doesn't already belong to vector<int> random.
            */
        } while(rand_elem(num) == true);

        numbers.push_back(num);
    }
}

/**
* Program single entry point.
*/
int main() {
    /*
    * Generates a seed for the random number generator generate() using system time.
    */
    srand(time(NULL));

    try {
        if (MAX_SIZE > 10) {
            throw (runtime_error("MAX_SIZE too big\n"));
        }

        random_fill();

        /*
        * Prints the content of the vector.
        */
        for (int h = 0; h<numbers.size(); ++h)  {
            cout<< numbers[h] << '\t';
        }

        /*
        * Error catching.
        */
    } catch (runtime_error& e) {
        cout <<"runtime error: "<< e.what()<<'\n';
        return 1;
    }

    return EXIT_SUCCESS;
}