Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法获取随机字符串 我使用C++来创建一个随机问题的应用程序。但我认为这不管用(因为我的逻辑不好)。我尝试的是: class English { public: string get_questions (int number) { if (number == 1) { // Chapter 1 string questions[10] = { "In what way is man considere to be a lower species when compared to animals, in general?", "What specific triats of character make man the lowest animal in Mark Twain's views?", "What aspects of human nature are pointed when man is compared with the anaconda, bees, roosters, cats.", "What specific traits of character make man the lowest animal in Mark Twain's views?", "Discuss the Importance of the experiments conducted by the Mark Twain.", "Can people improve themselves and remove this label in thismillennium?", "What are the traits due to which man cannot claim to have reached the meanest of the Higher Animals?", "\"The damned Human Race\" was written in 1900, is it valid today?", "Do you think Mark Twain was accurate while comparing Human nature to that of the birds, insects and other animals?", "Why did Mark Twain rejected Darwin's theory, what were his conclusions in this regard?" }; string result = questions[rand() % 9 + 0] + "\n"; return result; } } };_C++_Arrays_Random - Fatal编程技术网

无法获取随机字符串 我使用C++来创建一个随机问题的应用程序。但我认为这不管用(因为我的逻辑不好)。我尝试的是: class English { public: string get_questions (int number) { if (number == 1) { // Chapter 1 string questions[10] = { "In what way is man considere to be a lower species when compared to animals, in general?", "What specific triats of character make man the lowest animal in Mark Twain's views?", "What aspects of human nature are pointed when man is compared with the anaconda, bees, roosters, cats.", "What specific traits of character make man the lowest animal in Mark Twain's views?", "Discuss the Importance of the experiments conducted by the Mark Twain.", "Can people improve themselves and remove this label in thismillennium?", "What are the traits due to which man cannot claim to have reached the meanest of the Higher Animals?", "\"The damned Human Race\" was written in 1900, is it valid today?", "Do you think Mark Twain was accurate while comparing Human nature to that of the birds, insects and other animals?", "Why did Mark Twain rejected Darwin's theory, what were his conclusions in this regard?" }; string result = questions[rand() % 9 + 0] + "\n"; return result; } } };

无法获取随机字符串 我使用C++来创建一个随机问题的应用程序。但我认为这不管用(因为我的逻辑不好)。我尝试的是: class English { public: string get_questions (int number) { if (number == 1) { // Chapter 1 string questions[10] = { "In what way is man considere to be a lower species when compared to animals, in general?", "What specific triats of character make man the lowest animal in Mark Twain's views?", "What aspects of human nature are pointed when man is compared with the anaconda, bees, roosters, cats.", "What specific traits of character make man the lowest animal in Mark Twain's views?", "Discuss the Importance of the experiments conducted by the Mark Twain.", "Can people improve themselves and remove this label in thismillennium?", "What are the traits due to which man cannot claim to have reached the meanest of the Higher Animals?", "\"The damned Human Race\" was written in 1900, is it valid today?", "Do you think Mark Twain was accurate while comparing Human nature to that of the birds, insects and other animals?", "Why did Mark Twain rejected Darwin's theory, what were his conclusions in this regard?" }; string result = questions[rand() % 9 + 0] + "\n"; return result; } } };,c++,arrays,random,C++,Arrays,Random,我使用的代码是这样的: cout << English().get_questions(chapter); cout您应该使用sr初始化随机数生成器,并使用随机种子值来更改rand()函数的此行为。 您可以使用类似于srand(time(NULL))使用不同的种子初始化随机生成器 请查看您应该使用sr初始化随机数生成器,并使用随机种子值来更改rand()函数的此行为。 您可以使用类似于srand(time(NULL))使用不同的种子初始化随机生成器 请看一看您没有设定随机数生成器的

我使用的代码是这样的:

cout << English().get_questions(chapter);

cout您应该使用
sr初始化随机数生成器,并使用随机种子值来更改
rand()
函数的此行为。 您可以使用类似于
srand(time(NULL))
使用不同的种子初始化随机生成器


请查看您应该使用
sr初始化随机数生成器,并使用随机种子值来更改
rand()
函数的此行为。 您可以使用类似于
srand(time(NULL))
使用不同的种子初始化随机生成器


请看一看

您没有设定随机数生成器的种子,因此每次运行该程序时,您都会得到相同的随机数序列。在程序开始时使用一次。

您没有设定随机数生成器的种子,因此每次运行程序时都会得到相同的随机数序列。在程序开始时使用一次。

您需要使用头
中声明的函数
std::srand
来设置随机序列

比如说

class English {
public: 
    English() { if ( !init ) std::srand( unsigned( std::time( 0 ) ) ); init = true; }

    string get_questions (int number) const {
        if (number == 1) {
            // Chapter 1
            string questions[10] = { /*...*/ };
            string result = questions[rand() % 10] + "\n";
            return result;
        }
    }
private:
    static bool init;
};

bool English::init = false;

考虑到我在函数
get_questions

中进行了更改,您需要使用在标题
中声明的函数
std::srand
来设置随机序列

比如说

class English {
public: 
    English() { if ( !init ) std::srand( unsigned( std::time( 0 ) ) ); init = true; }

    string get_questions (int number) const {
        if (number == 1) {
            // Chapter 1
            string questions[10] = { /*...*/ };
            string result = questions[rand() % 10] + "\n";
            return result;
        }
    }
private:
    static bool init;
};

bool English::init = false;

考虑到我在函数
get_questions

中所做的更改,如果您使用的是符合C++11标准的编译器,更好的解决方案是使用该库:

//初始化字符串数组
std::默认随机引擎生成器;
标准:均匀分布(0,9);
int指数=分布(发电机);
返回问题[索引];

如果您使用的是兼容C++11的编译器,更好的解决方案是使用该库:

//初始化字符串数组
std::默认随机引擎生成器;
标准:均匀分布(0,9);
int指数=分布(发电机);
返回问题[索引];

他应该如何获得“随机种子值”——使用rand()?:)@费迪南德拜尔:我已经用细节和链接更新了我的答案,非常感谢兄弟!:)我从来都不知道这件事,它现在起作用了!:)@AfzaalAhmadZeeshan欢迎兄弟:)他应该如何获得“随机种子值”——使用rand()?:)@费迪南德拜尔:我已经用细节和链接更新了我的答案,非常感谢兄弟!:)我从来都不知道这件事,它现在起作用了!:)@AfzaalAhmadZeeshan Welcome brother:)那么,如果我在程序开始时使用
rand()
,或者我创建了另一个方法,那会起作用吗?那么,如果我在程序开始时使用
rand()
,或者我创建了另一个方法,那会起作用吗?一些评论:C随机数功能非常差:“标准”种子是一个时间值(
time(NULL)
)的精度仅为1秒,并且返回的类型与
srand()
expect的类型不同(因此它会丢失精度和可变性)。之后,
rand()
不能保证产生均匀分布(不),但即使在这种情况下,你也可以使用模运算来打破这种一致性。所以(我知道情况并非如此,你所揭示的似乎是一个示例练习)如果你的随机数(PRNG)的数量是一个关注点,考虑不使用C库,搜索一个质量好的PRNG C++库。另外,注意到C++ 11标准库附带了一个比C等价的FAAAAAAAAAAAAAAR。一些注释:C随机数设施非常差:“标准”种子是一个时间值(<代码>时间(null)< /代码>)它只具有1秒的精度,并且不会返回与
srand()
expect相同的类型(因此它会丢失精度和可变性)。稍后,
rand()
不能保证产生均匀分布(不会),但即使在这种情况下,也会使用模运算符破坏均匀性。因此(我知道情况并非如此,你所披露的似乎是一个示例练习)如果你的随机数(PRNG)的数量是一个关注点,考虑不使用C库,搜索一个质量好的PRNG C++库。此外,注意到,由于C++ 11,标准库附带的是一个比C等价的FAAAAAAAAAAAAAR。