Exemplo n.º 1
0
void
generate_drug ()
{
    GRand *rand;
    gint i, j, divisor;

    rand = g_rand_new ();

    for (i = 0; i < DRUG_NUM; i++)
        for (j = 0; j < CITY_NUM; j++)
        {
            drug_table[i][j].available = g_rand_boolean (rand);
            if (drug_table[i][j].available)
            {
                drug_table[i][j].qty = g_rand_int_range (rand, 0, 10);
                if (drug_table[i][j].qty > 0)
                    divisor = drug_table[i][j].qty;
                else
                    divisor = g_rand_int_range (rand, 1, 10);
                drug_table[i][j].price = ((double) 10 / divisor) *
                                         g_rand_double_range (rand, 0.1, 1.0) *
                                         drug_price[i];
            }
        }

    g_rand_free (rand);
}
Exemplo n.º 2
0
static void
modify_info (ExpInfo *o_info,
             ExpInfo *n_info)
{
  gint k, n;

  *n_info = *o_info;
  n = g_rand_int_range (gr, 0, MAX_TRANSFORMS);
  for (k = 0; k < n; k++)
    {
      switch (g_rand_int_range (gr, 0, 4))
        {
        case 0:
          n_info->transformSequence[g_rand_int_range (gr, 0, MAX_TRANSFORMS)] =
            g_rand_int_range (gr, 0, NUM_TRANSFORMS);
          break;

        case 1:
          n_info->source[g_rand_int_range (gr, 0, MAX_TRANSFORMS)] =
            g_rand_int_range (gr, 0, NUM_REGISTERS);
          break;

        case 2:
          n_info->control[g_rand_int_range (gr, 0, MAX_TRANSFORMS)] =
            g_rand_int_range (gr, 0, NUM_REGISTERS);
          break;

        case 3:
          n_info->dest[g_rand_int_range (gr, 0, MAX_TRANSFORMS)] =
            g_rand_int_range (gr, 0, NUM_REGISTERS);
          break;
        }
    }
}
Exemplo n.º 3
0
Arquivo: spread.c Projeto: Minoos/gimp
static void
spread_func (gint      x,
             gint      y,
             guchar   *dest,
             gint      bpp,
             gpointer  data)
{
  SpreadParam_t *param = (SpreadParam_t*) data;
  gdouble        angle;
  gint           xdist, ydist;
  gint           xi, yi;

  /* get random angle, x distance, and y distance */
  xdist = (param->x_amount > 0
           ? g_rand_int_range (param->gr, -param->x_amount, param->x_amount)
           : 0);
  ydist = (param->y_amount > 0
           ? g_rand_int_range (param->gr, -param->y_amount, param->y_amount)
           : 0);
  angle = g_rand_double_range (param->gr, -G_PI, G_PI);

  xi = x + floor (sin (angle) * xdist);
  yi = y + floor (cos (angle) * ydist);

  /* Only displace the pixel if it's within the bounds of the image. */
  if (xi >= 0 && xi < param->width && yi >= 0 && yi < param->height)
    {
      gimp_pixel_fetcher_get_pixel (param->pft, xi, yi, dest);
    }
  else /* Else just copy it */
    {
      gimp_pixel_fetcher_get_pixel (param->pft, x, y, dest);
    }
}
Exemplo n.º 4
0
void
test_field_volume_voids_planar(void)
{
    enum { max_size = 66 };
    GRand *rng = g_rand_new_with_seed(42);
    gsize niter = 100;

    for (guint iter = 0; iter < niter; iter++) {
        guint xres = g_rand_int_range(rng, 3, max_size);
        guint yres = g_rand_int_range(rng, 3, max_size);
        gdouble alpha = g_rand_double_range(rng, -5.0, 5.0);
        gdouble beta = g_rand_double_range(rng, -5.0, 5.0);
        gdouble base = g_rand_double_range(rng, -5.0, 5.0);
        GwyField *field = field_make_planar(xres, yres, alpha, beta);
        GwyFieldPart fpart = { 1, 1, xres-2, yres-2 };
        gdouble volume, volume_expected;
        gwy_field_set_xreal(field, xres/sqrt(xres*yres));
        gwy_field_set_yreal(field, yres/sqrt(xres*yres));
        volume = gwy_field_material_volume(field, &fpart,
                                           NULL, GWY_MASK_IGNORE, FALSE, base);
        volume_expected = planar_field_material_volume(field, base, FALSE);
        gwy_assert_floatval(volume, volume_expected, 1e-9*fabs(volume_expected));

        gwy_field_set_xreal(field, 1.0);
        gwy_field_set_yreal(field, 1.0);
        volume_expected = planar_field_material_volume(field, base, FALSE);
        volume = gwy_field_material_volume(field, &fpart,
                                           NULL, GWY_MASK_IGNORE, FALSE, base);
        gwy_assert_floatval(volume, volume_expected, 1e-9*fabs(volume_expected));

        g_object_unref(field);
    }
    g_rand_free(rng);
}
Exemplo n.º 5
0
static guint32
fuzz_packet (guint8 *buf, guint32 len, guint32 stream_pos)
{
  guint32 i;
  guint n_changes;
#define TCP_HEADER_LENGTH 32 /* bytes; or thereabouts (include some options) */

  /* Do we want to fuzz this packet? */
  if (stream_pos < fuzz_start_pos) {
    return len;
  }

  /* Get fuzzing. Only bother fuzzing the header; fuzzing the payload is
   * pointless. Weight the number of changes towards having only a few changes,
   * since that makes them less likely to be summarily rejected. */
  n_changes = random_int_poisson (n_changes_lambda);
  g_debug ("Making %u changes for bytes at stream position %u:",
      n_changes, stream_pos);

  for (i = 0; i < n_changes; i++) {
    guint32 pos = g_rand_int_range (prng, 0, MIN (len, TCP_HEADER_LENGTH));
    g_debug (" • Changing byte %u.", stream_pos + pos);
    buf[pos] = g_rand_int_range (prng, 0, G_MAXUINT8 + 1);
  }

  return len;
}
Exemplo n.º 6
0
void
test_field_surface_area_planar(void)
{
    enum { max_size = 76 };
    GRand *rng = g_rand_new_with_seed(42);
    gsize niter = 50;

    for (guint iter = 0; iter < niter; iter++) {
        guint xres = g_rand_int_range(rng, 1, max_size);
        guint yres = g_rand_int_range(rng, 1, max_size);
        gdouble alpha = g_rand_double_range(rng, -5.0, 5.0);
        gdouble beta = g_rand_double_range(rng, -5.0, 5.0);
        GwyField *field = field_make_planar(xres, yres, alpha, beta);
        gdouble area, area_expected;
        gwy_field_set_xreal(field, xres/sqrt(xres*yres));
        gwy_field_set_yreal(field, yres/sqrt(xres*yres));
        area = gwy_field_surface_area(field, NULL, NULL, GWY_MASK_IGNORE);
        area_expected = planar_field_surface_area(field);
        g_assert_cmpfloat(fabs(area - area_expected)/area_expected, <=, 1e-9);

        gwy_field_set_xreal(field, 1.0);
        gwy_field_set_yreal(field, 1.0);
        area = gwy_field_surface_area(field, NULL, NULL, GWY_MASK_IGNORE);
        area_expected = planar_field_surface_area(field);
        g_assert_cmpfloat(fabs(area - area_expected)/area_expected, <=, 1e-9);

        g_object_unref(field);
    }
    g_rand_free(rng);
}
Exemplo n.º 7
0
static void
int_set_randomize(GwyIntSet *intset,
                  GRand *rng)
{
    guint size = g_rand_int_range(rng, 0, 20) + g_rand_int_range(rng, 0, 10);

    for (guint i = 0; i < size; i++)
        gwy_int_set_add(intset, random_integer(rng));
}
Exemplo n.º 8
0
static void
_gstr_randomize(GRand *r, GString *gstr)
{
	gint i, max;

	g_string_set_size(gstr, 0);
	max = g_rand_int_range(r, 1, 15);
	for (i=0; i<max ; i++) {
		guint32 u32 = g_rand_int_range(r, 0, sizeof(random_chars)-1);
		g_string_append_c(gstr, random_chars[u32]);
	}
}
Exemplo n.º 9
0
static void
create_info (ExpInfo *info)
{
  gint k;

  for (k = 0; k < MAX_TRANSFORMS; k++)
    {
      info->transformSequence[k] = g_rand_int_range (gr, 0, NUM_TRANSFORMS);
      info->source[k] = g_rand_int_range (gr, 0, NUM_REGISTERS);
      info->control[k] = g_rand_int_range (gr, 0, NUM_REGISTERS);
      info->dest[k] = g_rand_int_range (gr, 0, NUM_REGISTERS);
    }
}
Exemplo n.º 10
0
void dp_deep_step_func (gpointer data, gpointer user_data)
{
	int r1, r2, r3, r4;
	int start_index, end_index;
	DpIndivid*my_tabu;
	DpDeepInfo*hdeepinfo = (DpDeepInfo*)user_data;
	int my_id = GPOINTER_TO_INT(data) - 1;
	DpPopulation*trial = hdeepinfo->trial;
	DpIndivid*my_trial = trial->individ[my_id];
	DpPopulation*population = hdeepinfo->population;
	DpIndivid*my_individ = population->individ[my_id];
	DpRecombinationControl *recombination_control = hdeepinfo->recombination_control;
	int ignore_cost = hdeepinfo->hevalctrl->eval_target->ignore_cost;
	GRand*hrand = my_individ->hrand;
	r1 = ( ignore_cost == 0 ) ? population->imin : -1;
	do {
		r2 = g_rand_int_range (hrand, 0, hdeepinfo->population_size);
	} while ( r2 == my_id || r2 == r1 );
	do {
		r3 = g_rand_int_range (hrand, 0, hdeepinfo->population_size);
	} while ( r3 == my_id || r3 == r1 || r3 == r2 );
	do {
		r4 = g_rand_int_range (hrand, 0, hdeepinfo->population_size);
	} while ( r4 == my_id || r4 == r1 || r4 == r2 || r4 == r3 );
	r1 = population->imin;
	my_tabu = population->individ[r1];
	start_index = g_rand_int_range (hrand, 0, population->ind_size);
	end_index = population->ind_size;
	dp_individ_copy_values(my_trial, my_individ);
	my_trial->age = 0;
	my_trial->r1 = r1;
	my_trial->r2 = r2;
	my_trial->r3 = r3;
	my_trial->r4 = r4;
	my_trial->moves = 0;
	my_trial->failures = 0;
	my_trial->grads = 0;
	dp_individ_recombination(recombination_control, hrand, my_trial, population->individ[r1], population->individ[r2], population->individ[r3], population->individ[r4], start_index, end_index);
	dp_evaluation_individ_evaluate(hdeepinfo->hevalctrl, my_trial, my_tabu, my_id, my_tabu->cost);
	if ( ignore_cost == 0 && my_id == population->imin ) {
		if ( my_trial->cost >= my_individ->cost ) {
			dp_individ_copy_values(my_trial, my_individ);
			my_trial->age++;
		}
	} else if ( 1 != dp_evaluation_individ_compare((const void *)(&my_individ), (const void *)(&my_trial), (void*)(hdeepinfo->hevalctrl)) ) {
		dp_individ_copy_values(my_trial, my_individ);
		my_trial->age++;
	}
}
Exemplo n.º 11
0
/**
 * @brief Randomizes the order of the hosts objects in the collection.
 * Not to be used while iterating over the single hosts as it resets the
 * iterator.
 *
 * @param[in] hosts The hosts collection to shuffle.
 */
