gpt4 book ai didi

c++ - 如何使用 cpp 设置随机数生成器的语法?

转载 作者:行者123 更新时间:2023-11-27 23:38:52 24 4
gpt4 key购买 nike

首先让我说我对编码很陌生,但喜欢学习。我想随机生成一个数字,然后跟随 700 if 语句,这些语句将根据生成的数字提出问题。因此,从用户 pov 开始,它会随机提问,然后生成另一个数字。

我试过计时随机数生成器。我似乎无法让 rand 运算符工作。我只想要一个 super 基本的发电机。

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
string a = "placeholder";
cout << "Welcome to Basic Strategy Practice" << endl;
cout << "You are given two aces and the dealer is showing a four. Hit,
double, or split ? " << endl;
cin >> a;
if (a == "split") {
cout << "Correct!" << endl;
}
else {
cout << "Incorrect" << endl;
}

return 0;
}

基本上,我会将上述代码封装在一个 if 语句中,该语句指定在提出每个问题后生成哪个数字。我认为您只需将 if 语句放在某种 while 循环中即可。谢谢!

最佳答案

我会做这样的事情:

#include <random>

int RandomBetween(int p_min, int p_max)
{
static default_random_engine generator;
std::uniform_int_distribution<int> distribution(p_min, p_max);
return distribution(generator);
}

之后,你可以在switch中调用这个函数:

switch(RandomBetween(a, b))
{
...
}

如果你想要一个完整的测验程序的例子,我做了这个:

#include <string>
#include <iostream>
#include <tuple>
#include <chrono>
#include <vector>
#include <algorithm>
#include <random>

std::string ToLower(std::string p_target)
{
std::transform(p_target.begin(), p_target.end(), p_target.begin(), [](unsigned char c) { return std::tolower(c); });
return p_target;
}

int RandomBetween(int p_min, int p_max)
{
static std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(p_min, p_max);
return distribution(generator);
}

class Question
{
public:
Question(const std::string& p_message, const std::string& p_answer, bool p_caseSensitive) :
m_message(p_message),
m_answer(p_answer),
m_caseSensitive(p_caseSensitive)
{}

std::tuple<bool, float> Ask() const
{
std::string answer;

std::cout << m_message << std::endl;

std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
std::cin >> answer;
end = std::chrono::system_clock::now();

return { answer == m_answer || (!m_caseSensitive && ToLower(answer) == ToLower(m_answer)), std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.0f };
}

private:
std::string m_message;
std::string m_answer;
bool m_caseSensitive;
};

int main()
{
bool gameRunning = true;
std::vector<Question> questions;

questions.emplace_back("Capital of France?", "Paris", false);
questions.emplace_back("Is C++ object oriented? [Y/n]", "y", false);

while (gameRunning)
{
const auto& [success, time] = questions[RandomBetween(0, 1)].Ask();

std::string answerSpeed = "(Answered in: " + std::to_string(time) + "s)";
if (success)
{
std::cout << "You are right!" << answerSpeed << std::endl;
}
else
{
std::cout << "You failed... Game is over!" << answerSpeed << std::endl;
gameRunning = false;
}
}

return 0;
}

哪个可以(因为是随机游戏)输出:

Capital of France?
Paris
You are right!(Answered in: 1.705000s)
Capital of France?
Paris
You are right!(Answered in: 2.368000s)
Capital of France?
Paris
You are right!(Answered in: 1.657000s)
Is C++ object oriented? [Y/n]
y
You are right!(Answered in: 3.927000s)
Capital of France?
I don't know
You failed... Game is over!(Answered in: 12.921000s)

关于c++ - 如何使用 cpp 设置随机数生成器的语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com