伪随机数生成错误

时间:2014-11-17 18:29:52

标签: c++

我是开发领域的初学者,我面临着C ++语言中伪代码随机错误的问题。 我认为我的程序中的错误是:         {         srand(time(NULL));     }

请帮助我如何删除此错误和错误原因。 我开发的程序如下,

#include<iostream>
using namespace std;
main()
{
       int lowerRange;
       int upperRange;
       lowerRange=1;
       upperRange=1;
       int secretNumber;
       int guess;
       guess=10;

       cout<<"My Student ID is BC130400789 "<<endl;
       cout<<"Enter lower range : ";
       cin>>lowerRange;

       cout<<"Enter upper range : ";
       cin>>upperRange;

       cout<<"Computer is calculating a random secret number in the given range...Done!"<<endl;
       cout<<"\nPlease guesss the secret number in the range ["<<lowerRange<<" - "<<upperRange<<"]: ";
       cin>>guess;

       if(guess<10)
       {
                    cout<<"You won! You guess the correct number.. ";
                    }
        else 
        {
             cout<<"Oooppsss...Your entered number is too high...Computer won"<<endl<<endl;

            }


            { srand(time (NULL));}


             secretNumber = rand()%10+1;
             cout<<"Secret number was: "<<secretNumber<<endl<<endl;
             }
                         system("pause");
       }

2 个答案:

答案 0 :(得分:1)

  1. main HAS TO return int
  2. rand()在cstdlib中定义,不包括
  3. 您还需要包含ctime for time function
  4. 正如G. Samaras所说,你不应该使用系统(“暂停”)(你也没有包含所需的标题),而且你有{}不匹配。
  5. 我还建议您阅读有关代码格式的内容。

答案 1 :(得分:0)

最后你有一个额外的括号。删除此项和system("pause");,您将不会收到任何错误。另外main()通常会返回int,因此会int main()


system(“pause”); - Why is it wrong?