zhishu.cpp

来自「drand()产生0~1之间的随机数以及产生负指数分布的随机数」· C++ 代码 · 共 58 行

CPP
58
字号
#include "stdio.h"
#include "math.h"
#include "time.h"
#include "stdlib.h"
double   Drand()//drand()产生0~1之间的随机数   
  {   
  float   x;   
  int   i;   
  for(i=0;i<20;i++)rand();   
  x=rand();   
  x=65539*x+1743251541;   
  x=fmod(x,2147483638);   
  return(x/2147483638);   
  }   
float   Expon(float   fl)//   产生负指数分布的随机数   
  {   
  float   v,u;   
  u=Drand();   
  v=-fl*log(u);   
  return   v;   
  }   
    
  float   poissn(float   la)//poissn()产生泊松分布随机数   
  {   
  int   k=0;   
  float   b,t=1.0f,r;   
  b=exp(-la);   
  while((t-b)>=0)   
  {   
  r=Drand();   
  t=t*r;   
  k=k+1;   
  }   
  return   k;   
  }   
    
  float   pgauss(float   k,float   j)//pgauss()产生正态分布随机数   
  {   
  float   v1,v2,s,w,y,sg[2];   
  do{v1=Drand();   
        v2=Drand();   
        s=v1*v1+v2*v2;   
        }   
  while(s>=1);   
  w=sqrt(-2.0*log(s)/s);   
  sg[0]=v1*w;   
  sg[1]=v2*w;   
  y=k*(sg[0]+sg[1])/2+j;   
  return   y;   
  }   
  

void main()
{float t,y;
printf("input lamda\n");
scanf("%f",&t);
y=Expon(t);
printf("%f\n",y);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?