Advertisement
Guest User

Untitled

a guest
Mar 24th, 2011
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. //Random wind speed generator with 0.05% possibility of storm with winds 10 mph higher
  2.  
  3. //Initialize objects
  4. #include<iostream>
  5. #include<iomanip>
  6. #include<fstream>
  7. #include<ctime>
  8. #include<cstdlib>
  9. double a,b;
  10. double random(double a, double b);
  11. double random_gust(double a, double b);
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16.     //User input for wind range
  17.     cout<<"Please enter range for wind speed"<<endl;
  18.     cin>>a>>b;
  19.  
  20.     //Seed for random number generator, based on computer clock
  21.     srand(time(NULL));
  22.    
  23.     //Open data file
  24.     ofstream outfile;
  25.     outfile.open("wind.txt");
  26.  
  27.     //Write header
  28.     outfile<<"TIME"<<setw(10)<<"SPEED"<<endl;
  29.  
  30.     //For loop to write normal data
  31.     for(int i=0; i<=3600; i=i + 10)
  32.     {
  33.         //If else for possibility of storm
  34.         if (rand()%1001 <=5)
  35.         {
  36.             //For loop to write storm data
  37.             for(int k=i; k<=i+300; k=k + 10)
  38.             {
  39.                 outfile<<k<<setw(10)<<setprecision(4)<<random_gust(a, b)<<" ***** "<<endl;
  40.             }
  41.             i=i+300;
  42.         }
  43.         else
  44.         {
  45.         outfile<<i<<setw(10)<<setprecision(4)<<random(a, b)<<endl;
  46.         }
  47.     }
  48.     outfile.close();
  49. }
  50.  
  51. //Module for normal wind speeds
  52. double random(double a, double b)
  53. {
  54.     return ((double)rand()/RAND_MAX)*(b-a) + a;
  55. }
  56.  
  57. //Module for storm wind speeds
  58. double random_gust(double a, double b)
  59. {
  60.     return ((double)rand()/RAND_MAX)*((b+10)-(a+10)) + (a+10);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement