473,473 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Same random numbers being created every time the loop goes around

1 New Member
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>
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iomanip>
  5. #include <cstdio>
  6. #include<random>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {    
  12.     srand((unsigned int)time(0));
  13.  
  14.     {
  15.         cout << "Name of reservoir: ";
  16.         string reservior_name;
  17.         cin >> reservior_name;
  18.  
  19.         cout << "Capacity in MAF: ";
  20.         double capacity;
  21.         cin >> capacity;
  22.  
  23.         cout << "Maximum inflow in MAF: ";
  24.         int max;
  25.         cin>> max;
  26.  
  27.         cout << "minimum inflow in MAF: ";
  28.         int min;
  29.         cin >> min;
  30.         if(min>max)
  31.         {cout<<endl<<"Error: The minimum inflow is higher than the maximum inflow."<<endl
  32.              << "Please re-enter your minimum inflow: ";
  33.         cin>>min;
  34.         }
  35.         double inflow_range= max-min;
  36.  
  37.         cout <<"required outflow in MAF: ";
  38.         double required;
  39.         cin >> required;
  40.         if (required > 0.9 * (min + max)/2)
  41.         {
  42.             cout<<endl<< "Warning: required ouflow is over 90% of the average inflow."<<endl
  43.                 << "Returning to main menu ";
  44.         }
  45.         else
  46.         { const     int simulations = 10;
  47.                     int water_level = 0;
  48.                     int years = 1;
  49.                     cout << "Running simulation..." << endl;
  50.                     for (int i = 1; i <= simulations; i++) 
  51.                     {
  52.                         int x = (rand()% (max-min + 1)) + min;
  53.  
  54.                         while (water_level < capacity)
  55.                         {
  56.                             //double r = rand() * 1.0 / RAND_MAX;
  57.                             //double x = min + inflow_range * r;
  58.                             //int x = (rand()% (max-min + 1)) + min;
  59.                             if (water_level + x > required)
  60.                             {
  61.                             water_level = water_level + x - required;
  62.                             }
  63.                             else
  64.                             {
  65.                                 water_level= 0;
  66.                             }
  67.                             years++;
  68.  
  69.                         }   
  70.                     cout <<"Simulations"<< i << "took" << years <<" to finish"<< endl;
  71.                     }
  72.         }
  73.     }
  74. system ("pause");
  75. return 0;        
  76. }

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:
Expand|Select|Wrap|Line Numbers
  1.  for (int i = 1; i <= simulations; i++) 
  2.                     {
  3.                         int x = (rand()% (max-min + 1)) + min;
  4.  
  5.                         while (water_level < capacity)
  6.                         {
  7.                             //double r = rand() * 1.0 / RAND_MAX;
  8.                             //double x = min + inflow_range * r;
  9.                             //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.
May 20 '13 #1
3 1646
Rabbit
12,516 Recognized Expert Moderator MVP
That's because you're seeding the RNG with the same number every time on line 12.
May 20 '13 #2
Oralloy
988 Recognized Expert Contributor
[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
May 20 '13 #3
donbock
2,426 Recognized Expert Top Contributor
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.
Jun 7 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: AndyW | last post by:
hey folks, Just have some code as follows:- for( int i=0; i<10; i++) { Random randomNumberGenerator; randomNumberGenerator = new Random(); int num = randomNumberGenerator.nextInt(10);...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
3
by: JoelPJustice | last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet...
3
by: clsmith66 | last post by:
I'm hoping someone can help me with a problem I'm having using the random class. I need to return a "random" number between 0 and 15, so i did this Dim r as new Random() Return r.Next(0, 15) ...
5
by: Anthony | last post by:
For Some reason, i get the same value everytime... Ive tested the RND Function with msgboxes and labels, they all have no influence on the RND Function... It still doesnt work... Private Sub...
6
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
8
by: Aspidus | last post by:
Hi, I wrote a function that generates a ulong I use it 3 times, one after the other, but it generates always the same numbers. At the end i get always 3 equal arrays. Any idea?
4
by: Buckaroo Banzai | last post by:
Hi, I'm trying to generate 3 random numbers inside a while loop. The problem I encountered is that I always get the same 3 numbers even after I reseed the number using srand((unsigned)time(NULL));...
14
Deathwing
by: Deathwing | last post by:
Hi everyone, I'm fairly new with python and as such am playing around alot with just basic coding/scripting. I've spent the better part of today working with random numbers. Thanks for all your...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.