產生指數隨機數的兩種方法的比較…

來源:互聯網
上載者:User

800)  r--->0  r末尾溢出
ln(r)  -inf 
所以連乘, n不易過大.....
改進NewRandom_exp2.m
對於任意n, 分段處理,可分m+1段
m=[n/100](取整)    尾段=mod(n,100) 模數
100對於連乘適中..  對於誤差適中...
test.m測試程式...(測試環境 P-m 1.6G  512m DDR400)
產生100000個。。。。(S)
>> [Time1,Time2]=test(100000)
Time1 is the runtime of random_exp1 Time1 =   78.0422m Time2 =    1.0715
                                                                                         made by aris  2006 3 20

 

%exponent distribution1........
function [ExpRandom,RunTime]=random_exp1(n);
t=cputime;
ExpTemp=[];
for i=1:n
    ExpTemp(i)=-log(rand);
end
RunTime=cputime-t;
ExpRandom=ExpTemp;

%exponent distribution2........
function [ExpRandom,RunTime]=random_exp2(n)
t=cputime;
ExpTemp=[];
RandTemp1=rand(1,n);
RandTemp2=rand(1,n-1);
r=1;
for i=1:n
    r=r*RandTemp1(i);
end
r=log(r);
RandTemp2=[0,RandTemp2,1];
RandTemp2=sort(RandTemp2);
for i=1:n
    ExpTemp(i)=(RandTemp2(i)-RandTemp2(i+1))*r;
end
RunTime=cputime-t;
ExpRandom=ExpTemp;

 

%exponent distribution........
function [ExpRandom,RunTime]=NewRandom_exp2(n)
t=cputime;
m=int16(n/100);
l=mod(n,100);
RandTemp=[];
for i=1:m
    RandTemp(i,:)=random_exp2(100);
end
RandTemp=vec(RandTemp);
RandTempl=random_exp2(l);
ExpRandom=[RandTemp';RandTempl];
RunTime=cputime-t;

test the [ExpRandom,RunTime]=random_exp1(n)
%         [ExpRandom,RunTime]=random_exp2(n)
%         [ExpRandom,RunTime]=NewRandom_exp2(n)
function [Time1,Time2]=test(n);
[ExpRandom,RunTime1]=random_exp1(n);
[ExpRandom,RunTime2]=NewRandom_exp2(n);
disp('Time1 is the runtime of random_exp1');
Time1=RunTime1;
Time2=RunTime2;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.