Esempio n. 1
0
Point3f Triangle::GetLightPointInGrid(int gridNum) const
{
	Vector3f u(vertex2 /*start*/, vertex1 /*end*/);
	Vector3f v(vertex2 /*start*/, vertex3 /*end*/);

	float du = _rand();
	float dv = _rand();

	return vertex2 + u * du + v * dv;
}
Esempio n. 2
0
void Knob::jitter(float_arr & pts, double xVar, double yVar,double bVar, double fVar) {
    for (int i = 0; i < DATA_LEN; i++) {
      double b = pts[i][XDB];
      double f = pts[i][XDF];

      if ((i>0) && (i < DATA_LEN-1))
        pts[i][X] += _rand()*xVar*2.0 - xVar;
      pts[i][Y] +=   _rand()*yVar*2.0 - yVar;
      // b,f each varies by + or - it*itsVar
      pts[i][XDB] += _rand()*b*bVar*2.0 - b*bVar;
      pts[i][XDF] += _rand()*f*fVar*2.0 - f*fVar;
    }
}
/// Update deadline to next appropriate value. Expects
/// CLOCK_MONOTONIC current time, in milliseconds.  Call on
/// successful connection.
void HttpConnection::PoolEntry::update_deadline(unsigned long now_ms)
{
  // Get the next desired inter-arrival time. Take a random sample
  // from an exponential distribution so as to avoid spikes.
  unsigned long interval_ms = (unsigned long)_rand();

  if ((_deadline_ms == 0L) ||
      ((_deadline_ms + interval_ms) < now_ms))
  {
    // This is the first request, or the new arrival time has
    // already passed (in which case things must be pretty
    // quiet). Just bump the next deadline into the future.
    _deadline_ms = now_ms + interval_ms;
  }
  else
  {
    // The new arrival time is yet to come. Schedule it relative to
    // the last intended time, so as not to skew the mean
    // upwards.

    // We don't recycle any connections in the UTs. (We could do this
    // by manipulating time, but would have no way of checking it
    // worked.)
    _deadline_ms += interval_ms; // LCOV_EXCL_LINE
  }
}
Esempio n. 4
0
void MWO_Randomise::operator()() const
{
	unsigned int i;

	for (i = 0; i < getRows()*getColumns(); i++) {
		element(i) = _rand();
	}
}
Esempio n. 5
0
ulong
_tlrand(void)
{
	Thread *t;
	
	t = (Thread*)pthread_getspecific(tlskey);
	assert(t != nil);
	return _rand(&t->r);
}
Esempio n. 6
0
void test_content()
{
  tjpeg_buffer_t *b = tjpeg_buffer_new();
  uint8_t         data[TJPEG_BUFFER_SIZE * 8];
  int             p = 0;

  printf("test_content\n");

  tjpeg_buffer_init(b);

  for (int i = 0; i < sizeof(data); ++i)
    data[i] = _rand(0, 1);

  while (p < sizeof(data) - 24) {
    int r = _rand(1, 16);
    uint16_t bits = 0;

    for (int i = 0; i < r; ++i)
      bits |= (1 & data[p + i]) << (r - 1 - i);

    tjpeg_buffer_add(b, bits, r);
    p += r;
  }

  int r = sizeof(data) - p - 8;
  uint16_t bits = 0;

  for (int i = 0; i < r; ++i)
    bits |= (1 & data[p + i]) << (r - 1 - i);

  tjpeg_buffer_add(b, bits & ((1 << r) - 1), r);
  p += r;

  for (int i = 0; i < TJPEG_BUFFER_SIZE - 1; ++i) {
    uint8_t bits = 0;

    for (int j = 0; j < 8; ++j) {
      bits |= (1 & data[i * 8 + j]) << (7 - j);
    }

    printf("Checking byte %2d:  0x%02x == 0x%02x\n", i, bits, ((unsigned char *) b)[i]);
    assert(bits == ((unsigned char *) b)[i]);
  }
}
MirrorDownload::MirrorDownload(const HttpConnectionPtr& conn, 
							   const MirrorList& mirrors, 
							   const std::string& srcFilename,
							   const fs::path& destFilename) :
	Download(conn, "", destFilename) // empty URL for starters
{
	if (mirrors.empty())
	{
		throw std::runtime_error("No mirrors available, cannot continue.");
	}

	// Pick a random mirror
	float p = static_cast<float>(_rand()) / _rand.max();

	// Generate the list of URLs to use, starting with the "preferred"
	// mirror on top of the list. The "preferred" mirror is the one picked
	// by the random number generator, respecting the weights.
	std::list<std::string> urls;
	std::list<Mirror> orderedMirrors;

	// Re-sort the incoming mirrors into the URL list
	for (MirrorList::const_iterator m = mirrors.begin(); m != mirrors.end(); ++m)
	{
		if (p < m->weight)
		{
			// Preferred mirror, add at front
			p = 1000000.0f; // make sure the rest of the mirrors isn't pushed at the front
			orderedMirrors.push_front(*m);
			urls.push_front(m->url + srcFilename);
			TraceLog::WriteLine(LOG_VERBOSE, "Picking mirror " + m->displayName);
		}
		else
		{
			// Non-preferred mirror, add at the end of the list
			p -= m->weight;
			urls.push_back(m->url + srcFilename);
			orderedMirrors.push_back(*m);
		}
	}

	if (urls.empty())
	{
		throw std::runtime_error("No URLs available after mirror sort, cannot continue.");
	}

	// Push the sorted URLs into the protected member
	_urls = std::vector<std::string>(urls.begin(), urls.end());

	// Push the sorted Mirrors into our own _mirrors member
	_mirrors = std::vector<Mirror>(orderedMirrors.begin(), orderedMirrors.end());
}
Esempio n. 8
0
void ng_ndp_netif_add(ng_ipv6_netif_t *iface)
{
    uint32_t reach_time = _rand(NG_NDP_MIN_RAND, NG_NDP_MAX_RAND);

    /* set default values */
    mutex_lock(&iface->mutex);
    iface->reach_time_base = NG_NDP_REACH_TIME;
    reach_time = (reach_time * iface->reach_time_base) / 10;
    iface->reach_time = timex_set(0, reach_time);
    timex_normalize(&iface->reach_time);
    iface->retrans_timer = timex_set(0, NG_NDP_RETRANS_TIMER);
    timex_normalize(&iface->retrans_timer);
    mutex_unlock(&iface->mutex);
}
Esempio n. 9
0
void test_length()
{
  tjpeg_buffer_t *b = tjpeg_buffer_new();
  int             l = 0;

  printf("test_length\n");

  while (1) {
    int r = _rand(1, 16);

    printf("expected length: %d\n", l / 8);
    printf("expected bit length: %d\n", l);

    assert(tjpeg_buffer_get_length(b) == l / 8);

    l += r;

    if (l <= TJPEG_BUFFER_SIZE * 8)
      tjpeg_buffer_add(b, _rand(0x0000, 0xffff) & ((1 << r) - 1), r);
    else
      break;
  }
}
Esempio n. 10
0
Task * 
getNextTaskLottery(TaskQueue * queue){
	int qty=0;
	Task * itr;
	for(itr=queue->head; itr != NULL && queue->tail;itr=itr->next)
		qty += itr->priority ;

	int ticket = (int)(((double)(((double)_rand())/((double)__MAXRAND)))*(double)qty);
//	printf("MR: %d, Qty: %d, Ticket: %d, rand: %d\n", __MAXRAND, qty, ticket, _rand());
	int i=0, tmp;
	for(itr=queue->head; itr != NULL && queue->tail;itr=itr->next)
		for(tmp=0;tmp<itr->priority;tmp++,i++)
			if(i == ticket){
				// Este se agrega				
				return itr;
			}
	return NULL;
}
Esempio n. 11
0
File: map.c Progetto: psachin/dan64
int
main()
{
	register uint8_t r;
	char buffer[2] = { 0, 0 };

	clrscr();

	while(1)
	{
		// module is slow, using AND instead
		r = _rand() & 3;
		// cputs does move the cursor
		buffer[0] = 176 + (r > 2 ? 1 : r);
		cputs(buffer);
	}

	return 0;
}
Esempio n. 12
0
static void _send_nbr_adv(kernel_pid_t iface, ng_ipv6_addr_t *tgt,
                          ng_ipv6_addr_t *dst, bool supply_tl2a)
{
    ng_pktsnip_t *hdr, *pkt = NULL;
    uint8_t adv_flags = 0;

    DEBUG("ndp: send neighbor advertisement (iface: %" PRIkernel_pid ", tgt: %s, ",
          iface, ng_ipv6_addr_to_str(addr_str, tgt, sizeof(addr_str)));
    DEBUG("dst: %s, supply_tl2a: %d)\n",
          ng_ipv6_addr_to_str(addr_str, dst, sizeof(addr_str)), supply_tl2a);

    if (ng_ipv6_netif_get(iface)->flags & NG_IPV6_NETIF_FLAGS_ROUTER) {
        adv_flags |= NG_NDP_NBR_ADV_FLAGS_R;
    }

    if (ng_ipv6_addr_is_unspecified(dst)) {
        ng_ipv6_addr_set_all_nodes_multicast(dst,
                                             NG_IPV6_ADDR_MCAST_SCP_LINK_LOCAL);
    }
    else {
        adv_flags |= NG_NDP_NBR_ADV_FLAGS_S;
    }

    if (supply_tl2a) {
        uint8_t l2src[8];
        uint16_t l2src_len;
        /* we previously checked if we are the target, so we can take our L2src */
        l2src_len = _get_l2src(l2src, sizeof(l2src), iface);

        if (l2src_len > 0) {
            /* add target address link-layer address option */
            pkt = ng_ndp_opt_tl2a_build(l2src, l2src_len, NULL);

            if (pkt == NULL) {
                DEBUG("ndp: error allocating Target Link-layer address option.\n");
                ng_pktbuf_release(pkt);
                return;
            }
        }
    }

    /* TODO: also check if the node provides proxy servies for tgt */
    if ((pkt != NULL) && !ng_ipv6_netif_addr_is_non_unicast(tgt)) {
        /* TL2A is not supplied and tgt is not anycast */
        adv_flags |= NG_NDP_NBR_ADV_FLAGS_O;
    }

    hdr = ng_ndp_nbr_adv_build(adv_flags, tgt, pkt);

    if (hdr == NULL) {
        DEBUG("ndp: error allocating Neighbor advertisement.\n");
        ng_pktbuf_release(pkt);
        return;
    }

    pkt = hdr;
    hdr = ng_ipv6_hdr_build(pkt, NULL, 0, (uint8_t *)dst,
                            sizeof(ng_ipv6_addr_t));

    if (hdr == NULL) {
        DEBUG("ndp: error allocating IPv6 header.\n");
        ng_pktbuf_release(pkt);
        return;
    }

    ((ng_ipv6_hdr_t *)hdr->data)->hl = 255;

    pkt = hdr;
    /* add netif header for send interface specification */
    hdr = ng_netif_hdr_build(NULL, 0, NULL, 0);

    if (hdr == NULL) {
        DEBUG("ndp: error allocating netif header.\n");
        return;
    }

    ((ng_netif_hdr_t *)hdr->data)->if_pid = iface;

    LL_PREPEND(pkt, hdr);

    if (ng_ipv6_netif_addr_is_non_unicast(tgt)) {
        /* avoid collision for anycast addresses
         * (see https://tools.ietf.org/html/rfc4861#section-7.2.7) */
        timex_t delay = { _rand(0, NG_NDP_MAX_AC_TGT_DELAY), 0 };
        ng_ipv6_nc_t *nc_entry = ng_ipv6_nc_get(iface, tgt);
        DEBUG("ndp: delay neighbor advertisement for %" PRIu32 " sec.",
              delay.seconds);

        /* nc_entry must be set so no need to check it */
        _send_delayed(&nc_entry->nbr_adv_timer, delay, pkt);
    }
    else {
        ng_netapi_send(ng_ipv6_pid, pkt);
    }
}
Esempio n. 13
0
	float VoxelWorldGeneratorImpl::_fractal_rand(float v)
	{
		return _rand(-v, v);
	}
