• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ ran1函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中ran1函数的典型用法代码示例。如果您正苦于以下问题:C++ ran1函数的具体用法?C++ ran1怎么用?C++ ran1使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了ran1函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: ran1

void PIC::inject_beam(double boundary, double vel, double angle, double ndensity, double Tempxy, double Tempz, double dt, long *d1)
{
 double Ninj=vel*ndensity*PI*pow(boundary,2)*dt/macroN; 
 double gamma=1.0/sqrt(1.0-pow(vel/clight,2));
 double utherm=clight*sqrt(pow(charge_p*Tempxy/(mass_p*clight*clight)+1.0,2)-1.0);
 double r_amp, phase;
 double u, v, w;
 for(int j=0; j<Ninj; j++) 
   { 
	Particle part_inj;
	r_amp=boundary*sqrt(ran1(d1));
	phase=2.0*PI*ran1(d1);	
	part_inj.x=r_amp*cos(phase);
	part_inj.y=r_amp*sin(phase);
	part_inj.z=-0.5*length;
	
	do { u = 2.0 * ran1(d1) - 1.0 ;  
	     v = 2.0 * ran1(d1) - 1.0 ;              
	     w = u*u + v*v ; }
	     while (w >= 1.0) ;
	
	part_inj.ux = part_inj.x/r_amp*vel*angle + utherm * u * sqrt(-2.0*log(w)/w) ;
	part_inj.uy = part_inj.y/r_amp*vel*angle + utherm * v * sqrt(-2.0*log(w)/w) ;
	part_inj.uz = gamma*vel;
    part.push_back(part_inj);
   }	
}
开发者ID:boine,项目名称:bphylib,代码行数:27,代码来源:pic.cpp


示例2: poidev

float poidev(float xm, long int *idum)
{
 static float sq,alxm,g,oldm=(-1.0);
 float em,t,y;
 
 if (xm < 12.0) {
    if (xm != oldm) {
       oldm=xm;
       g=exp(-xm);
    }
    em = -1;
    t=1.0;
    do {
       ++em;
       t *= ran1(idum);
    } while (t > g);
 } else {
    if (xm != oldm) {
       oldm=xm;
       sq=sqrt(2.0*xm);
       alxm=log(xm);
       g=xm*alxm-gammln(xm+1.0);
    }
    do {
       do {
          y=tan(PI*ran1(idum));
	  em=sq*y+xm;
       } while (em < 0.0);
       em=floor(em);
       t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
    } while (ran1(idum) > t);
 }

 return em;
}
开发者ID:KathleenLabrie,项目名称:KLlibc,代码行数:35,代码来源:poidev.c


示例3: gasdev

/* ********************************************************/
double gasdev(long int *idum) {
/*
 *  Gaussian randon number generator. Uses ran1.
***********************************************************/
    double ran1(long int *idum);
    static int iset = 0;
    static double gset;
    double fac, rsq, v1, v2;

    if (*idum < 0) iset = 0;
    if (iset == 0) {
        do {
            v1 = 2.0 * ran1(idum) - 1.0;
            v2 = 2.0 * ran1(idum) - 1.0;
            rsq = v1 * v1 + v2 * v2;
        } while (rsq >= 1.0 || rsq == 0.0);
        fac = sqrt(-2.0 * log(rsq) / rsq);
        gset = v1 * fac;
        iset = 1;
        return v2 * fac;
    } else {
        iset = 0;
        return gset;
    }
}
开发者ID:aywander,项目名称:pluto-outflows,代码行数:26,代码来源:random.c


示例4: gen_Ak

