Exemple #1
0
int random_vector(unsigned int seed, int code, DVEC(r)) {
    struct random_data buffer;
    char   random_state[128];
    memset(&buffer, 0, sizeof(struct random_data));
    memset(random_state, 0, sizeof(random_state));

    initstate_r(seed,random_state,sizeof(random_state),&buffer);
    // setstate_r(random_state,&buffer);
    // srandom_r(seed,&buffer);

    int phase = 0;
    double V1,V2,S;

    int k;
    switch (code) {
      case 0: { // uniform
        for (k=0; k<rn; k++) {
            rp[k] = urandom(&buffer);
        }
        OK
      }
      case 1: { // gaussian
        for (k=0; k<rn; k++) {
            rp[k] = gaussrand(&buffer,&phase,&V1,&V2,&S);
        }
        OK
      }

      default: ERROR(BAD_CODE);
    }
}
Exemple #2
0
/*
 * Lo mismo que la funcion anterior pero ahora de derecha a izquierda
 */
void trayectoriaRL(float t_0, float t_f, float y0, float * buff, int n,float theta, float mu, float sigma){
	float h = (t_f - t_0)/n;
	float sqrtdt = sqrt(h);
	buff[n-1] = y0;
	int i;	
	for(i = n-2; i>=0 ; i--){
		buff[i] = buff[i+1] + h*(theta*(mu-buff[i+1])) + sigma*sqrtdt*gaussrand();
	}
}
Exemple #3
0
/*
 * Funcion que genera trayectorias de izquierda a derecha para la creacion de puentes
 * Recibe como parametros, un tiempo inicial y final, un valor inicial y0,
 * un buffer y su longitud, asi como theta, mu y sigma que determinan a la ecuacion 
 * dXt = theta * (mu - Xt) * dt + sigma * dWt
 * donde dWt es la raiz cuadrada del salto multiplicado por un numero generado por box muller
 */
void trayectoriaLR(float t_0, float t_f, float y0, float * buff, int n,float theta, float mu, float sigma){
	float h = (t_f - t_0)/n;
	float sqrtdt = sqrt(h);
	buff[0] = y0;
	int i;	
	
	for(i = 1; i<n ; i++){
		buff[i] = buff[i-1] + h*(theta*(mu-buff[i-1])) + sigma*sqrtdt*gaussrand();
	}
}
Exemple #4
0
/*---------------------------------------------------------------------------*/
float *fmat_new_rand_gauss (int nrow, int ncol)
{
  long i;
  float *m = fmat_new (nrow, ncol);

  for (i = 0; i < nrow * ncol; i++)
    m[i] = gaussrand ();

  return m;
}
Exemple #5
0
Entity newTextParticleEntity(char * str, u32 color, int x, int y, int level){
    Entity e;
    e.type = ENTITY_TEXTPARTICLE;
    e.level = level;
    e.textParticle.color = color;
    e.textParticle.age = 0;
    e.textParticle.text = (char*)calloc(strlen(str),sizeof(char)); 
    strncpy(e.textParticle.text,str,strlen(str)); 
    e.x = x;
    e.y = y;
    e.canPass = true;
	e.textParticle.xx = x;
	e.textParticle.yy = y;
	e.textParticle.zz = 2;
	e.textParticle.xa = gaussrand() * 0.3;
	e.textParticle.ya = gaussrand() * 0.2;
	e.textParticle.za = ((float)rand() / RAND_MAX) * 0.7 + 2;
    
    return e;
}
Exemple #6
0
 Tensor(int n, int c, int h, int w, float std = 0) : data(new float[n * c * h * w]){
     N = n;
     C = c;
     H = h;
     W = w;
     if(std == 0) {
         memset(data.get(), 0, sizeof(float) * N * C * H * W);
     } else {
         gaussrand(0, std, data.get(), N*C*H*W);
     }
 }
