c++ - Same random numbers being created everytime the loop goes around. -
i beginner @ c++ , 1 of project involves loop inside loops , creating random numbers. here have far: `
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: minimum inflow higher maximum inflow."<<endl << "please re-enter 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 on 90% of average inflow."<<endl << "returning main menu "; } else { const int simulations = 10; int water_level = 0; int years = 1; cout << "running simulation..." << endl; (int = 1; <= 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 "<< <<" took " << years <<" years finish"<< endl; } } } system ("pause"); return 0; } ` so main question i'm running wall concerning setting loops underneath "running simulation" need set first loop run internal loop 10 times, each of 10 iterations of internal loop coming random numbers range of acceptable results query random value. i've been told idea use monte carlo method, i.e. put in here both monte carlo method , normal random number generating method. here is:
for (int = 1; <= 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 program create random value inflow. idea internal loop continue run until fill_level of reservoir, starts @ 0, hits capacity. process of simulating how many years (each iteration of internal loop representing year) repeated 10 times parent loop of water_level simulation loop.
the problem random number supposed created same number. different every time run it, same every time loops repeat make new simulation. have tried figure out problem hours , still stuck. appreciated.
the x random in code, problem algorithm , calculation after that. see code live.
you've forgotten reset simulation parameter @ each iteration, put these inside simulation loop:
--------------------------------------------+ | (int = 1; <= simulations; i++) | { | int water_level = 0; <--+ int years = 1; <--+ int x = (rand() % (max - min + 1)) + min; see code after edition: live code. output is
simulation 1 took 68 years finish simulation 2 took 101 years finish simulation 3 took 8 years finish
Comments
Post a Comment