void gen_Ak(double *Ak, int kx, int ky, int kz, double Pk, int dots)
{
    int nkx, nky, nkz;
    double phi, rnd1, rnd2, r;
    
    
    rnd1 = ran1(&iiseed);
    phi = 2 * PI * rnd1;                /* phase */

    rnd2 = ran1(&iiseed);
    r = sqrt(-2 * log(rnd2)*Pk);         /* amplitude */


    Ak[2*(kx*dots*dots + ky*dots + kz)] = r*cos(phi); /* real part */
    Ak[2*(kx*dots*dots + ky*dots + kz)+1] = r*sin(phi); /* imag. part */
    

    if((kx != 0) && (ky != 0) && (kz != 0))
      {
	  nkx = dots - kx;
	  nky = dots - ky;
	  nkz = dots - kz;

	  Ak[2*(nkx*dots*dots + nky*dots + nkz)] = r*cos(phi); /* real part */
	  Ak[2*(nkx*dots*dots + nky*dots + nkz)+1] = -r*sin(phi); /* imag. part */
      }
}
开发者ID:AthenaStacy,项目名称:gadget_bfield,代码行数:27,代码来源:rsk_turbdriving_field.c


示例5: while

void PIC::set_const_density_xy(double boundary, double T0, long *d1)
{ 
 double u,v,w;	
 double ut=clight*sqrt(pow(fabs(charge_p)*T0/(mass_p*clight*clight)+1.0,2)-1.0);
 for (int j=0 ; j < part.size(); j++)
  {
   	do {
        u = boundary*(2.0*ran1(d1)-1.0); 
     	v = boundary*(2.0*ran1(d1)-1.0);
	   } while (sqrt(pow(u,2)+pow(v,2)) > boundary);
    		
	part[j].x=u;  	
	part[j].y=v;
	part[j].z=length * ran1(d1);
	
	do { u = 2.0 * ran1(d1) - 1.0 ;  
	     v = 2.0 * ran1(d1) - 1.0 ;              
	     w = u*u + v*v ; }
	     while (w >= 1.0) ;

		part[j].ux = ut * u * sqrt(-2.0*log(w)/w) ;
	    part[j].uy = ut * v * sqrt(-2.0*log(w)/w) ;
		part[j].uz = 0.0; 
	 
  }	
}
开发者ID:boine,项目名称:bphylib,代码行数:26,代码来源:pic.cpp


示例6: sqrt

void Pic::waterbag_xy(double emittance_x, double emittance_y, double alpha_x, double alpha_y,
		double beta_x, double beta_y, double D0, double Ds0, double x0,
		double xs0, double y0, double ys0, int size, long *d){
  emittance_x *= 6.;
  emittance_y *= 6.;
  long j;
  double x, y, xs, ys;
  double xmax = sqrt(emittance_x*beta_x);
  double xsmax = sqrt(emittance_x * (1.0+alpha_x*alpha_x) / beta_x);
  double ymax = sqrt(emittance_y*beta_y);
  double ysmax = sqrt(emittance_y * (1.0+alpha_y*alpha_y) / beta_y);

  for(j=size; j<pics.size(); ++j){
    do{
      x = xmax*(2.0*ran1(d)-1.0);
      y = ymax*(2.0*ran1(d)-1.0);
      xs = xsmax*(2.0*ran1(d)-1.0);
      ys = ysmax*(2.0*ran1(d)-1.0);
  }while((x*x/beta_x + beta_x * pow(xs+alpha_x/beta_x*x, 2)) / emittance_x
	   + (y*y/beta_y + beta_y * pow(ys+alpha_y/beta_y*y, 2)) / emittance_y > 1.0);
    pics[j].x = x + D0*pics[j].dp + x0;  // stuff for head tail modes removed; SP
    pics[j].xs = xs + Ds0*pics[j].dp + xs0;  // stuff for head tail modes removed; SP
    pics[j].y = y+y0;                        // offset in vertrical space SA
    pics[j].ys = ys+ys0;
  }
  init_old_coord();
}
开发者ID:sappel,项目名称:patric_mti,代码行数:27,代码来源:Pic.cpp


示例7: gasdev

float gasdev(long *idum)
/* Returns a normally distributed variate with zero mean and
a unit variance, using ran1(idum) as the source of uniform deviates */
{
	float ran1(long *idum);
	static int iset=0;
	static float gset;
	float fac, rsq,v1,v2;

	if (*idum < 0) iset = 0;
	if (iset == 0) {
		do {
			v1 = 2.0*ran1(idum)-1.0;
			v2 = 2.0*ran1(idum)-1.0;
			rsq = v1*v1+v2*v2;
		} while (rsq >= 1.0 || rsq == 0.0);
		fac = sqrt(-2.0*log(rsq)/rsq);
		/*Now amke the Box-Muller transformation to get 
                  two normal deviates. Return one and save the other
		  for next time */
		gset = v1*fac;
		iset = 1;
		return (v2*fac);
	} else {
		iset = 0;
		return (gset);
	}
}
开发者ID:zoberg,项目名称:DNU_project,代码行数:28,代码来源:ran1.c