void
openvas_hosts_shuffle (openvas_hosts_t *hosts)
{
  int count;
  GList *new_list;
  GRand *rand;

  if (hosts == NULL)
    return;

  count = openvas_hosts_count (hosts);
  new_list = NULL;

  rand = g_rand_new ();

  while (count)
    {
      GList *element;

      /* Get element from random position [0, count[. */
      element = g_list_nth (hosts->hosts, g_rand_int_range (rand, 0, count));
      /* Remove it. */
      hosts->hosts = g_list_remove_link (hosts->hosts, element);
      /* Insert it in new list */
      new_list = g_list_concat (element, new_list);
      count--;
    }
  hosts->hosts = new_list;
  hosts->current = hosts->hosts;

  g_rand_free (rand);
}
Exemplo n.º 12
0
static GstFlowReturn
gst_break_my_data_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
{
    GstBreakMyData *bmd = GST_BREAK_MY_DATA (trans);
    guint i, size;

    g_return_val_if_fail (gst_buffer_is_writable (buf), GST_FLOW_ERROR);

    GST_OBJECT_LOCK (bmd);

    if (bmd->skipped < bmd->skip) {
        i = bmd->skip - bmd->skipped;
    } else {
        i = 0;
    }

    size = GST_BUFFER_SIZE (buf);

    GST_LOG_OBJECT (bmd,
                    "got buffer %p (size %u, timestamp %" G_GUINT64_FORMAT ", offset %"
                    G_GUINT64_FORMAT "", buf, size, GST_BUFFER_TIMESTAMP (buf),
                    GST_BUFFER_OFFSET (buf));

    for (; i < size; i++) {
        if (g_rand_double_range (bmd->rand, 0, 1.0) <= bmd->probability) {
            guint8 new;

            if (bmd->set < 0) {
                new = g_rand_int_range (bmd->rand, 0, 256);
            } else {
                new = bmd->set;
Exemplo n.º 13
0
static void
about_dialog_reshuffle (GimpAboutDialog *dialog)
{
  GRand *gr = g_rand_new ();
  gint   i;

  for (i = 0; i < dialog->n_authors; i++)
    dialog->shuffle[i] = i;

  for (i = START_INDEX; i < dialog->n_authors; i++)
    {
      gint j = g_rand_int_range (gr, START_INDEX, dialog->n_authors);

      if (i != j)
        {
          gint t;

          t = dialog->shuffle[j];
          dialog->shuffle[j] = dialog->shuffle[i];
          dialog->shuffle[i] = t;
        }
    }

  g_rand_free (gr);
}
Exemplo n.º 14
0
static int semi_uniform (RLearner *rl, const unsigned int state)
{
	double rand_value = 0.0; 
	int    action     = 0;
	
    if (rl == NULL)
    {
        fprintf(stderr, "RLearner-WARNING **: RLearner *rl is NULL\n");
    }
    else
    {
        rand_value = g_rand_double(rl->random);
        
        if (rand_value > (1.0 - EXPLORATION_THRESHOLD))
        {
            /* Random action */
            action = g_rand_int_range(rl->random, 0, rl->num_actions);
        }
        else
        {
            action = best_action(rl, state);
        }
    }
    
    return action;
} 
Exemplo n.º 15
0
int	main(int argc, char *argv[])
{
	GRand *rand;
	GTimer *timer;
	
	gint n;
	gint i, j;
	gint x = 0;
	rand = g_rand_new();	//创建随机数对象
	for(n=0; n<20; n++)
	{	//产生随机数并显示出来
		g_print("%d\t",g_rand_int_range(rand,1,100));
	}
	g_print("\n");
	g_rand_free(rand);	//释放随机数对象
	//创建计时器
	timer = g_timer_new();
	g_timer_start(timer);//开始计时
	for(i=0; i<10000; i++)
		for(j=0; j<3000; j++)
			x++;//累计
	g_timer_stop(timer);//计时结束
	//输出计时结果
	g_print("%ld\tall:%.2f seconds was used!\n",x,g_timer_elapsed(timer,NULL));
}
Exemplo n.º 16
0
static gboolean
gimp_operation_dissolve_mode_process (GeglOperation       *operation,
                                      void                *in_buf,
                                      void                *aux_buf,
                                      void                *aux2_buf,
                                      void                *out_buf,
                                      glong                samples,
                                      const GeglRectangle *result,
                                      gint                 level)
{
  gdouble         opacity  = GIMP_OPERATION_POINT_LAYER_MODE (operation)->opacity;
  gfloat         *in       = in_buf;
  gfloat         *out      = out_buf;
  gfloat         *aux      = aux_buf;
  gfloat         *mask     = aux2_buf;
  const gboolean  has_mask = mask != NULL;
  gint            x, y;

  for (y = result->y; y < result->y + result->height; y++)
    {
      GRand *gr = g_rand_new_with_seed (random_table[y % RANDOM_TABLE_SIZE]);

      /* fast forward through the rows pseudo random sequence */
      for (x = 0; x < result->x; x++)
        g_rand_int (gr);

      for (x = result->x; x < result->x + result->width; x++)
        {
          gfloat value = aux[ALPHA] * opacity * 255;

          if (has_mask)
            value *= *mask;

          if (g_rand_int_range (gr, 0, 255) >= value)
            {
              out[0] = in[0];
              out[1] = in[1];
              out[2] = in[2];
              out[3] = in[3];
            }
          else
            {
              out[0] = aux[0];
              out[1] = aux[1];
              out[2] = aux[2];
              out[3] = 1.0;
            }

          in   += 4;
          out  += 4;
          aux  += 4;

          if (has_mask)
            mask ++;
        }
      g_rand_free (gr);
    }

  return TRUE;
}
Exemplo n.º 17
0
static void
about_dialog_reshuffle (GimpAboutDialog *dialog)
{
  GRand *gr = g_rand_new ();
  gint   i;

  for (i = 0; i < dialog->n_authors; i++)
    dialog->shuffle[i] = i;

  /* here we rely on the authors array having Peter and Spencer first */
#define START_INDEX 2

  for (i = START_INDEX; i < dialog->n_authors; i++)
    {
      gint j = g_rand_int_range (gr, START_INDEX, dialog->n_authors);

      if (i != j)
        {
          gint t;

          t = dialog->shuffle[j];
          dialog->shuffle[j] = dialog->shuffle[i];
          dialog->shuffle[i] = t;
        }
    }

#undef START_INDEX

  g_rand_free (gr);
}
Exemplo n.º 18
0
static gboolean set_new_position(gpointer pipeline)
{
    gint64 seek_pos = 0;
    GRand* rand_generator = g_rand_new();

    g_debug("Starting seek :-)");
    if(counter >= SEEK_COUNT){
        g_rand_free (rand_generator);
        return FALSE;
    }
    counter++;
    if(volta_inicio){
        seek_pos = 0;
        volta_inicio = FALSE;
    }else{
        seek_pos = GST_SECOND * g_rand_int_range(rand_generator, 1, 30);
        volta_inicio = TRUE;
    }
     
    if(!gst_element_seek_simple(pipeline, GST_FORMAT_TIME, 
                                    GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 
                                    seek_pos)){
        g_debug("Error seeking!");
        g_rand_free (rand_generator);
        return TRUE;
    }
    
    g_debug("Seeked with success :-), position set is[%lld]", seek_pos / GST_SECOND);
    g_rand_free (rand_generator);
    return TRUE;
}
Exemplo n.º 19
0
void
test_surface_set(void)
{
    enum { max_size = 255, niter = 40 };

    GRand *rng = g_rand_new_with_seed(42);

    for (guint iter = 0; iter < niter; iter++) {
        guint res = g_rand_int_range(rng, 1, max_size);
        GwySurface *surface = gwy_surface_new_sized(res);

        for (guint i = 0; i < res; i++) {
            GwyXYZ pt = { i, G_PI/(i + 1), -0.5*i*i };
            gwy_surface_set(surface, i, pt);
        }

        for (guint k = 0; k < res; k++) {
            GwyXYZ pt = gwy_surface_get(surface, k);
            g_assert_cmpfloat(pt.x, ==, k);
            g_assert_cmpfloat(pt.y, ==, G_PI/(k + 1));
            g_assert_cmpfloat(pt.z, ==, -0.5*k*k);
        }
        g_object_unref(surface);
    }

    g_rand_free(rng);
}
Exemplo n.º 20
0
gchar *
fb_util_rand_alnum(guint len)
{
    gchar *ret;
    GRand *rand;
    guint i;
    guint j;

    static const gchar chars[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz"
        "0123456789";
    static const gsize charc = G_N_ELEMENTS(chars) - 1;

    g_return_val_if_fail(len > 0, NULL);
    rand = g_rand_new();
    ret = g_new(gchar, len + 1);

    for (i = 0; i < len; i++) {
        j = g_rand_int_range(rand, 0, charc);
        ret[i] = chars[j];
    }

    ret[len] = 0;
    g_rand_free(rand);
    return ret;
}
Exemplo n.º 21
0
/**
 * crank_bench_run_rand_int_range: (skip)
 * @run: A benchmark run.
 * @begin: Begin of range.
 * @end: End of range.
 *
 * Returns random integer in the range: [@begin, @end).
 *
 * Returns: A random integer in the range.
 */
gint32
crank_bench_run_rand_int_range (CrankBenchRun *run,
                                const gint32   begin,
                                const gint32   end)
{
  return g_rand_int_range (run->random, begin, end);
}
Exemplo n.º 22
0
// 定时事件
// 定时事件中维持两个形状,mass和s_mass,
// 当mass下落到容器的底部,释放mass
// 当mass为空时,将s_mass赋给mass,再重新生成s_mass
static gboolean timeout()
{
  gint type;
  gboolean flag;
  DrawInfo *draw_info = russian_get_db_draw_info();
  MatrixInfo *matrix_info = russian_get_db_matrix_info();
  
  // 程序刚运行时,s_mass为空,需要生成
  if (draw_info->s_mass==NULL)
  {
    type = g_rand_int_range(g_rand_new(), 0, MASS_TYPE_NUM);
    draw_info->s_mass = russian_mass_new(type);
  }
  // 当mass为空时,将s_mass赋给mass,再重新生成s_mass
  if (draw_info->mass==NULL)
  {
    draw_info->mass = draw_info->s_mass;
    russian_mass_show(draw_info->mass, DIAMOND_TYPE_MASTER);
    type = g_rand_int_range(g_rand_new(), 0, MASS_TYPE_NUM);
    russian_mass_clear(draw_info->s_mass, DIAMOND_TYPE_BACK);
    draw_info->s_mass = russian_mass_new(type);
    russian_mass_show(draw_info->s_mass, DIAMOND_TYPE_BACK);        
    return TRUE;
  }
  // 暂停,结束时,不执行下落
  if (matrix_info->pause==TRUE || matrix_info->end==TRUE)
  {
    return TRUE;
  }
  // 当mass下落到容器的底部,释放mass
  if (russian_mass_downMove(draw_info->mass)==FALSE)
  {
    // 释放mass
    g_free(draw_info->mass);
    draw_info->mass = NULL;
    // 清除容器中已成满的行
    flag = russian_db_repair_maxtrix();
    // 如果容器已经被填满,游戏结束
    if (flag==FALSE)
    {
      russian_db_draw_gameover();
      matrix_info->end = TRUE;
      return TRUE;
    }
  }
  return TRUE;
}
Exemplo n.º 23
0
static GstFlowReturn
gst_rnd_buffer_size_drain_adapter (GstRndBufferSize * self, gboolean eos)
{
  GstFlowReturn flow;
  GstBuffer *buf;
  guint num_bytes, avail;

  flow = GST_FLOW_OK;

  if (G_UNLIKELY (self->min > self->max))
    goto bogus_minmax;

  do {
    if (self->min != self->max) {
      num_bytes = g_rand_int_range (self->rand, self->min, self->max);
    } else {
      num_bytes = self->min;
    }

    GST_LOG_OBJECT (self, "pulling %u bytes out of adapter", num_bytes);

    buf = gst_adapter_take_buffer (self->adapter, num_bytes);

    if (buf == NULL) {
      if (!eos) {
        GST_LOG_OBJECT (self, "not enough bytes in adapter");
        break;
      }

      avail = gst_adapter_available (self->adapter);

      if (avail == 0)
        break;

      if (avail < self->min) {
        GST_WARNING_OBJECT (self, "discarding %u bytes at end (min=%u)",
            avail, self->min);
        gst_adapter_clear (self->adapter);
        break;
      }
      buf = gst_adapter_take_buffer (self->adapter, avail);
      g_assert (buf != NULL);
    }

    flow = gst_pad_push (self->srcpad, buf);
  }
  while (flow == GST_FLOW_OK);

  return flow;

/* ERRORS */
bogus_minmax:
  {
    GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
        ("The minimum buffer size is smaller than the maximum buffer size."),
        ("buffer sizes: max=%d, min=%d", self->min, self->max));
    return GST_FLOW_ERROR;
  }
}
Exemplo n.º 24
0
int main(int argc, char **argv)
{
    int npoints = 100;

    GRand *rng = g_rand_new ();

    pointlist2d_t *candidates = pointlist2d_new (npoints);
    for (int i=0; i<npoints; i++) {
        candidates->points[i].x = g_rand_double_range (rng, 0, 100);
        candidates->points[i].y = g_rand_double_range (rng, 0, 100);
    }

    for (int i=0; i<5000000; i++) {
        pointlist2d_t *a = pointlist2d_new (g_rand_int_range (rng, 3, npoints / 3));
        pointlist2d_t *b = pointlist2d_new (g_rand_int_range (rng, 3, npoints / 3));

        for (int j=0; j<a->npoints; j++) {
            a->points[j] = 
                candidates->points[g_rand_int_range (rng, 0, npoints)];
        }
        for (int j=0; j<b->npoints; j++) {
            b->points[j] = 
                candidates->points[g_rand_int_range (rng, 0, npoints)];
        }

        pointlist2d_t *ah = convexhull_graham_scan_2d (a);
        if (ah) {
            pointlist2d_t *bh = convexhull_graham_scan_2d (b);
            if (bh) {
                pointlist2d_t *isect = 
                    geom_convex_polygon_convex_polygon_intersect_2d (ah, bh);

                if (isect) 
                    pointlist2d_free (isect);
                pointlist2d_free (bh);
            }
            pointlist2d_free (ah);
        }

        pointlist2d_free (a);
        pointlist2d_free (b);
    }

    return 0;
}
Exemplo n.º 25
0
void
test_field_part_intersect(void)
{
    enum { max_size = 40, niter = 500, n = 10000 };
    GRand *rng = g_rand_new_with_seed(42);

    for (guint iter = 0; iter < niter; iter++) {
        GwyFieldPart result = {
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
        };
        GwyFieldPart otherpart = {
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
        };
        GwyFieldPart fpart = result;
        gboolean intersecting = gwy_field_part_intersect(&result, &otherpart);

        for (guint k = 0; k < n; k++) {
            guint j = g_rand_int_range(rng, 0, 2*max_size);
            guint i = g_rand_int_range(rng, 0, 2*max_size);
            gboolean in_fpart = (j >= fpart.col
                                 && j - fpart.col < fpart.width
                                 && i >= fpart.row
                                 && i - fpart.row < fpart.height);
            gboolean in_otherpart = (j >= otherpart.col
                                     && j - otherpart.col < otherpart.width
                                     && i >= otherpart.row
                                     && i - otherpart.row < otherpart.height);
            gboolean in_result = (j >= result.col
                                  && j - result.col < result.width
                                  && i >= result.row
                                  && i - result.row < result.height);

            g_assert_cmpuint(in_result, ==, in_fpart && in_otherpart);
            g_assert(intersecting || !in_result);
        }
    }

    g_rand_free(rng);
}
Exemplo n.º 26
0
static void
on_monitors_changed (MetaScreen *screen,
                     MetaPlugin *plugin)
{
  MetaDefaultPlugin *self = META_DEFAULT_PLUGIN (plugin);
  int i, n;
  GRand *rand = g_rand_new_with_seed (123456);

  clutter_actor_destroy_all_children (self->priv->background_group);

  n = meta_screen_get_n_monitors (screen);
  for (i = 0; i < n; i++)
    {
      MetaRectangle rect;
      ClutterActor *background_actor;
      MetaBackground *background;
      ClutterColor color;

      meta_screen_get_monitor_geometry (screen, i, &rect);

      background_actor = meta_background_actor_new (screen, i);

      clutter_actor_set_position (background_actor, rect.x, rect.y);
      clutter_actor_set_size (background_actor, rect.width, rect.height);

      /* Don't use rand() here, mesa calls srand() internally when
         parsing the driconf XML, but it's nice if the colors are
         reproducible.
      */
      clutter_color_init (&color,
                          g_rand_int_range (rand, 0, 255),
                          g_rand_int_range (rand, 0, 255),
                          g_rand_int_range (rand, 0, 255),
                          255);

      background = meta_background_new (screen);
      meta_background_set_color (background, &color);
      meta_background_actor_set_background (META_BACKGROUND_ACTOR (background_actor), background);
      g_object_unref (background);

      clutter_actor_add_child (self->priv->background_group, background_actor);
    }

  g_rand_free (rand);
}
Exemplo n.º 27
0
static void
_append_random_chars (gchar *d, const char *chars, guint n)
{
	size_t len = strlen (chars);
	gchar *p = d + strlen(d);
	for (guint i=0; i<n ;i++)
		*(p++) = chars [g_rand_int_range (prng, 0, len)];
	*p = '\0';
}
Exemplo n.º 28
0
void
test_field_part_union(void)
{
    enum { max_size = 40, niter = 500, n = 10000 };
    GRand *rng = g_rand_new_with_seed(42);

    for (guint iter = 0; iter < niter; iter++) {
        GwyFieldPart result = {
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
        };
        GwyFieldPart otherpart = {
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
            g_rand_int_range(rng, 0, max_size),
        };
        GwyFieldPart fpart = result;
        gwy_field_part_union(&result, &otherpart);

        for (guint k = 0; k < n; k++) {
            guint j = g_rand_int_range(rng, 0, 2*max_size);
            guint i = g_rand_int_range(rng, 0, 2*max_size);
            gboolean in_x = ((j >= fpart.col
                              || j >= otherpart.col)
                             && (j < fpart.col + fpart.width
                                 || j < otherpart.col + otherpart.width));
            gboolean in_y = ((i >= fpart.row
                              || i >= otherpart.row)
                             && (i < fpart.row + fpart.height
                                 || i < otherpart.row + otherpart.height));
            gboolean in_result = (j >= result.col
                                  && j - result.col < result.width
                                  && i >= result.row
                                  && i - result.row < result.height);

            g_assert_cmpuint(in_result, ==, in_x && in_y);
        }
    }

    g_rand_free(rng);
}
Exemplo n.º 29
0
static void
futz_row (void)
{
    gint i;
    GtkTreePath *path;
    GtkTreeIter iter;
    GtkTreeIter iter2;

    i = g_rand_int_range (grand, 0,
                          gtk_tree_model_iter_n_children (model, NULL));
    path = gtk_tree_path_new ();
    gtk_tree_path_append_index (path, i);
    gtk_tree_model_get_iter (model, &iter, path);
    gtk_tree_path_free (path);

    if (gtk_tree_selection_iter_is_selected (selection, &iter))
        return;
    switch (g_rand_int_range (grand, 0, 3))
    {
    case 0:
        /* insert */
        gtk_list_store_insert_after (GTK_LIST_STORE (model),
                                     &iter2, &iter);
        gtk_list_store_set (GTK_LIST_STORE (model), &iter2,
                            TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
                            -1);
        break;
    case 1:
        /* delete */
        if (gtk_tree_model_iter_n_children (model, NULL) == 0)
            return;
        gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
        break;
    case 2:
        /* modify */
        return;
        if (gtk_tree_model_iter_n_children (model, NULL) == 0)
            return;
        gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                            TEXT_COLUMN, words[g_rand_int_range (grand, 0, NUM_WORDS)],
                            -1);
        break;
    }
}
Exemplo n.º 30
0
void gw_flashcardstore_trim (GwFlashCardStore *store, gint max)
{
    //Sanity Checks
    if (store == NULL) return;
    if (max < 1) return;

    //Declarations
    GtkTreeModel *model;
    GtkTreePath *path;
    GtkTreeIter iter;
    gboolean valid;
    gint children;
    gchar *path_string;
    GRand *random;
    gint position;

    gtk_tree_sortable_set_sort_column_id (
        GTK_TREE_SORTABLE (store), 
        GW_FLASHCARDSTORE_COLUMN_WEIGHT, 
        GTK_SORT_DESCENDING);

    model = GTK_TREE_MODEL (store);
    random = g_rand_new ();
    children = gtk_tree_model_iter_n_children (model, NULL);

    if (random != NULL)
    {
      while (children > max && children > 0)
      {
        position = g_rand_int_range (random, 0, children);
        path_string = g_strdup_printf ("%d", position);
        if (path_string != NULL)
        {
          valid = gtk_tree_model_get_iter_from_string (model, &iter, path_string);
          if (valid)
          {
            gtk_tree_model_get (model, &iter, GW_FLASHCARDSTORE_COLUMN_TREE_PATH, &path, -1);
            if (path != NULL) gtk_tree_path_free (path); path = NULL;
            gtk_list_store_remove (GTK_LIST_STORE (store), &iter);
          }
          g_free (path_string); path_string = NULL;
        }
        children--;
      }
      g_rand_free (random); random = NULL;
    } 

    gtk_tree_sortable_set_sort_column_id (
        GTK_TREE_SORTABLE (store), 
        GW_FLASHCARDSTORE_COLUMN_ORDER, 
        GTK_SORT_ASCENDING);
    gtk_tree_sortable_set_sort_column_id (
        GTK_TREE_SORTABLE (store), 
        GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 
        GTK_SORT_ASCENDING);
}