• 沙河骑士
    了解作者
  • C/C++
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 6
    下载次数
  • 2006-12-28 11:53
    上传日期
用c/c++产生源码. 本人平时收集的. 主要用于算法仿真. Park and Miller with Bays-Durham shuffle and added safeguards
writenoise_cpp.rar
  • www.pudn.com.txt
    218B
  • writenoise.cpp
    4.2KB
内容介绍
#ifndef _RANDOM_H #define _RANDOM_H int seed; void setSeed(int s) { seed=s; } /* L'Eculer with Bays-Durham shuffle and added safeguards. Return a uniform random deviate between 0.0 and 1.0(exclusive of the endpoint values). Call with seed a negative integer to initialize; thereafter, do not alter seed between successive deviates in a sequence. RNMX should approaimate the largest floating value that is less than 1. this algorithm is ok when the required random numbers large than 100,000,000.*/ double ranLE(int &seed) { const int IM1=2147483563, IM2=2147483399; const int IA1=40014,IA2=40692,IQ1=53668,IQ2=52774; const int IR1=12211,IR2=3791,NTAB=32,IMM1=IM1-1; const int NDIV=1+IMM1/NTAB; const double EPS=3.0e-16,RNMX=1.0-EPS,AM=1.0/double(IM1); static int seed2=123456789,iy=0; static int iv[NTAB]; int j,k; double temp; if(seed<0) //initialize { seed=(seed==0 ? 1 : -seed); // Be sure to prevent seed=0. seed2=seed; for(j=NTAB+7;j>=0;j--) // load the shuffle table(after 8 warm-ups) { k=seed/IQ1; seed=IA1*(seed-k*IQ1)-k*IR1; if(seed<0) seed+=IM1; if(j<NTA iv[j]=seed; } iy=iv[0]; } k=seed/IQ1; // Start here when not initializing seed=IA1*(seed-k*IQ1)-k*IR1; // compute seed=(IA1*seed) % IM1 without overflows by if(seed<0) // Schrage's method seed+=IM1; k=seed2/IQ2; seed2=IA2*(seed2-k*IQ2)-k*IR2; // Compute seed2=(IA2*seed) % IM2 likewise. if(seed2<0) seed2+=IM2; j=iy/NDIV; // Will be in the range 0..NTAB-1 iy=iv[j]-seed2; //Here seed is shuffled, seed and seeds are combined iv[j]=seed; //to generate out put if(iy<1) iy+=IMM1; if((temp=AM*iy)>RNMX) // Because users don't expect endpoint values. return RNMX; else return temp; } /* Park and Miller with Bays-Durham shuffle and added safeguards. Returns a uniform * random deviate between 0.0 and 1.0(exclusive of the endpoint * values). Call with seed a negative integer to initialize; thereafter, do not alter seed between successive deviates in a sequence. RNMX should approaimate the largest floating value that is less than 1. this algorithm is ok when the required random numbers less than * 100,000,000. */ double ranPM(int &seed) { const int IA=16807,IM=2147483647,IQ=127773,IR=2836,NTAB=32; const int NDIV=(1+(IM-1)/NTA; const double EPS=3.0e-16,AM=1.0/IM,RNMX=(1.0-EPS); static int iy=0; static int iv[NTAB]; int j,k; double temp; if(seed<=0 || !iy) // initialize, be sure to prevent seed=0. { if(-seed<1) seed=1; else seed=-seed; for(j=NTAB+7;j>=0;j--) { k=seed/IQ; seed=IA*(seed-k*IQ)-IR*k; if(seed < 0) seed += IM; if(j<NTA iv[j]=seed; } iy=iv[0]; } k=seed/IQ; // start here when not initializing. seed=IA*(seed-k*IQ)-IR*k;// compute seed=(IA*seed)%IM without overflows by Schrage's method. j=iy/NDIV; // will be in the range 0..NTAB-1 iy=iv[j]; // output previously stored value and refill the shuffle table iv[j]=seed; if((temp=AM*iy)>RNMX) // because users don't expect endpoint values. return RNMX; else return temp; } double rnorm() { static int iset=0; static double gset; double fac,rsq,v1,v2; if(seed<0) // reinitialize iset=0; cout <<iset<<endl; if(iset==0) // we don't have an extra deviate handy, so pick two uniform numbers { // in the square extending from -1 to +1 in each direction. do { v1=2.0*ranPM(seed)-1.0; v2=2.0*ranPM(seed)-1.0; rsq=v1*v1+v2*v2; // see if they are in the unit circle, and if } while(rsq>=1.0||rsq==0.0);// they are not, try again. fac=sqrt(-2.0*log(rsq)/rsq); // Now make the Box-Muller transformation to get // two normal deviates. Return one and save the other for next time. gset=v1*fac; // we have an extra deviate handy, iset=1; // so unset the flat, return v2*fac; // and return it. } else { iset=0; return gset; } } #endif /* _RANDOM_H */ #include <iostream> #include "Random.h" using namespace std; int main() { setSeed(-5); for (int i=0;i<100;i++) { cout<< rnorm()<<endl; return 0; }
评论
    相关推荐
    • qpopper2.53.tar.Z
      pop3 server
    • imap-4.7.tar.Z
      被广泛使用的email服务器 /IMAPD/POPD
    • 53308459Add_Dlt_TabCtrl.rar
      Tab分页的删除和添加,实现分页动态的管理。
    • ns-allinone-2.33.tar.gz
      ns2.33这是目前比较新的ns2版本,欢迎下载
    • NetVideoActiveX23.rar
      海康威视 网络监控插件 带有例子 和开发的dll文件,非常易用
    • UR054g_(R01).zip
      法国inventel的ur054g(r01)v1.1的无线网卡驱动。
    • eat.rar
      外卖叫餐系统,采用ACCESS数据库,有完整天的后台管理系统
    • KSTVTUNE.ZIP
      装摄像头驱动需要用到的文件。Microsoft DirectX 9 SDK
    • Skin++.rar
      知名的Skin++界面库,内含所有库文件和大量皮肤文件.该库使用方便,可以减少您美化程序的痛苦.本版是破解版,没有注册提示.但只供学习研究使用哦,不要用在商业用途.
    • DLL函数查看器V2.0.rar
      DLL函数查看器V2.0 可以看看dll里导出地函数