Same random numbers being created every time the loop goes around

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khangho
    New Member
    • May 2013
    • 1

    Same random numbers being created every time the loop goes around

    Hi. I am a beginner at C++ and for one of my project involves loop inside loops and creating random numbers. Here is what I have so far:#include <iostream>
    Code:
    #include <string>
    #include <cstdlib>
    #include <ctime>
    #include <iomanip>
    #include <cstdio>
    #include<random>
    
    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 <<"Simulations"<< i << "took" << years <<" to finish"<< endl;
    					}
    		}
    	}
    system ("pause");
    return 0;		
    }

    So my main question is I'm running into a wall concerning setting up the for loops underneath "Running simulation" where I need to set up the first for loop to run the internal for loop 10 times, with each of those 10 iterations of the internal for loop coming up with random numbers for the range of acceptable results from the query for a random value. I've been told that the idea is to use the Monte Carlo method, i.e. I put in here both the Monte Carlo method and the normal random number generating method. Here it is:
    Code:
     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;
    so the program will create a random value for the inflow. The idea is that the internal for loop will continue to run until the fill_level of the reservoir, which starts at 0, hits the capacity. The process of simulating how many years (each iteration of the internal for loop representing a year) is to be repeated 10 times by the parent for loop of the water_level simulation for loop.

    The problem is that the random number that is supposed to created are the same number. THey are different every time I run it, but they are the same every time the loops repeat to make a new simulation. I have tried to figure out what the problem is for hours and still stuck. Any help is very appreciated.
    Last edited by Rabbit; May 20 '13, 01:40 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    That's because you're seeding the RNG with the same number every time on line 12.

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      [Rabbit] It looks like he is seeding the random number generator off of the current time. This should work, unless his clock is broken.

      [khangho] What you are saying is that x is constant for all simulations?

      Look at line 52, which generates x, which is apparently your inflow rate. This is going to be constant across the water level loop at line 54, so if the inflow is less than the required amount, the lake will ultimately drop to zero and stay there. Probably not what you want.

      Why are lines 56 through 58 commented out? These look to be the correct place to generate your random value, not outside the loop.

      Hopefully that helps a little,
      Oralloy

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        The resolution of the time_t value returned by time() [line 12] is implementation defined. It might be microseconds, milliseconds, seconds, minutes, hours, days, or anything. You can't be sure that two successive calls to time() will return different values.

        time() returns -1 if there is a problem with the operating system timekeeping facility. If there were a problem then every call to time() is sure to return the same value.

        Comment

        Working...