当前位置: 首页>>代码示例>>C++>>正文


C++ ran1函数代码示例

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


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


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