示例#1
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);
}
示例#2
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);
}
示例#3
0
static void _random2_distribution (GPtrArray *points,
				  AranSolver2d *solver)
{
  guint i;
  PointAccum *point;
  GRand *rand = g_rand_new_with_seed (_random_seed);

  point = point_accum_alloc (TRUE, NULL);

  for (i=0; i<np; i++)
    {

#ifdef VSG_HAVE_MPI
      if (i%_flush_interval == 0)
        {
          aran_solver2d_migrate_flush (solver);
          if (i%(_flush_interval*10) == 0)
            {
              if (_verbose && rk == 0)
                g_printerr ("%d: contiguous dist before %dth point\n", rk, i);

              aran_solver2d_distribute_contiguous_leaves (solver);
            }
        }
#endif /* VSG_HAVE_MPI */

      point->vector.x = g_rand_double_range (rand, -R, R);;
      point->vector.y = g_rand_double_range (rand, -R, R);;
      point->density = 1.;
      point->accum = 0.;
      point->id = i;

      if (check) memcpy (&check_points[i], point, sizeof (PointAccum));

      if (aran_solver2d_insert_point_local (solver, point))
        {
          if (i % 10000 == 0 && _verbose)
            g_printerr ("%d: insert %dth point\n", rk, i);

          point = point_accum_alloc (TRUE, NULL);
        }

#ifdef VSG_HAVE_MPI
      if (i%(_flush_interval*10) == 0)
        {
          if (_verbose && rk == 0)
            g_printerr ("%d: contiguous dist before %dth point\n", rk, i);
          aran_solver2d_distribute_contiguous_leaves (solver);
        }
#endif /* VSG_HAVE_MPI */
    }

  point_accum_destroy (point, TRUE, NULL);

#ifdef VSG_HAVE_MPI
  aran_solver2d_distribute_contiguous_leaves (solver);
#endif /* VSG_HAVE_MPI */

  g_rand_free (rand);
}
void dp_evaluation_individ_scramble(DpEvaluationCtrl*hevalctrl, DpIndivid*individ, double eps)
{
	int i;
	double y, z;
	for ( i = 0; i < hevalctrl->eval->size; i++) {
		if ( eps > 0 ) {
			z = (*(hevalctrl->eval->points[i]->param)) * hevalctrl->eval->points[i]->scale;
			y = g_rand_double_range(individ->hrand, (1.0 - eps) * z, (1.0 + eps) * z);
		} else {
			y = g_rand_double_range(individ->hrand, hevalctrl->eval->points[i]->lower, hevalctrl->eval->points[i]->upper);
		}
		if ( hevalctrl->eval->points[i]->limited ) {
			if ( hevalctrl->eval_strategy == sin_trans_flag )
				individ->x[i] = asin ( ( y - hevalctrl->eval->points[i]->alpha ) / hevalctrl->eval->points[i]->beta) / hevalctrl->eval->points[i]->gamma;
			else if ( hevalctrl->eval_strategy == tanh_trans_flag )
				individ->x[i] = atanh ( ( y - hevalctrl->eval->points[i]->alpha ) / hevalctrl->eval->points[i]->beta) / hevalctrl->eval->points[i]->gamma;
			else if ( hevalctrl->eval_strategy == alg_trans_flag )
				individ->x[i] = y;
			else if ( hevalctrl->eval_strategy == rand_trans_flag )
				individ->x[i] = y;
		} else {
			individ->x[i] = y;
		}
	}
}
示例#5
0
static void _random2_fill (AranSolver3d *solver)
{
  guint64 i;
  PointAccum *point;
  GRand *rand = g_rand_new_with_seed (_random_seed);

  point = point_accum_alloc (TRUE, NULL);

  for (i=0; i<np; i++)
    {

#ifdef VSG_HAVE_MPI
      if (i%_flush_interval == 0)
        {
          aran_solver3d_migrate_flush (solver);
          if (i%(_flush_interval*10) == 0)
            {
              if (_verbose && rk == 0)
                g_printerr ("%d: contiguous dist before %luth point\n", rk, i);

              aran_solver3d_distribute_contiguous_leaves (solver);
              _flush_interval *=2;
            }
        }
#endif /* VSG_HAVE_MPI */

      point->vector.x = g_rand_double_range (rand, -R, R);;
      point->vector.y = g_rand_double_range (rand, -R, R);;
      point->vector.z = g_rand_double_range (rand, -R, R);;
      point->density = 1./np;
      point->field = VSG_V3D_ZERO;
      point->id = i;

      /* if (i == 3) point->density = 0.; */
      /* if (i == 4) point->density = 0.; */
      /* if (i == 5) point->density = 0.; */

      if (check) memcpy (&check_points[i], point, sizeof (PointAccum));

      if (aran_solver3d_insert_point_local (solver, point))
        {
          if (i % 10000 == 0 && _verbose)
            g_printerr ("%d: insert %luth point\n", rk, i);

          point = point_accum_alloc (TRUE, NULL);
        }

    }

  point_accum_destroy (point, TRUE, NULL);

#ifdef VSG_HAVE_MPI
  aran_solver3d_migrate_flush (solver);
  aran_solver3d_distribute_contiguous_leaves (solver);
#endif /* VSG_HAVE_MPI */

  g_rand_free (rand);
}
/* UV sphere fill */
static void _uvsphere_fill (AranSolver3d *solver)
{
  gint i;
  PointAccum *point;
  VsgVector3d lb, ub;
  GRand *rand = g_rand_new_with_seed (_random_seed);
  gdouble r;

  aran_solver3d_get_bounds (solver, &lb, &ub);
  r = MIN (lb.x, ub.x);

  point = point_accum_alloc (TRUE, NULL);

  for (i=0; i< np; i++)
    {
      gdouble theta, phi;

      theta = g_rand_double_range (rand, 0.01 * G_PI, 0.99 * G_PI);
      phi = g_rand_double_range (rand, 0., 2.*G_PI);

      vsg_vector3d_from_spherical (&point->vector, r, theta, phi);
      point->density = 1.;
      point->field = VSG_V3D_ZERO;
      point->id = i;

      if (check) memcpy (&check_points[i], point, sizeof (PointAccum));

      if (aran_solver3d_insert_point_local (solver, point))
        {
          if (i % 10000 == 0 && _verbose)
            g_printerr ("%d: insert %dth point\n", rk, i);

          point = point_accum_alloc (TRUE, NULL);
        }

#ifdef VSG_HAVE_MPI
      if (i%(_flush_interval*10) == 0)
        {
          if (_verbose && rk == 0)
            g_printerr ("%d: contiguous dist before %dth point\n", rk, i);
          aran_solver3d_distribute_contiguous_leaves (solver);

          _flush_interval *=2;
        }
#endif /* VSG_HAVE_MPI */
    }

  point_accum_destroy (point, TRUE, NULL);

#ifdef VSG_HAVE_MPI
  aran_solver3d_distribute_contiguous_leaves (solver);
#endif /* VSG_HAVE_MPI */

  g_rand_free (rand);
}
static void _random_fill (AranSolver3d *solver)
{
  guint i;
  PointAccum *point;
  GRand *rand = g_rand_new_with_seed (_random_seed);

  for (i=0; i<np; i++)
    {
      PointAccum tmp;

#ifdef VSG_HAVE_MPI
      if (i%_flush_interval == 0)
        {
          aran_solver3d_migrate_flush (solver);
          if (i%(_flush_interval*10) == 0)
            {
              if (_verbose && rk == 0)
                g_printerr ("%d: contiguous dist before %dth point\n", rk, i);

              aran_solver3d_distribute_contiguous_leaves (solver);
              _flush_interval *=2;
            }
        }
#endif /* VSG_HAVE_MPI */

      tmp.vector.x = g_rand_double_range (rand, -R, R);;
      tmp.vector.y = g_rand_double_range (rand, -R, R);;
      tmp.vector.z = g_rand_double_range (rand, -R, R);;
      tmp.density = 1.;
      tmp.field = VSG_V3D_ZERO;
      tmp.id = i;

      if (check) memcpy (&check_points[i], &tmp, sizeof (PointAccum));

      if (i%sz != rk) continue;

      if (i % 10000 == 0 && _verbose)
        g_printerr ("%d: insert %dth point\n", rk, i);

      point = point_accum_alloc (TRUE, NULL);

      memcpy (point, &tmp, sizeof (PointAccum));

      aran_solver3d_insert_point (solver, point);
    }

#ifdef VSG_HAVE_MPI
  aran_solver3d_migrate_flush (solver);
  aran_solver3d_distribute_contiguous_leaves (solver);
#endif /* VSG_HAVE_MPI */

  g_rand_free (rand);
}
void _random_fill (VsgPRTree2d *tree, guint np)
{
  gint i;
  Pt *pt;
  VsgVector2d lb, ub;
  GRand *rand = g_rand_new_with_seed (_random_seed);

  vsg_prtree2d_get_bounds (tree, &lb, &ub);

  _ref_count = 0;

  for (i=0; i< np; i++)
    {
      gdouble x1, y1;
      gint c;

      x1 = g_rand_double_range (rand, lb.x, ub.x);
      y1 = g_rand_double_range (rand, lb.y, ub.y);

      c = i+1;

      _ref_count += c;

      if (i%_flush_interval == 0)
        {
          vsg_prtree2d_migrate_flush (tree);
          if (i%(_flush_interval*10) == 0)
            {
              if (_verbose && rk == 0)
                g_printerr ("%d: contiguous dist before %dth point\n", rk, i);
              _distribute (tree);
            }
        }

      if (i%sz != rk) continue;

      if (i % 10000 == 0 && _verbose)
        g_printerr ("%d: insert %dth point\n", rk, i);

      pt = pt_alloc (TRUE, NULL);
      pt->vector.x = x1;
      pt->vector.y = y1;
      pt->weight = c;

      vsg_prtree2d_insert_point (tree, pt);
    }

  vsg_prtree2d_migrate_flush (tree);

  _distribute (tree);

  g_rand_free (rand);
}
示例#9
0
文件: tile-paper.c 项目: GNOME/gegl
static inline void
random_move (gint  *x,
             gint  *y,
             gint   max,
             GRand *gr)
{
  gdouble angle  = g_rand_double_range (gr, 0.0, 1.0) * G_PI;
  gdouble radius = g_rand_double_range (gr, 0.0, 1.0) * (gdouble) max;

  *x = (gint) (radius * cos (angle));
  *y = (gint) (radius * sin (angle));
}
static float
event_get_value_with_deviation(GRand *rand, float last_value, Term *term,
    Variable *var)
{
    float min, max;

    if (term_is_discrete(term) || isnan(last_value))
        return last_value;

    min = fabs(last_value - LAST_VALUE_DEVIATION);
    max = last_value + LAST_VALUE_DEVIATION;
    if (term) {
        if (max > term->max)
            max = term->max;
        if (min < term->min)
            min = term->min;
    } else {
        if (max > var->max)
            max = var->max;
        if (min < var->min)
            min = var->min;
    }

    float val;
    val = (float)g_rand_double_range(rand, min, max);
    return val;
}
示例#11
0
/**
 * crank_bench_run_rand_double_range: (skip)
 * @run: A benchmark run.
 * @begin: Begin of range.
 * @end: End of range.
 *
 * Returns random double in the range: [@begin, @end).
 *
 * Returns: A random double in the range.
 */