Exemple #7
0
Entity newItemEntity(Item item, int x, int y, int level){
    Entity e;
    e.type = ENTITY_ITEM;
    e.level = level;
    e.entityItem.age = 0;
    e.entityItem.item = item;
    e.x = x;
    e.y = y;
    e.xr = 3;
    e.yr = 3;
    e.canPass = false;
    
	e.entityItem.xx = x;
	e.entityItem.yy = y;
	e.entityItem.zz = 2;
	e.entityItem.xa = gaussrand() * 0.1;
	e.entityItem.ya = gaussrand() * 0.1;
	e.entityItem.za = ((float)rand() / RAND_MAX) * 0.45 + 1;
    
    return e;
}
Exemple #8
0
int Plasma::init_vel(int ndistr=ZEROVEL){
  int i,j;
  int distr;
  double delta,T0;
  for(i=0;i<n;i++){

   if(ndistr==SEPARATE){
    if(i<ni){
     distr=idistr;
     T0=ini_Ti/(rel_Ti ? 1: par_T);
    }
    else{
     distr=edistr;
     T0=ini_Te/(rel_Te ? 1: par_T);
    }
   }
   else{
     distr=ndistr;
     T0=1.;
   }

   for(j=0;j<3;j++){
    if((i<ni && stable_ions) || distr==ZEROVEL)v[i][j]=0;
    else if(distr==MAXWELL_P){
     if(i<ni)delta=T0/mass;
     else delta=T0;
     maxwellrand(v[i].v,delta);
    }
    else if(distr==MAXWELL){
     if(i<ni)delta=2.*T0/mass;
     else delta=2.*T0;
     v[i][j]=gaussrand(10*sqrt(delta),delta);
    }
    else{
      msg_error("Plasma.init_vel : Unknown distribution.\n");
      return 0;
    }
   }
 }

 if(ndistr==SEPARATE){
  if(idistr==MAXWELL && edistr==MAXWELL){
   vel_scale(ini_Ti/(rel_Ti ? 1: par_T),ini_Te/(rel_Te ? 1: par_T)); // simultaneous scale ( better )
  }
  else{
   if(idistr==MAXWELL)ivel_scale(ini_Ti/(rel_Ti ? 1: par_T));
   if(edistr==MAXWELL)evel_scale(ini_Te/(rel_Te ? 1: par_T));
  }
 }
 else if(ndistr==MAXWELL)vel_scale(1.);

 return 1;
}
int main (void) 
{
	double x;
	double y;
	double z;
	double norm;
	int num = 1000;
	int i = 1;

	for( ; i <= num; i++)
	{
		x = gaussrand();
		y = gaussrand();
		z = gaussrand();
		norm = sqrt(x*x+y*y+z*z);
		x = x/norm;
		y = y/norm;
		z = z/norm;
		printf("%f, %f, %f\n", x, y, z);
	}
}
Exemple #10
0
int sp_gen_gauss(sp_data *sp, sp_ftbl *ft, SPFLOAT scale, uint32_t seed)
{
    int n;

    sp_randmt rand;

    sp_randmt_seed(&rand, NULL, seed);

    for(n = 0; n < ft->size; n++) {
        ft->tbl[n] = gaussrand(&rand, scale);
    }

    return SP_OK;
}
Exemple #11
0
/**
 * @brief Give a random destination to the given UFO close to a position, and make him to move there.
 * @param[in] ufocraft Pointer to the UFO which destination will be changed.
 * @param[in] pos The position the UFO should around.
 * @sa UFO_SetRandomPos
 */
void UFO_SetRandomDestAround (aircraft_t* ufocraft, const vec2_t pos)
{
	vec2_t dest;
	const float spread = 2.0f;
	float rand1, rand2;

	gaussrand(&rand1, &rand2);
	rand1 *= spread;
	rand2 *= spread;

	Vector2Set(dest, pos[0] + rand1, pos[1] + rand2);

	UFO_SendToDestination(ufocraft, dest);
}
Exemple #12
0
void generateData(float **data, int nframe, int samplesperframe, int nchan,
		  int iscomplex, int bandwidth, float tone, float *mean, float *stdDev) {
  int i, n;
  float s;

  for (i=0; i<nframe*samplesperframe; i++) {
    s = sin(tone/bandwidth*i*M_PI)*0.1; // There will be a phase jump from buffer to buffer
    for (n=0; n<nchan; n++) {
      data[n][i] = gaussrand()+s;
    }
  }

  // Calculate RMS to allow thresholding
  *mean = 0;
  *stdDev = 0;
  for (n=0; n<nchan; n++) {
    float thismean, thisStdDev;
    MeanStdDev(data[n], nframe*samplesperframe, &thismean, &thisStdDev);
    *mean += thismean;
    *stdDev += thisStdDev;
  }
  *mean /= nchan;
  *stdDev /= nchan;
}
Exemple #13
0
int random_vector(unsigned int seed, int code, DVEC(r)) {
    int phase = 0;
    double V1,V2,S;

    srandom(seed);

    int k;
    switch (code) {
      case 0: { // uniform
        for (k=0; k<rn; k++) {
            rp[k] = urandom();
        }
        OK
      }
      case 1: { // gaussian
        for (k=0; k<rn; k++) {
            rp[k] = gaussrand(&phase,&V1,&V2,&S);
        }
        OK
      }

      default: ERROR(BAD_CODE);
    }
}
Exemple #14
0
void fvec_randn (float * v, long n)
{
  long i;
  for (i = 0 ; i < n ; i++)
    v[i] = gaussrand();
}
/*
 * Replace or place a tile.
 */
widp
wid_game_map_client_replace_tile (widp w, 
                                  double x, double y, 
                                  thingp t,
                                  tpp tp)
{
    tree_rootp thing_tiles;
    const char *tilename;
    tilep tile;
    widp child;

    verify(w);

    /*
     * Grow tl and br to fit the template thing. Use the first tile.
     */
    if (!tp) {
        tp = thing_tp(t);
        if (!tp) {
            ERR("no thing template to replace on client");
            return (0);
        }
    }

    if ((x < 0) || (y < 0) || (x >= MAP_WIDTH) || (y >= MAP_WIDTH)) {
        LOG("client: thing template [%s] cannot be placed at %f %f",
            tp_short_name(tp), x, y);
        return (0);
    }

    thing_tiles = tp_get_tiles(tp);
    if (!thing_tiles) {
        ERR("thing template [%s] has no tiles", tp_short_name(tp));
        return (0);
    }

    thing_tilep thing_tile;

    /*
     * Get a random tile to start with.
     */
    thing_tile = (typeof(thing_tile)) thing_tile_random(thing_tiles);

    /*
     * Find the real tile that corresponds to this name.
     */
    tilename = thing_tile_name(thing_tile);
    tile = tile_find(tilename);

    if (!tile) {
        ERR("tile name %s from thing %s not found on client",
            tilename,
            tp_short_name(tp));
        return (0);
    }

    /*
     * Make a new thing.
     */
    child = wid_new_square_button(wid_game_map_client_grid_container,
                                  "client map tile");

    wid_set_mode(child, WID_MODE_NORMAL);
    wid_set_no_shape(child);

    /*
     * "paint" the thing.
     */
    wid_game_map_client_set_thing_template(child, tp);

    if (!t) {
        t = thing_client_local_new(tp);
    }

    wid_set_thing(child, t);
    wid_set_tile(child, tile);

    double dx = 0;
    double dy = 0;

    /*
     * Does it appear as a different size on screen?
     */
    double scale = tp_get_scale(tp);

    /*
     * So we have baby and bigger slimes. But alas this is visual only and has 
     * no effect on hp on the server yet.
     */
    if (thing_is_variable_size(t)) {
        scale += gaussrand(0.0, 0.05);
    }

    if (scale != 1.0) {
        wid_scaling_blit_to_pct_in(child, scale, scale, 500, 9999999);
    }

    if (thing_is_cloud_effect(t)) {
        /*
         * The epicenter needs to be where it was on the server as we do a 
         * flood fill to see where the rest of the explosion goes.
         */
        if (!t->is_epicenter) {
            dx = gaussrand(0.0, 0.5);
            dy = gaussrand(0.0, 0.5);
        }

        wid_fade_out(child, 1000);
    }

    thing_client_wid_update(t, x + dx, y + dy, false /* smooth */,
                            true /* is new */);

    /*
     * Offset tall things
     */
    if (scale != 1.0) {
        if (thing_is_blit_y_offset(t)) {
            wid_set_blit_y_offset(child, 
                                  wid_get_height(child) * scale * -((scale - 1.0) / 2.0));
        }
    }

    /*
     * If this is a pre-existing thing perhaps being recreated ona new level
     * then it will have a direction already. Update it.
     */
    if (thing_is_animated(t)) {
        thing_animate(t);
    }

    /*
     * This adds it to the grid wid.
     */
#ifdef DEBUG_CLIENT_THING
    wid_update(child);
    char name[20];
    sprintf(name, "%d",t->thing_id);
    wid_set_text(child,name);
#endif

    /*
     * We've been told about the epicenter of an explsion, now emulate the 
     * blast.
     */
    if (t->is_epicenter && thing_is_cloud_effect(t) ) {

        if ((tp->id == THING_EXPLOSION1)        ||
            (tp->id == THING_EXPLOSION2)        ||
            (tp->id == THING_EXPLOSION3)        ||
            (tp->id == THING_EXPLOSION4)        ||
            (tp->id == THING_SMALL_EXPLOSION1)  ||
            (tp->id == THING_SMALL_EXPLOSION2)  ||
            (tp->id == THING_SMALL_EXPLOSION3)  ||
            (tp->id == THING_SMALL_EXPLOSION4)  ||
            (tp->id == THING_MED_EXPLOSION1)    ||
            (tp->id == THING_MED_EXPLOSION2)    ||
            (tp->id == THING_MED_EXPLOSION3)    ||
            (tp->id == THING_MED_EXPLOSION4)    ||
            (tp->id == THING_FIREBURST1)        ||
            (tp->id == THING_FIREBURST2)        ||
            (tp->id == THING_FIREBURST3)        ||
            (tp->id == THING_FIREBURST4)        ||
            (tp->id == THING_BOMB)              ||
            (tp->id == THING_POISON1)           ||
            (tp->id == THING_POISON2)           ||
            (tp->id == THING_CLOUDKILL1)        ||
            (tp->id == THING_CLOUDKILL2)) {

            level_place_explosion(client_level,
                                  0, /* owner */
                                  tp,
                                  t->x, t->y,
                                  t->x, t->y);
        } else {
            ERR("unknown explosion %s", thing_logname(t));
        }
    }

    const char *sound = tp_sound_on_creation(tp);
    if (sound) {
        if (thing_is_cloud_effect(t)) {
            if (t->is_epicenter) {
                sound_play_at(sound, t->x, t->y);
            }
        } else {
            sound_play_at(sound, t->x, t->y);
        }
    }

    return (child);
}
Exemple #16
0
void Particle::sense(Maze const &maze, int seed)
{
	srand(seed);
	for (int i = 0; i < NUM_MEASUREMENTS; i++)
	{
        measurements[i] = getDistanceToClosestWall(maze, sensors[i][0], sensors[i][1], sensors[i][2]) + gaussrand()*SENSE_NOISE;
	}
}