每次循环时都会创建相同的随机数。

时间:2013-05-20 06:25:28

标签: c++ loops random numbers nested-loops

我是C ++的初学者,我的一个项目涉及循环内部循环和创建随机数。这是我到目前为止: `

using namespace std;

int main()
{   
    srand((unsigned int)time(0));

{
    cout << "Name of reservoir: ";
    string reservior_name;
    cin >> reservior_name;

    cout << "Capacity in MAF: ";
    double capacity;
    cin >> capacity;

    cout << "Maximum inflow in MAF: ";
    int max;
    cin>> max;

    cout << "minimum inflow in MAF: ";
    int min;
    cin >> min;
    if(min>max)
    {cout<<endl<<"Error: The minimum inflow is higher than the maximum inflow."<<endl
         << "Please re-enter your minimum inflow: ";
    cin>>min;
    }
    double inflow_range= max-min;

    cout <<"required outflow in MAF: ";
    double required;
    cin >> required;
    if (required > 0.9 * (min + max)/2)
    {
        cout<<endl<< "Warning: required ouflow is over 90% of the average inflow."<<endl
            << "Returning to main menu ";
    }
    else
    { const     int simulations = 10;
                int water_level = 0;
                int years = 1;
                cout << "Running simulation..." << endl;
                for (int i = 1; i <= simulations; i++) 
                {
                    int x = (rand()% (max-min + 1)) + min;

                    while (water_level < capacity)
                    {
                        //double r = rand() * 1.0 / RAND_MAX;
                        //double x = min + inflow_range * r;
                        //int x = (rand()% (max-min + 1)) + min;
                        if (water_level + x > required)
                        {
                        water_level = water_level + x - required;
                        }
                        else
                        {
                            water_level= 0;
                        }
                        years++;

                    }   
                cout <<"Simulation "<< i <<" took " << years <<" years to finish"<< endl;
                }
    }
}
system ("pause");
return 0;       
}
`

所以我的主要问题是我遇到了关于在“运行模拟”下面设置for循环的问题,我需要设置第一个for循环来运行内部for循环10次,其中每个循环10次内部for循环的迭代产生随机数,用于查询随机值的可接受结果范围。我被告知这个想法是使用蒙特卡罗方法,即我在这里放入蒙特卡罗方法和普通随机数生成方法。这是:

for (int i = 1; i <= simulations; i++) 
                {
                    int x = (rand()% (max-min + 1)) + min;

                    while (water_level < capacity)
                    {
                        //double r = rand() * 1.0 / RAND_MAX;
                        //double x = min + inflow_range * r;
                        //int x = (rand()% (max-min + 1)) + min;

所以程序会为流入创建一个随机值。我们的想法是内部for循环将继续运行,直到容器的fill_level(从0开始)达到容量。模拟多少年(每个迭代的内部for循环代表一年)的过程将由父级重复10次循环water_level模拟for循环。

问题是应该创建的随机数是相同的数字。每次运行它时都不同,但每次循环重复进行新模拟时它们都是相同的。我试图弄清楚问题是几个小时仍然卡住了。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

x在您的代码中是随机的,问题是之后的算法和计算。查看您的代码live

你忘了在每次迭代时重置模拟参数,把它们放在模拟循环中:

--------------------------------------------+
                                            |
for (int i = 1; i <= simulations; i++)      |
{                                           |
  int water_level = 0;                   <--+
  int years = 1;                         <--+
  int x = (rand() % (max - min + 1)) + min;

请参阅此版本后的代码:live code。输出是

Simulation 1 took 68 years to finish
Simulation 2 took 101 years to finish
Simulation 3 took 8 years to finish

答案 1 :(得分:0)

使用所示的代码,每次迭代(模拟)在模拟的所有年份都获得x的单个值。您注释掉的代码会为每年生成x的新值。你想要哪种方法?我倾向于认为流入量每年都有所不同,因此每年应为x生成一个新值。

看起来您应该为每次模拟重置yearswater_level

cout << "Running simulation..." << endl;
for (int i = 1; i <= simulations; i++) 
{
    int water_level = 0;
    int years = 1;

    while (water_level < capacity)
    {
        int x = (rand() % (max - min + 1)) + min;
        if (water_level + x > required)
            water_level += x - required;
        else
            water_level = 0;
        years++;
    }
    cout <<"Simulation "<< i <<" took " << years <<" years to finish"<< endl;
}

为了进行调试,我想打印控件参数(minmaxcapacityrequired),然后打印键值({ {1}},yearx)在内部water_level循环的每次迭代中,直到我满意它才能正常工作。