⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zhishu.cpp

📁 drand()产生0~1之间的随机数以及产生负指数分布的随机数
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -