Ejemplo n.º 1
0
void getRandomOpenSpot(int *targeti, int *targetj, bool visible) {
	do { // Pick a random open target on the map
		*targeti = random_between(0, MAP_WIDTH-1);
		*targetj = random_between(0, MAP_HEIGHT-1);
	} while (
		map[TILE_TO_INDEX(*targeti, *targetj)] != 0 && (!visible || (
			map[TILE_TO_INDEX(*targeti, (*targetj)-1)] != 0 &&
			map[TILE_TO_INDEX((*targeti)+1, (*targetj)-1)] != 0
		)
	));
}
Ejemplo n.º 2
0
void
randomize_buffer(MYFLT *buf, int size, MYFLT lo, MYFLT hi)
{
  int i;
  for (i=0 ; i<size ; i++) {
    buf[i] = random_between(lo, hi);
  }
}
Ejemplo n.º 3
0
void board_add_tile(BoardPtr b)
{
  int index, len, value_tile;
  
  len = intlist_len(&(b->pos_free)); 
  index = random_between(0, len);

  Tile *tile;
  tile = malloc(sizeof(Tile));

  value_tile = random_between(0, 11);
  if (value_tile == 0) {
    *tile = tile_make(4);
  }
  else
    *tile = tile_make(2);

  board_set(b, tile, index);
  board_update_freepos(b);
}
Ejemplo n.º 4
0
void flush_cache()
{
    int size = 20*1024*1024;
    char *buf = (char *) malloc(size);

    int i;
    for(i=0; i < size; i++)
        buf[i] = random_between(0, 0xFF);

    free(buf);
}
Ejemplo n.º 5
0
bool rate_by_thousand(uint32 rate)
{
    return random_between(1, 1000) <= rate;
}
Ejemplo n.º 6
0
bool rate_by_percent(uint32 rate)
{
    return random_between(1, 100) <= rate;
}
Ejemplo n.º 7
0
int main(int argc, char ** argv){
  int *lattice;
  int neigh[4];
  int n_side;
  int i_flip=0;
  int i,j;
  double r;
  double delta_E;
  double T = atof(argv[2]);
  double beta = 1.0/T;
  double E;
  double M;
  double alpha;
  double h;
  int n_steps;

  double average_m = 0.0;
  double average_E = 0.0;
  double average_E2 = 0.0;

  n_side = atoi(argv[1]);
  lattice = init_lattice(n_side);
  E = compute_energy(lattice, n_side);
  M = compute_magnetiza(lattice, n_side);
  /*printf("%f %f\n", E, M);*/
  n_steps = n_side * n_side * 10;
  
  for(i=0;i<n_steps;i++){
    i_flip = random_between(0,n_side*n_side-1);
    get_neighbors(lattice, n_side, i_flip, neigh);
    
    h = 0.0;
    for(j=0;j<4;j++){
      h += lattice[neigh[j]];
    }
    delta_E = 2.0 * h * lattice[i_flip];

    r = MIN(1.0, exp(-beta * delta_E));
    alpha = drand48();
    if(alpha < r){
      lattice[i_flip] = -lattice[i_flip];
      E = E + delta_E;
    }    
    M = compute_magnetiza(lattice, n_side);
    /*printf("%f %f\n", E, M);*/

    if(i>30000){
      average_m += fabs(M)/(n_side*n_side);
      average_E += E;
      average_E2 += E*E;
    }
  }
  
  average_m = average_m/(n_steps-30000);
  average_E = average_E/(n_steps-30000);
  average_E2 = average_E2/(n_steps-30000);
  
  double varE = average_E2 - average_E*average_E;
  
  printf("%f %f %f\n", T, average_m, varE);
  
  return 0;
}