【问题标题】:rand() generator stuck on 1 number c++ [duplicate]rand() 生成器卡在 1 个数字 c++ [重复]
【发布时间】:2015-02-23 09:43:52
【问题描述】:

我正在编写一个带有返回随机数的函数的代码。随机数是从 0 到我输入的最大数生成的。除此之外,我必须生成 3 个随机数,然后随机数将返回 char '-' '+' '*' 。代码不能正常工作。每当我输入 100 时,生成的随机数始终为 41,而生成 char 的另一个随机数始终生成 - 带有笑脸的符号(不是开玩笑,char 符号旁边会出现一个笑脸)。谁能告诉我我的代码有什么问题?非常感谢您帮助我。

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    int getNumber(int);
    char getOperator(int);
    int max = 0;
    int randOperator = 0;

    cout << "Enter a value(1- 101)" << endl;
    cin >> max;

    while((max < 0) || (max > 101))
    {
        cout << "invalid value. Please enter a value (1 - 101)" << endl;
        cin >> max;
    }

    cout << endl;
    cout << getNumber(max) << endl;

    cout << getOperator(randOperator);


}

int getNumber(int max)
{
    int hehe;
    hehe = rand()% max;
    return hehe;
}

char getOperator(int randOperator)
{
    for(int x = 0; x < 5; x++)
    {
        randOperator = rand() % 3;

        if(randOperator = 0)
        {
            cout << '+';
        }else if(randOperator = 1)
        {
            cout << '-';
        }else
        {
            cout << '*';
        }

        return randOperator;
    }
}

【问题讨论】:

  • srand() 播种你的rand。您可以在谷歌中搜索它。你会得到答案的。

标签: c++


【解决方案1】:

此代码应该可以解决您的问题

#include <iostream>
#include <cmath>
#include <cstdlib>
#include<time.h>
using namespace std;

int main()
{
    srand(time(NULL));
    int getNumber(int);
    char getOperator(int);
    int max = 0;
    int randOperator = 0;

    cout << "Enter a value(1- 101)" << endl;
    cin >> max;

    while((max < 0) || (max > 101))
    {
        cout << "invalid value. Please enter a value (1 - 101)" << endl;
        cin >> max;
    }

    cout << endl;
    cout << getNumber(max) << endl;

    cout << getOperator(randOperator);


}

int getNumber(int max)
{
    int hehe;
    hehe = rand()% max;
    return hehe;
}

char getOperator(int randOperator)
{
    for(int x = 0; x < 5; x++)
    {
        randOperator = rand() % 3;

        if(randOperator == 0)
        {
            return '+';
        }else if(randOperator == 1)
        {
            return '-';
        }else
        {
            return '*';
        }

    }
}

要在每次运行程序时生成不同的随机数,您需要播种rand()。您可以使用我在这段代码中使用的srand() 来做到这一点。

你犯的一个粗心的错误是

if(randOperator = 0)

else if(randOperator = 1)

如果您仔细观察,您会发现您使用= 而不是== 来检查是否相等。 = 将分配值,因此要检查是否相等,请使用 ==,如我的代码中所示。

现在在这部分代码中

char getOperator(int randOperator)
{
    for(int x = 0; x < 5; x++)
    {
        randOperator = rand() % 3;

        if(randOperator = 0)
        {
            cout << '+';
        }else if(randOperator = 1)
        {
            cout << '-';
        }else
        {
            cout << '*';
        }

        return randOperator;
    }
}

函数的返回类型是 char,而您返回的是 int。在我的代码中,我没有使用cout &lt;&lt; '+';,而是使用了return '+';。通过这样做,操作符将如您所愿返回。

嗯,这就是解释。如果你需要更多解释,那就问吧。

【讨论】:

  • @DownVoter,最好能给出拒绝投票的理由。只有这样我才能纠正它。
  • 我也讨厌这样。投反对票,不发表评论。我投票给你。
  • @Lightness Races in Orbit,如果你不知道,那我怎么知道我的答案有什么问题?如果您将问题通知我,那么我将能够更正它并且将来也不会重复它。正是通过这种方式,我们才能提高自己。
  • @Lightness Races in Orbit,是的,确实如此。如果我们不允许告诉别人他们的错误,那么新手如何能够以正确的方式发布答案?
  • @ArunA.S:我们被允许在答案上写 cmets 并指出问题。但我们不允许表明反馈与否决票相关联。这很奇怪。当我投反对票时,我想知道为什么投反对票,即“-1:因为....”
【解决方案2】:

有两个错误:

第一个错误: 在您的函数 getOperator 中,您在 if 语句中分配一个值,而不是检查是否相等!为此,您必须使用==

因此,您总是得到“-”作为结果。因为randOperator=1 的结果为 1,结果为真。

 char getOperator(int randOperator)
 {
    for(int x =0; x < 5; x++)
    {
        randOperator = rand() % 3;

        if(randOperator == 0) //!!!!! You used (randOperator = 0) which will be interpreted as false
        {
            cout << '+';
        }else if(randOperator == 1) // !!!!!! You used (randOperator = 1) which will be interpreted as true
        {
            cout << '-';
        }else
        {
            cout << '*';
        }

        return randOperator;
    }
}

第二个bug(笑脸)

你把操作员拿出来并返回 rand(笑脸)的值,这被 for 覆盖,它只运行一次(因为返回)?!

试试这个:

 char getOperator(int randOperator)
 {
        randOperator = rand() % 3;

        if(randOperator == 0) //!!!!! You used (randOperator = 0) which will be interpreted as false
        {
            return '+';
        }else if(randOperator == 1) // !!!!!! You used (randOperator = 1) which will be interpreted as true
        {
            return '-';
        }
        return '*';
}

仍然丑得要命,但它会起作用。

【讨论】:

  • 显然我们不应该在 return 语句之后添加
  • @NumbNuts 对不起错字(旧的副本和过去的错误):-)
  • 谢谢。现在可以了。我现在唯一的问题是如何在我的常规随机数生成器上修复一般的 rand() 。我应该使用 srand() 而不是 rand()。因为 rand() % max 永远不会在我设置 max = 100 时生成不同的数字。
  • @NumbNuts,实际上你需要同时使用两者,srand() 只是种子rand(),简单地说,通过在rand() 之前使用srand(),你将能够产生不同的价值。检查我的答案,它显示了如何。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 2012-09-06
  • 2021-11-03
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多