Esempio n. 14
0
int	Build2DTextures( IntroProgressDelegate& _Delegate )
{
return 0;

	DrawUtils	Draw;
	{
		TextureBuilder	TB( 512, 512 );
		Draw.SetupSurface( 512, 512, TB.GetMips()[0] );	// Let's draw into the first mip level !

/* General tests for drawing tools and filtering
 		Noise	N( 1 );
		N.Create2DWaveletNoiseTile( 6 );	// If you need to use wavelet noise...
		TB.Fill( FillNoise, &N );

		Draw.DrawLine( 20.0f, 0.0f, 400.0f, 500.0f, 10.0f, FillLine, NULL );

		Draw.SetupContext( 30.0f, 0.0f, 20.0f );
 		Draw.DrawEllipse( 10.0f, 13.4f, 497.39f, 282.78f, 40.0f, 0.0f, FillEllipse, NULL );

		Draw.SetupContext( 250.0f, 0.0f, 30.0f );
 		Draw.DrawRectangle( 10.0f, 13.4f, 197.39f, 382.78f, 40.0f, 0.5f, FillRectangle, NULL );

//		Filters::BlurGaussian( TB, 20.0f, 20.0f, true, 0.5f );
//		Filters::UnsharpMask( TB, 20.0f );
//		Filters::BrightnessContrastGamma( TB, 0.2f, 0.0f, 2.0f );
//		Filters::Emboss( TB, NjFloat2( 1, 1 ), 4.0f );
//		Filters::Erode( TB );
//		Filters::Dilate( TB );

// 		// Test the AO converter
// 		TextureBuilder	PipoTemp( TB.GetWidth(), TB.GetHeight() );
// 		PipoTemp.CopyFrom( TB );
// 		Generators::ComputeAO( PipoTemp, TB, 2.0f );

//*/

// 		// Test the dirtyness generator
//		Generators::Dirtyness( TB, N, 0.5f, 0.0f, 0.1f, 0.3f, 0.01f );

	// Test the secret marble generator
//		Generators::Marble( TB, 30, 0.2f, 0.5f, 1.0f, 0.5f, 0.5f );
//		Generators::Marble3D( TB, 16, 0.2f );


//* Test advanced drawing

 		Noise	N( 1 );
		// Draw scratches
		for ( int i=0; i < 10; i++ )
		{
			NjFloat2	Pos;
			Pos.x = _frand( 0.0f, 512.0f );
			Pos.y = _frand( 0.0f, 512.0f );
			NjFloat2	Dir;
			Dir.x = _frand( -1.0f, +1.0f );
			Dir.y = _frand( -1.0f, +1.0f );

			float	Length = _frand( 100.0f, 500.0f );
			float	Thickness = _frand( 4.0f, 5.0f );
			float	Curve = _frand( -0.05f, 0.05f );

			Draw.DrawScratch( Pos, Dir, Length, Thickness, 0.01f, Curve, 10.0f, FillScratch, &N );
		}

		// Draw splotches
		for ( int Y=0; Y < 20; Y++ )
		{
			int		Count = _rand( 10, 20 );

			float	Angle = _frand( -180.0f, +180.0f );
			float	DeltaAngle = _frand( 0.0f, 40.0f );		// Splotches rotate with time
					DeltaAngle /= Count;

			NjFloat2	Pos;
			Pos.x = _frand( -512.0f, 512.0f );
			Pos.y = _frand( -512.0f, 512.0f );

			NjFloat2	Size;
			Size.x = _frand( 40.0f, 50.0f );
			Size.y = _frand( 40.0f, 50.0f );
			float	DeltaSize = _frand( 0.0f, 30.0f );	// Splotches get bigger or smaller with time
					DeltaSize /= Count;

			float	Step = _frand( 0.25f, 1.0f ) * Size.x;	// Splotches interpenetrate

			for ( int X=0; X < Count; X++ )
			{
				Draw.SetupTransform( Pos.x, Pos.y, Angle );
				Draw.DrawRectangle( 0.0f, 0.0f, Size.x, Size.y, 0.1f * Size.Min(), 0.0f, FillSplotch, &N );

				Angle += DeltaAngle;
				float	AngleRad = DEG2RAD( Angle );
				Pos = Pos + Step * NjFloat2( cosf(AngleRad), sinf(AngleRad) );
				Size.x += DeltaSize;
			}
		}

// Draw.DrawRectangle( 0.0f, 0.0f, 100.0f, 100.0f, 10.0f, 0.0f, FillSplotch, &N );

//			Filters::BrightnessContrastGamma( TB, 0.1f, 0.7f, 2.0f );
//			Filters::Erode( TB, 3 );
//*/

		gs_pTexTestNoise = TB.CreateTexture( PixelFormatRGBA16F::DESCRIPTOR, TextureBuilder::CONV_RGBA_NxNyHR_M );
	}

	return 0;	// OK !
}
Esempio n. 15
0
World::Box& World::addRandomBox(const glm::vec3& pos) {
  glm::vec3 color(_rand(_mt), _rand(_mt), _rand(_mt));
  return addBox(btVector3(pos.x, pos.y, pos.z), _rand(_mt) * btRadians(90.0f),
                0, 0, color);
}
Esempio n. 16
0
int rand(void) {
    return _rand();
}