示例8: gasdev

/*-------------------*
 *  Function gasdev  |
 *-------------------*
 | a slight modification of crecipes version
*/
double gasdev(double m, double v)                            // m=v=u (mean poisson >30)
{
  static int iset=0;
  static float gset=0.0;
  float fac=0.0, r=0.0, v1=0.0, v2=0.0;
  double ran1();
  if(iset==0) 
    {
      do              // Compute probability r=v1^2+v2^2
        {
          v1=2.0*ran1()-1.0;
          v2=2.0*ran1()-1.0;
          r=v1*v1+v2*v2;
        } while(r>=1.0);
      fac=sqrt(-2.0*log(r)/r);
      gset=v1*fac;
      iset=1;
      return(m+sqrt(v)*v2*fac);
    } 
  else
    {
      iset=0;
      return(m+sqrt(v)*gset);
    }
}
开发者ID:celinesf,项目名称:personal,代码行数:30,代码来源:make_gametes.c


示例9: _gaussdev

void _gaussdev(float *xmv, long n)
{
  /* Returns a normally distributed deviate with zero mean and unit variance,
     using ran1() as the source of uniform deviates. */

  /*  float ran1(long *idum); */
  static int iset=0;
  static float gset;
  float fac,rsq,v1,v2;
  long i;

  for (i=0;i<n;i++) {
    if (iset == 0) {
      do {
  v1=2.0*ran1()-1.0;
  v2=2.0*ran1()-1.0;
  rsq=v1*v1+v2*v2;
      } while (rsq >= 1.0 || rsq == 0.0);
      fac=sqrt(-2.0*log(rsq)/rsq);
      gset=v1*fac;
      iset=1;
      xmv[i] = v2*fac;
    } else {
      iset=0;
      xmv[i] = gset;
    }
  }
}
开发者ID:frigaut,项目名称:yorick-imutil,代码行数:28,代码来源:imutil.c


示例10: numbers

/*---------*
 *  Pick2  |
 *---------*
 | Pick two numbers (i and j) from n
 | Called in street.c by pick2_chrom
*/
int pick2(int n, int *i, int *j)
{
  double ran1();
  *i=n*ran1();
  while((*j=n*ran1())==*i);
  return(0);
}
开发者ID:celinesf,项目名称:personal,代码行数:13,代码来源:make_gametes.c


示例11: gasdev

float gasdev(long *idum)
/************************************************************
 *	Code of this function is taken from the book			*
 *	"Neumerical Recepies in C", 1992, p289.					*
 *	Returns a normally distributed deviate with zero mean	*
 *	and unit variance, using ran1(idum) as the source of	*
 *	uniform deviates.										*
 ************************************************************/
{
	float ran1(long *idum);
	static int iset=0;
	static float gset;
	float fac, rsq, v1, v2;
	
	if (*idum <0) iset=0;
	if (iset ==0){
		do{
			v1=2.0*ran1(idum)-1.0;
			v2=2.0*ran1(idum)-1.0;
			rsq=v1*v1+v2*v2;
		} while (rsq>=1.0 || rsq ==0.0);
		fac=sqrt(-2.0*log(rsq)/rsq);
		gset=v1*fac;
		iset=1;
		return v2*fac;
	}
	else{
		iset=0;
		return gset;
	}
}
开发者ID:shashankthebest,项目名称:robocup,代码行数:31,代码来源:globals.cpp


示例12: gamdev

/*
 *----------------------------------------------------------------
 *	Gamma Distribution
 *	Returns a deviate distributed as a gamma distribution
 *	of integer order ia, i.e., a waiting time to the iath
 *	event in a Poisson process of unit mean, using ran1(idum)
 *	as the source of uniform deviates
 *	p. 292 of the book
 *----------------------------------------------------------------
 */