gdouble
crank_bench_run_rand_double_range (CrankBenchRun *run,
                                   const gdouble  begin,
                                   const gdouble  end)
{
  return g_rand_double_range (run->random, begin, end);
}
示例#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;
示例#13
0
文件: druxlord.c 项目: ajhwb/druxlord
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);
}
示例#14
0
文件: spread.c 项目: 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);
    }
}
示例#15
0
/** Evaluate an Eh_input_val

\param val An initialized Eh_input_val
\param ... Extra parameter for a time value if \a val is set to read from
           a time series.

\return The new value
*/
double
eh_input_val_eval( Eh_input_val val , ... )
{
   va_list args;
   double data;

   if ( val->type == EH_INPUT_VAL_RAND_NORMAL )
      val->val = eh_rand_normal( val->rand , val->data[0] , val->data[1] );
   else if ( val->type == EH_INPUT_VAL_RAND_UNIFORM )
      val->val = g_rand_double_range( val->rand , val->data[0] , val->data[1] );
   else if ( val->type == EH_INPUT_VAL_RAND_WEIBULL )
      val->val = eh_rand_weibull( val->rand , val->data[0] , val->data[1] );
   else if ( val->type == EH_INPUT_VAL_RAND_USER )
      val->val = eh_rand_user( val->rand , val->x , val->y , val->len );
   else if ( val->type == EH_INPUT_VAL_FILE )
   {

      eh_require( val->x );
      eh_require( val->y );

      va_start( args , val );
      data = va_arg( args , double );

      interpolate( val->x , val->y , val->len , &data , &(val->val) , 1 );

      va_end( args );
   }
示例#16
0
/**
 * crank_bench_run_rand_float_range: (skip)
 * @run: A benchmark run.
 * @begin: Begin of range.
 * @end: End of range.
 *
 * Returns random float in the range: [@begin, @end).
 *
 * Returns: A random float in the range.
 */
gfloat
crank_bench_run_rand_float_range (CrankBenchRun *run,
                                   const gfloat   begin,
                                   const gfloat   end)
{
  return (gfloat) g_rand_double_range (run->random, (gdouble)begin, (gdouble)end);
}
示例#17
0
void
test_field_mean(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 mean, mean_expected;
        mean = gwy_field_mean_full(field);
        mean_expected = 0.5*(alpha + beta);
        gwy_assert_floatval(mean, mean_expected, 1e-9*fabs(mean_expected));

        field_randomize(field, rng);
        guint width = g_rand_int_range(rng, 1, xres+1);
        guint height = g_rand_int_range(rng, 1, yres+1);
        guint col = g_rand_int_range(rng, 0, xres-width+1);
        guint row = g_rand_int_range(rng, 0, yres-height+1);
        GwyFieldPart fpart = { col, row, width, height };

        GwyMaskField *mask = random_mask_field(xres, yres, rng);
        guint m = gwy_mask_field_part_count(mask, &fpart, TRUE);
        guint n = gwy_mask_field_part_count(mask, &fpart, FALSE);
        gdouble mean_include = gwy_field_mean(field, &fpart,
                                              mask, GWY_MASK_INCLUDE);
        gdouble mean_exclude = gwy_field_mean(field, &fpart,
                                              mask, GWY_MASK_EXCLUDE);
        gdouble mean_ignore = gwy_field_mean(field, &fpart,
                                             mask, GWY_MASK_IGNORE);

        if (isnan(mean_include)) {
            g_assert_cmpuint(m, ==, 0);
            gwy_assert_floatval(mean_exclude, mean_ignore,
                                1e-9*fabs(mean_ignore));
        }
        else if (isnan(mean_exclude)) {
            g_assert_cmpuint(n, ==, 0);
            gwy_assert_floatval(mean_include, mean_ignore,
                                1e-9*fabs(mean_ignore));
        }
        else {
示例#18
0
static gboolean
on_window_draw (GtkWidget *widget,
                cairo_t   *cr)

{
  GRand *rand = g_rand_new_with_seed(0);
  int i;
  int width, height;

  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);

  ensure_resources (cairo_get_target (cr));

  cairo_set_source_rgb(cr, 1, 1, 1);
  cairo_paint(cr);

  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_set_line_width(cr, 1.0);
  cairo_rectangle (cr, 0.5, 0.5, width - 1, height - 1);
  cairo_stroke (cr);

  for(i = 0; i < load_factor * 150; i++)
    {
      int source = g_rand_int_range(rand, 0, 255);
      double phi = g_rand_double_range(rand, 0, 2 * M_PI) + angle;
      double r = g_rand_double_range(rand, 0, width / 2 - RADIUS);
      int x, y;

      int source_x = (source % 16) * DIAMETER;
      int source_y = (source / 16) * DIAMETER;

      x = round(width / 2 + r * cos(phi) - RADIUS);
      y = round(height / 2 - r * sin(phi) - RADIUS);

      cairo_set_source_surface(cr, source_surface,
                               x - source_x, y - source_y);
      cairo_rectangle(cr, x, y, DIAMETER, DIAMETER);
      cairo_fill(cr);
    }

  g_rand_free(rand);

  return FALSE;
}
示例#19
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;
}
示例#20
0
static void
random_rgba (GRand  *gr,
             gfloat *dest)
{
  gint i;

  for (i = 0; i < 3; i++)
    dest[i] = (gfloat) g_rand_double_range (gr, 0.0, 1.0);
}
示例#21
0
static guchar*
rgb_to_hsl (GimpDrawable     *drawable,
            LICEffectChannel  effect_channel)
{
  guchar       *themap, data[4];
  gint          x, y;
  GimpRGB       color;
  GimpHSL       color_hsl;
  gdouble       val = 0.0;
  glong         maxc, index = 0;
  GimpPixelRgn  region;
  GRand        *gr;

  gr = g_rand_new ();

  maxc = drawable->width * drawable->height;

  gimp_pixel_rgn_init (&region, drawable, border_x, border_y,
                       border_w, border_h, FALSE, FALSE);

  themap = g_new (guchar, maxc);

  for (y = 0; y < region.h; y++)
    {
      for (x = 0; x < region.w; x++)
        {
          data[3] = 255;

          gimp_pixel_rgn_get_pixel (&region, data, x, y);
          gimp_rgba_set_uchar (&color, data[0], data[1], data[2], data[3]);
          gimp_rgb_to_hsl (&color, &color_hsl);

          switch (effect_channel)
            {
            case LIC_HUE:
              val = color_hsl.h * 255;
              break;
            case LIC_SATURATION:
              val = color_hsl.s * 255;
              break;
            case LIC_BRIGHTNESS:
              val = color_hsl.l * 255;
              break;
            }

          /* add some random to avoid unstructured areas. */
          val += g_rand_double_range (gr, -1.0, 1.0);

          themap[index++] = (guchar) CLAMP0255 (RINT (val));
        }
    }

  g_rand_free (gr);

  return themap;
}
示例#22
0
static void
update_sensor_value(IsSensor *sensor,
		    IsFakePlugin *self)
{
	/* fake value */
	is_sensor_set_value(sensor,
			    g_rand_double_range(self->priv->rand,
						is_sensor_get_low_value(sensor),
						is_sensor_get_high_value(sensor)));
}
示例#23
0
文件: main.c 项目: sonald/glxdemo
static gboolean on_ref_draw(GtkWidget* widget, cairo_t* cr, gpointer data)
{
    double r = g_rand_double_range(rand, 0.0, 1.0);
    double g = g_rand_double_range(rand, 0.0, 1.0);
    double b = g_rand_double_range(rand, 0.0, 1.0);
    
    int x = g_rand_int_range(rand, 0, g_width);
    int y = g_rand_int_range(rand, 0, g_height);

    int w = g_rand_int_range(rand, g_width/2, g_width+1);
    int h = g_rand_int_range(rand, g_height/2, g_height+1);

    cairo_set_source_rgb(cr, r, g, b);
    cairo_rectangle(cr, x, y, w, h);
    cairo_fill(cr);

    gtk_widget_queue_draw(top);
    return TRUE;
}
示例#24
0
static void random_fill (VsgPRTree3d *tree, guint np)
{
  gint i;
  VsgVector3d *pt;
  Sphere *c;
  VsgVector3d lb, ub;
  GRand *rand = g_rand_new_with_seed (_random_seed);

  vsg_prtree3d_get_bounds (tree, &lb, &ub);

  for (i=0; i< np; i++)
    {
      gdouble x1, x2, y1, y2, z1, z2, r;

      x1 = g_rand_double_range (rand, lb.x, ub.x);
      y1 = g_rand_double_range (rand, lb.y, ub.y);
      z1 = g_rand_double_range (rand, lb.z, ub.z);
      x2 = g_rand_double_range (rand, lb.x, ub.x);
      y2 = g_rand_double_range (rand, lb.y, ub.y);
      z2 = g_rand_double_range (rand, lb.z, ub.z);
      r = g_rand_double_range (rand, 0., 0.1);

      if (i%_flush_interval == 0) vsg_prtree3d_migrate_flush (tree);

      if (i%sz != rk) continue;

      if (i % 10000 == 0 && _verbose) g_printerr ("%d: insert %dth point\n", rk, i);

      pt = pt_alloc (TRUE, NULL);
      pt->x = x1;
      pt->y = y1;
      pt->z = z1;
      vsg_prtree3d_insert_point (tree, pt);

      if (_put_regions)
        {
          c = rg_alloc (TRUE, NULL);
          c->center.x = x2;
          c->center.y = y2;
          c->center.z = z2;
          c->radius = r;
          vsg_prtree3d_insert_region (tree, c);
        }
/*       g_printerr ("%d: %d\n", rk, i); */
    }

  vsg_prtree3d_migrate_flush (tree);

  g_rand_free (rand);
}
示例#25
0
static void
add_random (GRand  *gr,
            gfloat *dest,
            gfloat  amount)
{
  gint    i;
  gfloat  tmp;

  amount /= 2.0;

  if (amount > 0)
    for (i = 0; i < 3; i++)
      {
        tmp = dest[i] + (gfloat) g_rand_double_range (gr, -amount, amount);
        dest[i] = CLAMP (tmp, 0.0, 1.0);
      }
}
示例#26
0
文件: g_neuron.c 项目: v36372/glibnn
void g_neuron_set_random_weight(GNeuron *neuron)
{
	GRand* rand;
	gint i;

	g_return_if_fail(NULL != neuron);

	neuron->priv = G_TYPE_INSTANCE_GET_PRIVATE (neuron,
			TYPE_G_NEURON, GNeuronPrivate);
	GNeuronPrivate *priv = neuron->priv;

	rand = g_rand_new();

	for(i = 0; i < priv->ninput; ++i)
	{
		priv->weights[i] = g_rand_double_range(rand, -0.5f, 0.5f);
	}
}
示例#27
0
static void
generatevectors (void)
{
  gdouble alpha;
  gint i, j;
  GRand *gr;

  gr = g_rand_new();

  for (i = 0; i < numx; i++)
    {
      for (j = 0; j < numy; j++)
        {
          alpha = g_rand_double_range (gr, 0, 2) * G_PI;
          G[i][j][0] = cos (alpha);
          G[i][j][1] = sin (alpha);
        }
    }

  g_rand_free (gr);
}
示例#28
0
int random_assignment(Job *jobarray, int njobs, int nmachines,
                      solution *new_sol, GRand *rand_) {
    int i, val = 0;
    double n;
    partlist *temp = (partlist *) NULL;
    Job *j = (Job *) NULL;
    GQueue *queue = (GQueue *) NULL;
    queue = g_queue_new();

    for (i = 0; i < nmachines; ++i) {
        g_queue_push_head(queue, new_sol->part + i);
    }

    for (i = 0; i < njobs; ++i) {
        j = jobarray + i;
        n = g_rand_double_range(rand_, 0.0, 1.0);

        if (n < 0.8) {
            temp = (partlist *) g_queue_pop_head(queue);
        } else if (n >= 0.8 && n < 0.95) {
            temp = (partlist *) g_queue_pop_nth(queue, 1);
        } else {
            if(nmachines > 2) {
                temp = (partlist *)g_queue_pop_nth(queue, 2);
            } else {
                temp = (partlist *)g_queue_pop_nth(queue, 1);
            }
        }

        val = partlist_insert(temp, new_sol->vlist, j);
        CCcheck_val_2(val, "Failed in partlist_insert_order");
        g_queue_insert_sorted(queue, temp, compare_func1, NULL);
    }

CLEAN:
    g_queue_free(queue);
    return val;
}
示例#29
0
static void
calc_sample_coords (gint      src_x,
                    gint      src_y,
                    gint      x_amount,
                    gint      y_amount,
                    GRand    *gr,
                    gdouble  *x,
                    gdouble  *y)
{
  gdouble        angle;
  gint           xdist, ydist;

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

  *x = src_x + floor (sin (angle) * xdist);
  *y = src_y + floor (cos (angle) * ydist);
}
示例#30
0
文件: sinus.c 项目: Minoos/gimp
static void
prepare_coef (params *p)
{
  GimpRGB color1;
  GimpRGB color2;
  gdouble scalex = svals.scalex;
  gdouble scaley = svals.scaley;
  GRand *gr;

  gr = g_rand_new ();

  g_rand_set_seed (gr, svals.seed);

  switch (svals.colorization)
    {
    case BILINEAR:
      p->blend = bilinear;
      break;
    case SINUS:
      p->blend = cosinus;
      break;
    case LINEAR:
    default:
      p->blend = linear;
    }

  if (svals.perturbation==IDEAL)
    {
      /* Presumably the 0 * g_rand_int ()s are to pop random
       * values off the prng, I don't see why though. */
      p->c11= 0 * g_rand_int (gr);
      p->c12= g_rand_double_range (gr, -1, 1) * scaley;
      p->c13= g_rand_double_range (gr, 0, 2 * G_PI);
      p->c21= 0 * g_rand_int (gr);
      p->c22= g_rand_double_range (gr, -1, 1)  * scaley;
      p->c23= g_rand_double_range (gr, 0, 2 * G_PI);
      p->c31= g_rand_double_range (gr, -1, 1) * scalex;
      p->c32= 0 * g_rand_int (gr);
      p->c33= g_rand_double_range (gr, 0, 2 * G_PI);
    }
  else
    {
      p->c11= g_rand_double_range (gr, -1, 1) * scalex;
      p->c12= g_rand_double_range (gr, -1, 1) * scaley;
      p->c13= g_rand_double_range (gr, 0, 2 * G_PI);
      p->c21= g_rand_double_range (gr, -1, 1) * scalex;
      p->c22= g_rand_double_range (gr, -1, 1) * scaley;
      p->c23= g_rand_double_range (gr, 0, 2 * G_PI);
      p->c31= g_rand_double_range (gr, -1, 1) * scalex;
      p->c32= g_rand_double_range (gr, -1, 1) * scaley;
      p->c33= g_rand_double_range (gr, 0, 2 * G_PI);
    }

  if (svals.tiling)
    {
      p->c11= ROUND (p->c11/(2*G_PI))*2*G_PI;
      p->c12= ROUND (p->c12/(2*G_PI))*2*G_PI;
      p->c21= ROUND (p->c21/(2*G_PI))*2*G_PI;
      p->c22= ROUND (p->c22/(2*G_PI))*2*G_PI;
      p->c31= ROUND (p->c31/(2*G_PI))*2*G_PI;
      p->c32= ROUND (p->c32/(2*G_PI))*2*G_PI;
    }

  color1 = svals.col1;
  color2 = svals.col2;

  if (drawable_is_grayscale)
    {
      gimp_rgb_set (&color1, 1.0, 1.0, 1.0);
      gimp_rgb_set (&color2, 0.0, 0.0, 0.0);
    }
  else
    {
      switch (svals.colors)
        {
        case USE_COLORS:
          break;
        case B_W:
          gimp_rgb_set (&color1, 1.0, 1.0, 1.0);
          gimp_rgb_set (&color2, 0.0, 0.0, 0.0);
          break;
        case USE_FG_BG:
          gimp_context_get_background (&color1);
          gimp_context_get_foreground (&color2);
          break;
        }
    }

  gimp_rgba_get_uchar (&color1, &p->r, &p->g, &p->b, &p->a);

  gimp_rgba_subtract (&color2, &color1);
  p->dr = color2.r * 255.0;
  p->dg = color2.g * 255.0;
  p->db = color2.b * 255.0;
  p->da = color2.a * 255.0;

  g_rand_free (gr);
}