全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2103
推到 Plurk!
推到 Facebook!

用C++定義uniform r.v..來產生各種distribution

尚未結案
zargminidisc
一般會員


發表:3
回覆:1
積分:0
註冊:2005-06-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-07 09:39:06 IP:220.137.xxx.xxx 未訂閱
大家好 我是一個C++的新手 這裡有個題目..說要用C++跑出規定的東西 附上我的程式碼 可以請高手幫我看看哪裡有問題嗎 感謝 程式碼如下
using std::cout;
using std::endl;
using std::cerr;
using std::ios;
                                                                                
#include
#include
using std::ofstream;
                                                                                
#include
#include
#include    // generate array of uniform distribution on [0,1]
void uniform( double rand_uni [] , int size ){
                                                                                
       srand( time(0) );
       double max = RAND_MAX;
       double random;
                                                                                
       for( int i = 0; i < size; ++i ){
            random = rand();
            rand_uni[i] = random/max;
       }
                                                                                
}// end uniform
                                                                                
// generates binomial distribution
void binomial( int n, double p ){
                                                                                
     ofstream output_binomial("binomial.txt", ios::out);
                                                                                
     double rand_uni[n*1000];
     int binomial_out[1000];
     for( int i = 0; i < 1000; ++i ){
          binomial_out[i] = 0;             binomial_out[i] = 0;
     }
                                                                                
     uniform( rand_uni, n*1000 );
                                                                                
     for( int i = 0; i < 1000; ++i ){
          for( int j = 0; j < n; ++j ){
               if( rand_uni[n*i+j] <= p )
                   ++binomial_out[i];
          }
     }// end outer for
                                                                                
     for( int i = 0; i < 1000; ++i ){
          output_binomial << binomial_out[i] << " ";
          if( ((i+1))==0 )
              output_binomial << endl;
     }
                                                                                
     output_binomial.close();
                                                                                
}// end binomial
                                                                                
// generate exponential distributions
// generate exponential distributions
void exponential( double lambda ){
                                                                                
     ofstream output_exponential("exponential.txt", ios::out);
                                                                                
     double rand_uni[1000];
     uniform( rand_uni, 1000 );
                                                                                
     double exponential_out[1000];
     for( int i = 0; i < 1000; ++i ){
          exponential_out[i] = ((-1)/(lambda))*log(rand_uni[i]);
     }
                                                                                
     for( int i = 0; i < 1000; ++i){
          output_exponential << exponential_out[i] << " ";
          if( ((i+1)%5)==0 )
              output_exponential << endl;
     }
                                                                                
     output_exponential.close();
                                                                                
}// end exponential
// generate normal distribution
void normal( double mean, double variance ){
                                                                                
     ofstream output_normal("normal.txt", ios::out);
                                                                                
     double rand_uni_1[1000];
     double rand_uni_2[1000];
     uniform( rand_uni_1, 1000 );
     uniform( rand_uni_2, 1000 );
                                                                                
     double normal_out[1000];
     for( int i = 0; i < 1000; ++i ){
          normal_out[i] = ( sqrt(variance) * cos(2*3.1415926*rand_uni_1[i]) *
                            sqrt( (-2)*(log(rand_uni_2[i])) ) ) + mean;
     }
                                                                                
     for( int i = 0; i < 1000; ++i ){
          output_normal << normal_out[i] << " ";
          if( ((i+1)%5)==0 )
              output_normal << endl;
     }
                                                                                
     output_normal.close();
                                                                                
}// end normal
                                                                                
                                                                                
                                                                                
                                                                                
int main( int argc, char* argv[] )
{
   if( argc > 1 ){
    if( (!strcmp( argv[1], "binomial" )) && (argc == 4) ){
        int n;
        double p;
        char* test1;
        char* test2;
        n = strtol( argv[2], &test1, 10 );
        p = strtod( argv[3], &test2 );
                                                                                
        if( *test1 != 0 ){
            cout << "Wrong Parameter!";
            exit(1);
        }
    }
        else if( n <= 0 ){
             cout << "Wrong Parameter!";
             exit(1);
        }
                                                                                
        if( *test2 != 0 ){
            cout << "Wrong Parameter!";
            exit(1);
        }
        else if( (p<0) || (p>1) ){
             cout << "Wrong Parameter!";
             exit(1);
        }
                                                                                
        binomial( n , p );
                                                                                
    }
    else if( (!strcmp( argv[1], "exponential" )) && (argc == 3) ){
         double lambda;
         char* test;
         lambda = strtod( argv[2], &test );
  if( *test != 0 ){
            cout << "Wrong Parameter!";
            exit(1);
         }
         else if( lambda <= 0 ){
            cout << "Wrong Parameter!";
            exit(1);
         }
                                                                                
         exponential( lambda );
                                                                                
    }
    else if( (!strcmp( argv[1], "normal" )) && (argc == 4) ){
         double mean;
         double variance;
         char* test1;
         char* test2;
         mean = strtod( argv[2], &test1 );
         variance = strtod( argv[3], &test2 );
                                                                                
         if( *test1 != 0 ){
            cout << "Wrong Parameter!";
    cout << "Wrong Parameter!";
            exit(1);
         }
                                                                                
         if( *test2 != 0 ){
            cout << "Wrong Parameter!";
            exit(1);
         }
         else if( variance <= 0 ){
            cout << "Wrong Parameter!";
            exit(1);
         }
                                                                                
         normal( mean, variance );
                                                                                
    }
    else{
         cout << "Wrong Input!";
         exit(1);
    }
   }// end of outmost if
                                                                                
    return 0;
  return 0;
                                                                                
} // end main
發表人 - zargminidisc 於 2005/06/07 09:53:55
系統時間:2024-05-18 5:39:02
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!