float gamdev(int ia, int *idum)
{
	int j;
	float am,e,s,v1,v2,x,y;
    
	
	if (ia < 1){
		printf("Error in routine GAMDEV!");
		exit ( 0 );
	}
	if (ia < 6) {
		x=1.0;
		for (j=1;j<=ia;j++) x *= ran1(idum);
		x = -log(x);
	} else {
		do {
			do {
				do {
					v1=2.0*ran1(idum)-1.0;
					v2=2.0*ran1(idum)-1.0;
				} while (v1*v1+v2*v2 > 1.0);
				y=v2/v1;
				am=ia-1;
				s=sqrt(2.0*am+1.0);
				x=s*y+am;
			} while (x <= 0.0);
			e=(1.0+y*y)*exp(am*log(x/am)-s*y);
		} while (ran1(idum) > e);
	}
	return x;
}
开发者ID:marcelo-walter,项目名称:mclone,代码行数:41,代码来源:random.cpp


示例13: ran1

// normal random variate generator using box-muller
double Random::gran(double s, double m)
{        /* mean m, standard deviation s */
  double x1, x2, w, y1;
  static double y2;
  static int use_last = 0;

  if (use_last)        /* use value from previous call */
    {
      y1 = y2;
      use_last = 0;
    }
  else
    {
      do {
	x1 = 2.0 * ran1() - 1.0;
	x2 = 2.0 * ran1() - 1.0;
	w = x1 * x1 + x2 * x2;
      } while ( w >= 1.0 );
      w = sqrt( (-2.0 * log( w ) ) / w );
      y1 = x1 * w;
      y2 = x2 * w;
      use_last = 1;
    }
  return( m + y1 * s );
}
开发者ID:ericyeung,项目名称:PHY407,代码行数:26,代码来源:random.cpp


示例14: awgn

int16_t awgn(awgn_state_t *s)
{
    double fac;
    double r;
    double v1;
    double v2;
    double amp;

    if (s->iset == 0)
    {
        do
        {
            v1 = 2.0*ran1(s) - 1.0;
            v2 = 2.0*ran1(s) - 1.0;
            r = v1*v1 + v2*v2;
        }
        while (r >= 1.0);
        fac = sqrt(-2.0*log(r)/r);
        s->gset = v1*fac;
        s->iset = 1;
        amp = v2*fac*s->rms;
    }
    else
    {
        s->iset = 0;
        amp = s->gset*s->rms;
    }
    return fsaturate(amp);
}
开发者ID:avesus,项目名称:guruchat,代码行数:29,代码来源:awgn.c


示例15: spin_matrix

/**
 * Tries to flip spin i, j. If accepted, the energy and magnetization is updated
 * 
 * @param i
 * @param j
 */
void IsingLattice2D::flipSpin(int i, int j, bool force) {
    double dE =     2*spin_matrix(i,j)*(
                    spin_matrix(i, periodic(j+1)) +
                    spin_matrix(i ,periodic(j-1)) +
                    spin_matrix(periodic(i+1), j) +
                    spin_matrix(periodic(i-1), j));
    if (dE <= 0 || force) {
        M += -2*spin_matrix(i, j);
        E += dE;
        spin_matrix(i,j) *= -1;
        accepted ++;
    }
    else {
        // Check for acceptance;
        if (dE == 4) {
            if (ran1(&idum) <= p4) {
                // accept move
                M += -2*spin_matrix(i, j);
                E += dE;
                spin_matrix(i, j) *= -1;
                accepted ++;
            } else rejected ++;
        }
        if (dE == 8) {
            if (ran1(&idum) <= p8) {
                // Accept move
                M += -2*spin_matrix(i, j);
                E += dE;
                spin_matrix(i, j) *= -1;
                accepted ++;
            } else rejected ++;
        }
    }
}
开发者ID:henriasv,项目名称:FYS3150,代码行数:40,代码来源:IsingLattice2D.cpp


示例16: flipSpin

void IsingLattice2D::Metropolis() {
    for (int n = 0; n<N*N; n++) {
        int i = (int) (ran1(&idum)*N);
        int j = (int) (ran1(&idum)*N);
        flipSpin(i, j, false);
    }
}
开发者ID:henriasv,项目名称:FYS3150,代码行数:7,代码来源:IsingLattice2D.cpp


示例17: gasdev

/*!
 * Numerical Recipes Gaussian random number generator
 */
double gasdev(long *idum)
{
  static int iset              = 0;
  static double gset;
  double fac, rsq, v1, v2;
  int i;
  double sum;
  if (0) {
  if (*idum<0)            iset = 0;
  if (iset==0) {
    do {
      v1                       = 2.0*ran1(idum)-1.0;
      v2                       = 2.0*ran1(idum)-1.0;
      rsq                      = v1*v1 + v2*v2;
    } while ((rsq>=1.0) || (rsq==0.0));
    fac                        = sqrt(-2.0*log(rsq)/rsq);
    gset                       = v1*fac;
    iset                       = 1;
    return v2*fac;
  } else {
    iset                       = 0;
    return gset;
  }
  } else {
    sum = 0;
    for (i=0; i<12; ++i) {
      sum += ran1(idum)-0.5;
    }
    return sum;
  }


}
开发者ID:DavidLBruce,项目名称:sim3dof,代码行数:36,代码来源:MathUtils.cpp


示例18: randn_internal

/* Next function is from numerical recipes in C. */
float randn_internal(long *idum, int reset) {
  static int iset=0;
  static float gset;
  float fac, rsq, v1, v2;

  if (reset) {
    iset = 0;
  }

  if (iset==0) {
    do {
      v1 = 2.0*ran1(idum, reset)-1.0;
      v2 = 2.0*ran1(idum, reset)-1.0;
      rsq = v1*v1+v2*v2;
    } while(rsq >= 1.0 || rsq == 0.0);

    fac = sqrt(-2.0*log(rsq)/rsq);
    gset = v1*fac;
    iset = 1;
    return v2*fac;
  } else {
    iset = 0;
    return gset;
  }
}
开发者ID:TueVJ,项目名称:EuropeanGridS,代码行数:26,代码来源:util.c


示例19: main

int main(void)
{
	float d,**a,**al,*b,*x;
	unsigned long i,j,*indx;
	long idum=(-1);

	a=matrix(1,7,1,4);
	x=vector(1,7);
	b=vector(1,7);
	al=matrix(1,7,1,2);
	indx=lvector(1,7);
	for (i=1;i<=7;i++) {
		x[i]=ran1(&idum);
		for (j=1;j<=4;j++) {
			a[i][j]=ran1(&idum);
		}
	}
	banmul(a,7,2,1,x,b);
	for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]);
	bandec(a,7,2,1,al,indx,&d);
	banbks(a,7,2,1,al,indx,b);
	for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]);
	free_lvector(indx,1,7);
	free_matrix(al,1,7,1,2);
	free_vector(b,1,7);
	free_vector(x,1,7);
	free_matrix(a,1,7,1,4);
	return 0;
}
开发者ID:lunalogicIntern2015,项目名称:LunaLibrary,代码行数:29,代码来源:xbandec.c


示例20: gamdev

float gamdev(int ia, long *idum)
{
	float ran1(long *idum);
	void nrerror(char error_text[]);
	int j;
	float am,e,s,v1,v2,x,y;

	if (ia < 1) nrerror("Error in routine gamdev");
	if (ia < 6) {
		x=1.0;
		for (j=1;j<=ia;j++) x *= ran1(idum);
		x = -log(x);
	} else {
		do {
			do {
				do {
					v1=ran1(idum);
					v2=2.0*ran1(idum)-1.0;
				} while (v1*v1+v2*v2 > 1.0);
				y=v2/v1;
				am=ia-1;
				s=sqrt(2.0*am+1.0);
				x=s*y+am;
			} while (x <= 0.0);
			e=(1.0+y*y)*exp(am*log(x/am)-s*y);
		} while (ran1(idum) > e);
	}
	return x;
}
开发者ID:contentsciences,项目名称:githubapitest,代码行数:29,代码来源:gamdev.c



注:本文中的ran1函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ rand函数代码示例发布时间:2022-05-30
下一篇:
C++ ralloc_strdup函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap