예제 #1
0
void LMS::pick_line(                //fit sample
                    float &line_m,  //output gradient
                    float &line_c) {
  inT16 trial_count;             //no of attempts
  static uinT16 seeds[3] = { SEED1, SEED2, SEED3 };
  //for nrand
  inT32 index1;                  //picked point
  inT32 index2;                  //picked point

  trial_count = 0;
  do {
    index1 = (inT32) nrand48 (seeds) % samplecount;
    index2 = (inT32) nrand48 (seeds) % samplecount;
    line_m = samples[index2].x () - samples[index1].x ();
    trial_count++;
  }
  while (line_m == 0 && trial_count < LMS_MAX_FAILURES);
  if (line_m == 0) {
    line_c = (samples[index2].y () + samples[index1].y ()) / 2;
  }
  else {
    line_m = (samples[index2].y () - samples[index1].y ()) / line_m;
    line_c = samples[index1].y () - samples[index1].x () * line_m;
  }
}
예제 #2
0
void LMS::constrained_fit(                //fit sample
                          float fixed_m,  //forced gradient
                          float &out_c) {
  inT32 index;                   //of median
  inT32 trials;                  //no of medians
  float test_c;                  //candidate line
  static uinT16 seeds[3] = { SEED1, SEED2, SEED3 };
  //for nrand
  float test_error;              //error of test line

  m = fixed_m;
  switch (samplecount) {
    case 0:
      c = 0.0f;
      line_error = 0.0f;
      break;

    case 1:
                                 //horiz thru pt
      c = samples[0].y () - m * samples[0].x ();
      line_error = 0.0f;
      break;

    case 2:
      c = (samples[0].y () + samples[1].y ()
        - m * (samples[0].x () + samples[1].x ())) / 2;
      line_error = m * samples[0].x () + c - samples[0].y ();
      line_error *= line_error;
      break;

    default:
      index = (inT32) nrand48 (seeds) % samplecount;
                                 //compute line
      c = samples[index].y () - m * samples[index].x ();
      compute_errors(m, c);  //from given line
      index = choose_nth_item (samplecount / 2, errors, samplecount);
      line_error = errors[index];
      for (trials = 1; trials < lms_line_trials; trials++) {
        index = (inT32) nrand48 (seeds) % samplecount;
        test_c = samples[index].y () - m * samples[index].x ();
        //compute line
        compute_errors(m, test_c);
        index = choose_nth_item (samplecount / 2, errors, samplecount);
        test_error = errors[index];
        if (test_error < line_error) {
                                 //find least median
          line_error = test_error;
          c = test_c;
        }
      }
  }
  fitted = TRUE;
  out_c = c;
  a = 0;
}
예제 #3
0
long int BlobCache::blob_random() {
#ifdef _WIN32
    return rand();
#else
    return nrand48(mRandState);
#endif
}
long LLW_compute_table_chunk(struct TrainingCache *cache, const struct Model *model)
{
	long i,j,random_num,row;
	const long nb_data = model->nb_data;
	const long chunk_size = cache->chunk_size;
	
	long nb_SV = 0;

	for(i=1; i<=nb_data; i++)
	  {
	  cache->out_of_chunk[i] = i;
	  cache->in_chunk[i] = 0;
	  if(cache->activeset[i])
	  	nb_SV++;
	  }	
	  
	for(i=1; i<=chunk_size; i++)
	  {
	  random_num = nrand48(xi);
	  row = (random_num % (nb_data-i+1))+1;
	  cache->table_chunk[i] = cache->out_of_chunk[row];
	  for(j=row; j<=nb_data-i; j++)
	    cache->out_of_chunk[j] = cache->out_of_chunk[j+1];
	  cache->in_chunk[cache->table_chunk[i]] = 1;
	  }
	  
	  return nb_SV;
}
예제 #5
0
void LMS::pick_quadratic(                 //fit sample
                         double &line_a,  //x suaread
                         float &line_m,   //output gradient
                         float &line_c) {
  inT16 trial_count;             //no of attempts
  static uinT16 seeds[3] = { SEED1, SEED2, SEED3 };
  //for nrand
  inT32 index1;                  //picked point
  inT32 index2;                  //picked point
  inT32 index3;
  FCOORD x1x2;                   //vector
  FCOORD x1x3;
  FCOORD x3x2;
  double bottom;                 //of a

  trial_count = 0;
  do {
    if (trial_count >= LMS_MAX_FAILURES - 1) {
      index1 = 0;
      index2 = samplecount / 2;
      index3 = samplecount - 1;
    }
    else {
      index1 = (inT32) nrand48 (seeds) % samplecount;
      index2 = (inT32) nrand48 (seeds) % samplecount;
      index3 = (inT32) nrand48 (seeds) % samplecount;
    }
    x1x2 = samples[index2] - samples[index1];
    x1x3 = samples[index3] - samples[index1];
    x3x2 = samples[index2] - samples[index3];
    bottom = x1x2.x () * x1x3.x () * x3x2.x ();
    trial_count++;
  }
  while (bottom == 0 && trial_count < LMS_MAX_FAILURES);
  if (bottom == 0) {
    line_a = 0;
    pick_line(line_m, line_c);
  }
  else {
    line_a = x1x3 * x1x2 / bottom;
    line_m = x1x2.y () - line_a * x1x2.x ()
      * (samples[index2].x () + samples[index1].x ());
    line_m /= x1x2.x ();
    line_c = samples[index1].y () - samples[index1].x ()
      * (samples[index1].x () * line_a + line_m);
  }
}
예제 #6
0
/**
 * PRNG uniformly distributed between 0 and 2^32 - 1.
 *
 * @note Contrary to POSIX lrand48(), this function is thread-safe.
 * @warning Series generated by this function are not reproducible.
 * Use nrand48() if you need reproducible series.
 *
 * @return an integral value within [0.0, 2^32-1] inclusive
 */
long vlc_lrand48 (void)
{
    long ret;

    vlc_mutex_lock (&rand48.lock);
    init_rand48 ();
    ret = nrand48 (rand48.subi);
    vlc_mutex_unlock (&rand48.lock);
    return ret;
}
예제 #7
0
int get_random_number(void)
{
    //nrand48 to get random number
    struct timeval randtime;
    gettimeofday(&randtime,(struct timezone *) 0);
    unsigned short xsub1[3];
    xsub1[0] = (ushort)randtime.tv_usec;
    xsub1[1] = (ushort)(randtime.tv_usec >> 16);
    xsub1[2] = (ushort)(getpid());
    return nrand48(xsub1) & 3;
}
예제 #8
0
파일: SFMT.c 프로젝트: hitchiker42/SciLisp
void sfmt_init_stable_r(sfmt_t *sfmt){
  uint32_t init_array[SFMT_N32];
  struct timeval tp;
  union lcg_state lcg_init;
  lcg_init.X_i=time_seed();
  int i;
  for(i=0;i<SFMT_N32;i++){
    init_array[i]=nrand48(lcg_init.state);
  }
  sfmt_init_by_array(sfmt,init_array,SFMT_N32);
}
예제 #9
0
파일: buf_oper.c 프로젝트: mikey/HTX
bldbuf(unsigned short *wbuf, unsigned int dlen, int buf_size,
       char *pattern_id, int *blkno)
{
   int            i, j = 1, k, dlen_static, a, b, c;
   long           nrand48();
   unsigned short xsubi[3];

   dlen_static = dlen;
   init_seed(xsubi);
   a = xsubi[0];
   b = xsubi[1];
   c = xsubi[2];
   dlen = dlen / 512;
   i = 0;
   if ( (strcmp(pattern_id, "#001") == 0) ||
        (strcmp(pattern_id, "#002") == 0) ) {
      for ( ; dlen != 0; --dlen, j++ ) {
         wbuf[i]   = blkno[0] >> 16;
         wbuf[i+1] = blkno[0];
         wbuf[i+2] = a;
         wbuf[i+3] = b;
         wbuf[i+4] = c;
         wbuf[i+5] = nrand48(xsubi) % 65536;
         for ( k = 6; k <= 256; ) {
            wbuf[i+k] = (i + k) / 2;
            if ( strcmp(pattern_id, "#001") == 0 )
               wbuf[i+k+1] = j * 0x101;
            else
               wbuf[i+k+1] = nrand48(xsubi) % 65536;
            k += 2;
         }
         i += 256;
      }
      while ( (buf_size - dlen_static) >=0 ) {
          wbuf[i] = 0xffff;
          i++;
          buf_size -= 2;
      }
   }
}
예제 #10
0
inline double urandom() {
    /* the probalility of matching will be theoretically p^3(in fact, it is not)
       p is matching probalility of random().
       using the test there, only 3 matches, using random(), 13783 matches
    */
    unsigned short state[3];
    state[0] = random();
    state[1] = random();
    state[2] = random();

    const long max_random = 2147483647; // 2**31 - 1
    return (double)nrand48(state) / (double)max_random;
}
예제 #11
0
unsigned int random_generator::get_random()
{
    int rn;
#ifdef HAVE_RANDOM_R
    int ret = random_r(&m_data_blob, &rn);//max is RAND_MAX
    assert(ret == 0);
#elif (defined HAVE_DRAND48)
    rn = nrand48(m_data_blob); // max is 1<<31
#else
    #error no random function
#endif
    return rn;
}
예제 #12
0
파일: topology.c 프로젝트: stwn/kuliax
int add_node(CnetNodetype nodetype, char *name, int defn, int *wasnew)
{
    extern int	strcasecmp(const char *, const char *);

    NODE	*np;
    int		 n;

    for(n=0 ; n<_NNODES ; n++)
	if(strcasecmp(name, NP[n].nodename) == 0) {
	    if(defn)
		NP[n].nodetype	= nodetype;	/* over-ride */
	    *wasnew		= FALSE;
	    return(n);				/* node seen before */
	}

    if(strlen(name) >= MAX_NODENAME_LEN) {
	fprintf(stderr,"%s: node name '%s' is too long (max is %d)\n",
				argv0, name, MAX_NODENAME_LEN-1);
	++nerrors;
	/* continue anyway */
    }

    NP = (NODE *)realloc((char *)NP, (unsigned)(_NNODES+1)*sizeof(NODE));
    np = &(NP[_NNODES]);

    memset((char *)np, 0, sizeof(NODE));
    np->nodetype		= nodetype;
    np->nodename		= strdup(name);
    np->nlinks			= 0;
    np->links			= NEW(int);
    np->runstate		= STATE_REBOOTING;

#if	(NODE_CLOCK_SKEW == 0)
    np->clock_skew	= int64_ZERO;
#else
    if(cflag)
	np->clock_skew	= int64_ZERO;
    else
	int64_I2L(np->clock_skew,
		  ((int)nrand48(xsubi)%(2*NODE_CLOCK_SKEW) - NODE_CLOCK_SKEW));
#endif

    init_nodeattrs(&(np->nattr));
    np->nattr.address		= (CnetAddr)(DEFAULTNODE.address + _NNODES);
    init_linkattrs(&(np->lattr));

    *wasnew			= TRUE;
    return( _NNODES++ );
}
예제 #13
0
파일: ccnr_sendq.c 프로젝트: Emat12/ccnx
static unsigned
randomize_content_delay(struct ccnr_handle *h, struct content_queue *q)
{
    unsigned usec;
    
    usec = q->min_usec + q->rand_usec;
    if (usec < 2)
        return(1);
    if (usec <= 20 || q->rand_usec < 2) // XXX - what is a good value for this?
        return(usec); /* small value, don't bother to randomize */
    usec = q->min_usec + (nrand48(h->seed) % q->rand_usec);
    if (usec < 2)
        return(1);
    return(usec);
}
예제 #14
0
파일: main.c 프로젝트: alk/shm_pipe
static
void *reader_thread(void *dummy)
{
	struct fifo_window window;
	unsigned long long count=0;
	int sum=0;
	unsigned short xsubi[3];
	memset(xsubi, 0, sizeof(xsubi));

	printf("reader's pid is %d\n", gettid());
	if (setaffinity)
		move_to_cpu(0);

	if (serialize)
		sem_wait(&reader_sem);

	fifo_window_init_reader(fifo, &window, 0, READER_BATCH*sizeof(int)*2);
	while (1) {
		int *ptr;
		unsigned len, i;

		if (serialize) {
			sem_post(&writer_sem);
			sem_wait(&reader_sem);
		}

		fifo_window_exchange_reader(&window);

		if (window.len == 0) {
			if (done_flag)
				break;
			fifo_window_reader_wait(&window);
			continue;
		}

		ptr = fifo_window_peek_span(&window, &len);
		len /= sizeof(int);
		if (len > READER_BATCH)
			len = READER_BATCH;
		fifo_window_eat_span(&window, len*sizeof(int));

		for (i = 0; i < len; i++)
			sum |= *ptr++ ^ nrand48(xsubi);
		count += i;
	}
	printf("sum = 0x%08x\ncount = %lld\n", sum, count);
	return (void *)(intptr_t)sum;
}
예제 #15
0
void test_rand()
{
  unsigned short a[7];
  unsigned b;
  
  rand();	// no-warning
  drand48();	// no-warning
  erand48(a);	// no-warning
  jrand48(a);	// no-warning
  lcong48(a);	// no-warning
  lrand48();	// no-warning
  mrand48();	// no-warning
  nrand48(a);	// no-warning
  rand_r(&b);	// no-warning
  random();	// no-warning
}
예제 #16
0
파일: main.c 프로젝트: alk/shm_pipe
static
void *writer_thread(void *dummy)
{
	struct fifo_window window;
	unsigned count = 0;
	unsigned short xsubi[3];
	memset(xsubi, 0, sizeof(xsubi));

	printf("writer's pid is %d\n", gettid());
	if (setaffinity)
		move_to_cpu(1);

	if (serialize)
		sem_wait(&writer_sem);

	fifo_window_init_writer(fifo, &window, 4, WRITER_BATCH*sizeof(int)*2);
	while (count < SEND_WORDS) {
		int *ptr;
		unsigned len, i;
		fifo_window_exchange_writer(&window);

		if (serialize) {
			sem_post(&reader_sem);
			sem_wait(&writer_sem);
		}

		ptr = fifo_window_peek_span(&window, &len);
		len /= sizeof(int);
		if (len > WRITER_BATCH)
			len = WRITER_BATCH;
		count += len;
		if (count > SEND_WORDS) {
			count -= len;
			len = SEND_WORDS - count;
			count += len;
		}
		fifo_window_eat_span(&window, len*sizeof(int));
		for (i=0;i<len;i++)
			*ptr++ = nrand48(xsubi);
	}
	fprintf(stderr, "writer count %u\n", count);
	done_flag = 1;
	fifo_window_exchange_writer(&window);
	if (serialize)
		sem_post(&reader_sem);
	return 0;
}
예제 #17
0
static int test_mmio_read(struct hwtest_ctx *ctx) {
	int i;
	for (i = 0; i < 10000; i++) {
		struct nv01_pgraph_state exp, real;
		nv01_pgraph_gen_state(ctx, &exp);
		int idx = nrand48(ctx->rand48) % ARRAY_SIZE(nv01_pgraph_state_regs);
		uint32_t reg = nv01_pgraph_state_regs[idx];
		nv01_pgraph_load_state(ctx, &exp);
		nva_rd32(ctx->cnum, reg);
		if ((reg & ~0xf) == 0x400460) {
			exp.xy_misc_1 &= ~0xfff000;
		}
		nv01_pgraph_dump_state(ctx, &real);
		if (nv01_pgraph_cmp_state(&exp, &real)) {
			printf("After reading %08x\n", reg);
			return HWTEST_RES_FAIL;
		}
	}
	return HWTEST_RES_PASS;
}
예제 #18
0
static int test_mmio_write(struct hwtest_ctx *ctx) {
	int i;
	for (i = 0; i < 10000; i++) {
		struct nv01_pgraph_state exp, real;
		nv01_pgraph_gen_state(ctx, &exp);
		int idx = nrand48(ctx->rand48) % ARRAY_SIZE(nv01_pgraph_state_regs);
		uint32_t reg = nv01_pgraph_state_regs[idx];
		uint32_t val = ((uint32_t*)&exp)[idx];
		nv01_pgraph_load_state(ctx, &exp);
		nva_wr32(ctx->cnum, reg, val);
		if (reg == 0x400180) {
			exp.debug[1] &= ~1;
			exp.ctx_control &= ~0x01000000;
		}
		if ((reg & ~0xff) == 0x400400) {
			if ((reg & 0x7f) < 0x50) {
				int xy = reg >> 7 & 1;
				int idx = reg >> 2 & 0x1f;
				nv01_pgraph_vtx_fixup(&exp, xy, idx, val, 0);
			} else if (reg < 0x400460) {
				int xy = reg >> 2 & 1;
				nv01_pgraph_iclip_fixup(&exp, xy, val, 0);
			} else {
예제 #19
0
int
main (void)
{
  time_t t = time (NULL);
  int i, ret = 0;
  double d;
  long int l;
  struct drand48_data data;
  unsigned short int buf[3];

  srand48 ((long int) t);
  for (i = 0; i < 50; i++)
    if ((d = drand48 ()) < 0.0 || d >= 1.0)
      {
        printf ("drand48 %d %g\n", i, d);
        ret = 1;
      }

  srand48_r ((long int) t, &data);
  for (i = 0; i < 50; i++)
    if (drand48_r (&data, &d) != 0 || d < 0.0 || d >= 1.0)
      {
        printf ("drand48_r %d %g\n", i, d);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if ((d = erand48 (buf)) < 0.0 || d >= 1.0)
      {
        printf ("erand48 %d %g\n", i, d);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if (erand48_r (buf, &data, &d) != 0 || d < 0.0 || d >= 1.0)
      {
        printf ("erand48_r %d %g\n", i, d);
        ret = 1;
      }

  srand48 ((long int) t);
  for (i = 0; i < 50; i++)
    if ((l = lrand48 ()) < 0 || l > INT32_MAX)
      {
        printf ("lrand48 %d %ld\n", i, l);
        ret = 1;
      }

  srand48_r ((long int) t, &data);
  for (i = 0; i < 50; i++)
    if (lrand48_r (&data, &l) != 0 || l < 0 || l > INT32_MAX)
      {
        printf ("lrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if ((l = nrand48 (buf)) < 0 || l > INT32_MAX)
      {
        printf ("nrand48 %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if (nrand48_r (buf, &data, &l) != 0 || l < 0 || l > INT32_MAX)
      {
        printf ("nrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  srand48 ((long int) t);
  for (i = 0; i < 50; i++)
    if ((l = mrand48 ()) < INT32_MIN || l > INT32_MAX)
      {
        printf ("mrand48 %d %ld\n", i, l);
        ret = 1;
      }

  srand48_r ((long int) t, &data);
  for (i = 0; i < 50; i++)
    if (mrand48_r (&data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
      {
        printf ("mrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if ((l = jrand48 (buf)) < INT32_MIN || l > INT32_MAX)
      {
        printf ("jrand48 %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if (jrand48_r (buf, &data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
      {
        printf ("jrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  return ret;
}
예제 #20
0
ATF_TC_BODY(tsearch_test, tc)
{
	/*
	 * Run the test below in a deterministic fashion to prevent this
	 * test from potentially flapping. We assume that this provides
	 * enough coverage.
	 */
#if 0
	unsigned short random_state[3];
	arc4random_buf(random_state, sizeof(random_state));
#else
	unsigned short random_state[3] = { 26554, 13330, 3246 };
#endif

#define NKEYS 1000
	/* Create 1000 possible keys. */
	int keys[NKEYS];
	for (int i = 0; i < NKEYS; ++i)
		keys[i] = i;

	/* Apply random operations on a binary tree and check the results. */
	posix_tnode *root = NULL;
	bool present[NKEYS] = {};
	for (int i = 0; i < NKEYS * 10; ++i) {
		int key = nrand48(random_state) % NKEYS;
		switch (nrand48(random_state) % 3) {
		case 0:  /* tdelete(). */
			if (present[key]) {
				ATF_CHECK(tdelete(&key, &root, compar) != NULL);
				present[key] = false;
			} else {
				ATF_CHECK_EQ(NULL,
				    tdelete(&key, &root, compar));
			}
			break;
		case 1:  /* tfind(). */
			if (present[key]) {
				ATF_CHECK_EQ(&keys[key],
				    *(int **)tfind(&key, &root, compar));
			} else {
				ATF_CHECK_EQ(NULL, tfind(&key, &root, compar));
			}
			break;
		case 2:  /* tsearch(). */
			if (present[key]) {
				ATF_CHECK_EQ(&keys[key],
				    *(int **)tsearch(&key, &root, compar));
			} else {
				ATF_CHECK_EQ(&keys[key], *(int **)tsearch(
				    &keys[key], &root, compar));
				present[key] = true;
			}
			break;
		}
		tnode_assert(root);
	}

	/* Remove all entries from the tree. */
	for (int key = 0; key < NKEYS; ++key)
		if (present[key])
			ATF_CHECK(tdelete(&key, &root, compar) != NULL);
	ATF_CHECK_EQ(NULL, root);
}
예제 #21
0
void test06 ( void )

/******************************************************************************/
/*
  Purpose:

    TEST06 tests NRAND48.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    18 August 2008

  Author:

    John Burkardt
*/
{
  int i;
  int modulus = 65536;
  long long int seed;
  unsigned short int seedvec[3];
  long int value;

  printf ( "\n" );
  printf ( "TEST06\n" );
  printf ( "  NRAND48 returns nonnegative long integers in [0,+2^31].\n" );
  printf ( "  The 48 bit seed is an explicit argument of 3 16 bit values.\n" );

  seed = 123456789LL;
  seedvec[0] = seed % modulus;
  seed = seed / modulus;
  seedvec[1] = seed % modulus;
  seed = seed / modulus;
  seedvec[2] = seed % modulus;
  seed = seed / modulus;

  printf ( "\n" );
  printf ( "  The initial seed is %d\n", seed );
  printf ( "  The seed vector is %d, %d, %d\n", seedvec[0], seedvec[1], seedvec[2] );
  printf ( "\n" );

  for ( i = 1; i <= 10; i++ )
  {
    value = nrand48 ( seedvec );

    printf ( "  %6d  %12d\n", i, value );
  }

  seed = 987654321LL;
  seedvec[0] = seed % modulus;
  seed = seed / modulus;
  seedvec[1] = seed % modulus;
  seed = seed / modulus;
  seedvec[2] = seed % modulus;
  seed = seed / modulus;

  printf ( "\n" );
  printf ( "  The initial seed is %d\n", seed );
  printf ( "  The seed vector is %d, %d, %d\n", seedvec[0], seedvec[1], seedvec[2] );
  printf ( "\n" );


  for ( i = 1; i <= 10; i++ )
  {
    value = nrand48 ( seedvec );

    printf ( "  %6d  %12d\n", i, value );
  }

  seed = 123456789LL;
  seedvec[0] = seed % modulus;
  seed = seed / modulus;
  seedvec[1] = seed % modulus;
  seed = seed / modulus;
  seedvec[2] = seed % modulus;
  seed = seed / modulus;

  printf ( "\n" );
  printf ( "  The initial seed is %d\n", seed );
  printf ( "  The seed vector is %d, %d, %d\n", seedvec[0], seedvec[1], seedvec[2] );
  printf ( "\n" );


  for ( i = 1; i <= 10; i++ )
  {
    value = nrand48 ( seedvec );

    printf ( "  %6d  %12d\n", i, value );
  }
  return;
}
예제 #22
0
파일: tst-rand48.c 프로젝트: AubrCool/glibc
static int
do_test (void)
{
  unsigned short int xs[3] = { 0x0001, 0x0012, 0x0123 };
  unsigned short int lxs[7];
  unsigned short int *xsp;
  int result = 0;
  long int l;
  double d;
  double e;

  /* Test srand48.  */
  srand48 (0x98765432);
  /* Get the values of the internal Xi array.  */
  xsp = seed48 (xs);
  if (xsp[0] != 0x330e || xsp[1] != 0x5432 || xsp[2] != 0x9876)
    {
      puts ("srand48(0x98765432) didn't set correct value");
      printf ("  expected: { %04hx, %04hx, %04hx }\n", 0x330e, 0x5432, 0x9876);
      printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
      result = 1;
    }
  /* Put the values back.  */
  memcpy (xs, xsp, sizeof (xs));
  (void) seed48 (xs);

  /* See whether the correct values are installed.  */
  l = lrand48 ();
  if (l != 0x2fed1413l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x2fed1413l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != -0x5d73effdl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, -0x5d73effdl, l);
      result = 1;
    }

  l = lrand48 ();
  if (l != 0x585fcfb7l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x585fcfb7l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != -0x61770b8cl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, -0x61770b8cl, l);
      result = 1;
    }

  /* Test seed48.  The previous call should have install the values in
     the initialization of `xs' above.  */
  xs[0] = 0x1234;
  xs[1] = 0x5678;
  xs[2] = 0x9012;
  xsp = seed48 (xs);
  if (xsp[0] != 0x62f2 || xsp[1] != 0xf474 || xsp[2] != 0x9e88)
    {
      puts ("seed48() did not install the values correctly");
      printf ("  expected: { %04hx, %04hx, %04hx }\n", 0x62f2, 0xf474, 0x9e88);
      printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
      result = 1;
    }

  /* Test lrand48 and mrand48.  We continue from the seed established
     above.  */
  l = lrand48 ();
  if (l != 0x017e48b5l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x017e48b5l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != -0x1485e05dl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, -0x1485e05dl, l);
      result = 1;
    }

  l = lrand48 ();
  if (l != 0x6b6a3f95l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x6b6a3f95l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != 0x175c0d6fl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x175c0d6fl, l);
      result = 1;
    }

  /* Test lcong48.  */
  lxs[0] = 0x4567;
  lxs[1] = 0x6789;
  lxs[2] = 0x8901;
  lxs[3] = 0x0123;
  lxs[4] = 0x2345;
  lxs[5] = 0x1111;
  lxs[6] = 0x2222;
  lcong48 (lxs);

  /* See whether the correct values are installed.  */
  l = lrand48 ();
  if (l != 0x6df63d66l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x6df63d66l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != 0x2f92c8e1l)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x2f92c8e1l, l);
      result = 1;
    }

  l = lrand48 ();
  if (l != 0x3b4869ffl)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x3b4869ffl, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != 0x5cd4cc3el)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x5cd4cc3el, l);
      result = 1;
    }

  /* Check whether srand48() restores the A and C parameters.  */
  srand48 (0x98765432);

  /* See whether the correct values are installed.  */
  l = lrand48 ();
  if (l != 0x2fed1413l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x2fed1413l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != -0x5d73effdl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, -0x5d73effdl, l);
      result = 1;
    }

  l = lrand48 ();
  if (l != 0x585fcfb7l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x585fcfb7l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != -0x61770b8cl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, -0x61770b8cl, l);
      result = 1;
    }

  /* And again to see whether seed48() does the same.  */
  lcong48 (lxs);

  /* See whether lxs wasn't modified.  */
  l = lrand48 ();
  if (l != 0x6df63d66l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x6df63d66l, l);
      result = 1;
    }

  /* Test seed48.  The previous call should have install the values in
     the initialization of `xs' above.  */
  xs[0] = 0x1234;
  xs[1] = 0x5678;
  xs[2] = 0x9012;
  xsp = seed48 (xs);
  if (xsp[0] != 0x0637 || xsp[1] != 0x7acd || xsp[2] != 0xdbec)
    {
      puts ("seed48() did not install the values correctly");
      printf ("  expected: { %04hx, %04hx, %04hx }\n", 0x0637, 0x7acd, 0xdbec);
      printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
      result = 1;
    }

  /* Test lrand48 and mrand48.  We continue from the seed established
     above.  */
  l = lrand48 ();
  if (l != 0x017e48b5l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x017e48b5l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != -0x1485e05dl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, -0x1485e05dl, l);
      result = 1;
    }

  l = lrand48 ();
  if (l != 0x6b6a3f95l)
    {
      printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x6b6a3f95l, l);
      result = 1;
    }

  l = mrand48 ();
  if (l != 0x175c0d6fl)
    {
      printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x175c0d6fl, l);
      result = 1;
    }

  /* Test drand48.  */
  d = drand48 ();
  if (d != 0.0908832261858485424)
    {
      printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
	      __LINE__ - 4, DECIMAL_DIG, 0.0908832261858485424,
	      DECIMAL_DIG, d);
      result = 1;
    }

  d = drand48 ();
  if (d != 0.943149381730059133133)
    {
      printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
	      __LINE__ - 4, DECIMAL_DIG, 0.943149381730059133133,
	      DECIMAL_DIG, d);
      result = 1;
    }

  /* Now the functions which get the Xis passed.  */
  xs[0] = 0x3849;
  xs[1] = 0x5061;
  xs[2] = 0x7283;

  l = nrand48 (xs);
  if (l != 0x1efe61a1l)
    {
      printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x1efe61a1l, l);
      result = 1;
    }

  l = jrand48 (xs);
  if (l != -0xa973860l)
    {
      printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, -0xa973860l, l);
      result = 1;
    }

  l = nrand48 (xs);
  if (l != 0x2a5e57fel)
    {
      printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x2a5e57fel, l);
      result = 1;
    }

  l = jrand48 (xs);
  if (l != 0x71a779a8l)
    {
      printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x71a779a8l, l);
      result = 1;
    }

  /* Test whether the global A and C are used.  */
  lcong48 (lxs);

  l = nrand48 (xs);
  if (l != 0x32beee9fl)
    {
      printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x32beee9fl, l);
      result = 1;
    }

  l = jrand48 (xs);
  if (l != 0x7bddf3bal)
    {
      printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x7bddf3bal, l);
      result = 1;
    }

  l = nrand48 (xs);
  if (l != 0x85bdf28l)
    {
      printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x85bdf28l, l);
      result = 1;
    }

  l = jrand48 (xs);
  if (l != 0x7b433e47l)
    {
      printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
	      __LINE__ - 4, 0x7b433e47l, l);
      result = 1;
    }

  /* Test erand48.  Also compare with the drand48 results.  */
  (void) seed48 (xs);

  d = drand48 ();
  e = erand48 (xs);
  if (d != e)
    {
      printf ("\
drand48() and erand48 in lines %d and %d produce different results\n",
	      __LINE__ - 6, __LINE__ - 5);
      printf ("  drand48() = %g, erand48() = %g\n", d, e);
      result = 1;
    }
  else if (e != 0.640650904452755298735)
    {
      printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
	      __LINE__ - 4, DECIMAL_DIG, 0.640650904452755298735,
	      DECIMAL_DIG, e);
      result = 1;

    }

  d = drand48 ();
  e = erand48 (xs);
  if (d != e)
    {
      printf ("\
drand48() and erand48 in lines %d and %d produce different results\n",
	      __LINE__ - 6, __LINE__ - 5);
      printf ("  drand48() = %g, erand48() = %g\n", d, e);
      result = 1;
    }
  else if (e != 0.115372323508150742555)
    {
      printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
	      __LINE__ - 4, DECIMAL_DIG, 0.0115372323508150742555,
	      DECIMAL_DIG, e);
      result = 1;

    }

  return result;
}
예제 #23
0
파일: ilrand.cpp 프로젝트: keesvp/OpenIL
double normrand( double x, double s, ush3 randval )
{
  return x + s * cuminv( (double)( nrand48( randval ) & 0x7FFF ) );
}
예제 #24
0
파일: ccnr_store.c 프로젝트: Emat12/ccnx
static int
r_store_index_cleaner(struct ccn_schedule *sched,
    void *clienth,
    struct ccn_scheduled_event *ev,
    int flags)
{
    struct ccnr_handle *h = clienth;
    struct hashtb_enumerator ee;
    struct hashtb_enumerator *e = &ee;
    struct ccn_btree_node *node = NULL;
    int k;
    int res;
    int overquota;
    
    (void)(sched);
    (void)(ev);
    if ((flags & CCN_SCHEDULE_CANCEL) != 0 ||
         h->btree == NULL || h->btree->io == NULL) {
        h->index_cleaner = NULL;
        ccn_indexbuf_destroy(&h->toclean);
        return(0);
    }
    /* First, work on cleaning the things we already know need cleaning */
    if (h->toclean != NULL) {
        for (k = 0; k < CCN_BT_CLEAN_BATCH && h->toclean->n > 0; k++) {
            node = ccn_btree_rnode(h->btree, h->toclean->buf[--h->toclean->n]);
            if (node != NULL && node->iodata != NULL) {
                res = ccn_btree_chknode(node); /* paranoia */
                if (res < 0 || CCNSHOULDLOG(h, sdfsdffd, CCNL_FINER))
                    ccnr_msg(h, "write index node %u (err %d)",
                             (unsigned)node->nodeid, node->corrupt);
                if (res >= 0) {
                    if (node->clean != node->buf->length)
                        res = h->btree->io->btwrite(h->btree->io, node);
                    if (res < 0)
                        ccnr_msg(h, "failed to write index node %u",
                                 (unsigned)node->nodeid);
                    else
                        node->clean = node->buf->length;
                }
                if (res >= 0 && node->iodata != NULL && node->activity == 0) {
                    if (CCNSHOULDLOG(h, sdfsdffd, CCNL_FINER))
                        ccnr_msg(h, "close index node %u",
                                 (unsigned)node->nodeid);
                    res = ccn_btree_close_node(h->btree, node);
                }
            }
        }
        if (h->toclean->n > 0)
            return(nrand48(h->seed) % (2U * CCN_BT_CLEAN_TICK_MICROS) + 500);
    }
    /* Sweep though and find the nodes that still need cleaning */
    overquota = 0;
    if (h->btree->nodepool >= 16)
        overquota = hashtb_n(h->btree->resident) - h->btree->nodepool;
    hashtb_start(h->btree->resident, e);
    for (node = e->data; node != NULL; node = e->data) {
        if (overquota > 0 &&
              node->activity == 0 &&
              node->iodata == NULL &&
              node->clean == node->buf->length) {
            overquota -= 1;
            if (CCNSHOULDLOG(h, sdfsdffd, CCNL_FINEST))
                ccnr_msg(h, "prune index node %u",
                         (unsigned)node->nodeid);
            hashtb_delete(e);
            continue;
        }
        node->activity /= 2; /* Age the node's activity */
        if (node->clean != node->buf->length ||
            (node->iodata != NULL && node->activity == 0)) {
            if (h->toclean == NULL) {
                h->toclean = ccn_indexbuf_create();
                if (h->toclean == NULL)
                    break;
            }
            ccn_indexbuf_append_element(h->toclean, node->nodeid);
        }
        hashtb_next(e);
    }
    hashtb_end(e);
    /* If nothing to do, shut down cleaner */
    if ((h->toclean == NULL || h->toclean->n == 0) && overquota <= 0 &&
        h->btree->io->openfds <= CCN_BT_OPEN_NODES_IDLE) {
        h->btree->cleanreq = 0;
        h->index_cleaner = NULL;
        ccn_indexbuf_destroy(&h->toclean);
        if (CCNSHOULDLOG(h, sdfsdffd, CCNL_FINE))
            ccnr_msg(h, "index btree nodes all clean");
        
        return(0);
    }
    return(nrand48(h->seed) % (2U * CCN_BT_CLEAN_TICK_MICROS) + 500);
}
예제 #25
0
파일: bbs_pseudo.c 프로젝트: cpsource/dibit
// get some bytes (taken from gmpbbs.c)
void rndbbs_randbytes(PGM_CTX *pgm_ctx, char *retbuf, size_t nbytes)
#define FUNC_NAME "rndbbs_randbytes"
{
  char *urandom_buffer = NULL;
  RNDBBS *bbs;

  // pick up handy ptr
  bbs = &pgm_ctx->rndbbs;

  if ( bbs->xor_urandom )
    {
      if ( (urandom_buffer = (char *) malloc(nbytes)) == NULL)
	{
	  perror(FUNC_NAME ": malloc");
	  return;
	}

      {
	int i;
	int j;
	unsigned int r;

	r = nrand48(pgm_ctx->xsubi);
	for ( j = 0, i = 0 ; i < nbytes ; i++ ) {
	  if ( j > 3 ) {
	    j = 0;
	    r = nrand48(pgm_ctx->xsubi);
	  }
	  urandom_buffer [ i ] = (unsigned char)r;
	  r >>= 8;
	  j += 1;
	}
      }

#if 0
      if ( _urandread(urandom_buffer, nbytes) != nbytes )
	{
	  perror(FUNC_NAME ": _urandread: continuting...");
	  bbs->xor_urandom = 0;
	}
#endif
    }

  memset(retbuf, 0, nbytes);

  if (!bbs->improved)
    {
      /* basic implementation without improvements (only keep parity) */
      {
	int i;
	for (i=0;i<nbytes;i++)
	  {
	    int j;

	    /* we keep the parity (least significant bit) of each x_n */
	    for (j=7;j>=0;j--)
	      {
		/* x[n+1] = x[n]^2 (mod blumint) */
		mpz_powm_ui(bbs->x, bbs->x, 2, bbs->blumint);

		/* mpz_fdiv_ui(bbs->x, 2) == mpz_tstbit(bbs->x, 0) */
		retbuf[i] |= (mpz_tstbit(bbs->x, 0) << j);
	      }
	    if (bbs->xor_urandom)
	      retbuf[i] ^= urandom_buffer[i];
	  }
	if ( urandom_buffer != NULL )
	  free(urandom_buffer);
	return;
      }
    }
  else
    {
      /* improved implementation (keep log2(log2(blumint)) bits of x[i]) */
      unsigned int loglogblum = log(1.0*bbs->key_bitlen)/log(2.0);

      unsigned int byte=0, bit=0, i;

      for (;;)
	{
	  //printf("%s: calculating x[n+1], loglogblum = %d\n",
	  //__FUNCTION__,loglogblum);
	  
	  /* x[n+1] = x[n]^2 (mod blumint) */
	  mpz_powm_ui(bbs->x, bbs->x, 2, bbs->blumint);

	  for (i=0;i<loglogblum;i++)
	    {
	      if (byte == nbytes)
		{
		  if ( urandom_buffer != NULL )
		    free(urandom_buffer);
		  return;
		}

	      /* get the ith bit of x */
	      retbuf[byte] |= (mpz_tstbit(bbs->x, i) << (7-bit) );

	      if (bit == 7)
		{
		  if (bbs->xor_urandom)
		    retbuf[byte] ^= urandom_buffer[byte];
		  byte++;
		  bit=0;
		}
	      else
		{
		  bit++;
		}
	    }
	}
    }
}
예제 #26
0
파일: topology.c 프로젝트: stwn/kuliax
void random_topology(int nnodes)
{
    int		i, j, n, minlinks, wasnew;
    int		x, y;
    char	*fmt;
    int		**grid;
    BOOL	**adj;

         if(nnodes <= 10 )	fmt = "host%d";
    else if(nnodes <= 100)	fmt = "host%02d";
    else			fmt = "host%03d";

    n		= (int)sqrt((double)(nnodes-1)) + 1;
    grid	= (int **)malloc(n * sizeof(int *));
    for(i=0 ; i<n ; ++i) {
	grid[i]	= (int *)malloc(n * sizeof(int));
	for(j=0 ; j<n ; ++j)
	    grid[i][j] = UNKNOWN;
    }

    adj		= (BOOL **)malloc(nnodes * sizeof(BOOL *));
    for(i=0 ; i<nnodes ; ++i) {
	adj[i]	= (BOOL *)malloc(nnodes * sizeof(BOOL));
	memset(adj[i], 0, nnodes * sizeof(BOOL));
    }

    for(i=0 ; i<nnodes ; ) {
	x = ((int)nrand48(xsubi)%n);
	y = ((int)nrand48(xsubi)%n);
	if(grid[x][y] == UNKNOWN)
	    grid[x][y] = i++;
    }

    for(i=0, j=0 ; i<nnodes; j++) {
	x	= j%n;
	y	= j/n;
	if(grid[x][y] == UNKNOWN)
	    continue;
	sprintf(chararray, fmt, i);
	add_node(NT_HOST, chararray, TRUE, &wasnew);
	NP[i].nattr.x	= (1.5*x+1) * DEF_NODE_X;
	NP[i].nattr.y	= (1.5*y+1) * DEF_NODE_Y;
	grid[x][y]	= i++;
    }

    minlinks	= (nnodes<6) ? n/2 : (3*nnodes)/2;
    for(i=0 ; i<minlinks ; i++)
	add1link(n, grid, nnodes, adj);
    i=0;
    while(!connected(nnodes, adj)) {
	add1link(n, grid, nnodes, adj);
	++i;
    }
    if(vflag)
	fprintf(stderr,"%d extra link%s required for connectivity\n", P(i));

    for(i=0 ; i<n ; ++i)
	free(grid[i]);
    free(grid);
    for(i=0 ; i<nnodes ; ++i)
	free(adj[i]);
    free(adj);
}
예제 #27
0
/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    ts_table_t *p_table = (ts_table_t *)p_this;
    ts_stream_t *p_ts_stream = p_table->p_ts_stream;
    ts_packetizer_sys_t *p_sys;
    vlc_value_t val;
    char *psz_network_name;
    mtime_t i_max_period;
    unsigned short subi[3];
    vlc_rand_bytes( subi, sizeof(subi) );

    p_sys = p_table->p_sys = malloc( sizeof(ts_packetizer_sys_t) );
    memset( p_sys, 0, sizeof(ts_packetizer_sys_t) );

    config_ChainParse( p_table, SOUT_CFG_PREFIX, ppsz_sout_options,
                   p_table->p_cfg );
    tstable_CommonOptions( p_table );

    if ( p_table->i_rap_advance == -1 )
        i_max_period = p_table->i_max_period;
    else
        i_max_period = p_table->i_period;

    switch ( p_ts_stream->params.i_conformance )
    {
    default:
    case CONFORMANCE_NONE:
    case CONFORMANCE_ISO:
    case CONFORMANCE_HDMV:
        break;

    case CONFORMANCE_ATSC:
        msg_Warn( p_table, "NIT is not compatible with ATSC conformance" );
        break;

    case CONFORMANCE_DVB:
        if ( i_max_period > INT64_C(10000000) )
            msg_Warn( p_table, "NIT period shouldn't exceed 10 s in DVB systems" );
        break;
    }

    var_Get( p_table, SOUT_CFG_PREFIX "version", &val );
    if ( val.i_int != -1 )
        p_sys->i_version = val.i_int % 32;
    else
        p_sys->i_version = nrand48(subi) % 32;

    var_Get( p_table, SOUT_CFG_PREFIX "network-name", &val );
    psz_network_name = val.psz_string;
    p_sys->p_network_name = p_ts_stream->params.pf_charset(
                            p_ts_stream->params.p_charset, psz_network_name,
                            &p_sys->i_network_name_size );
    if ( p_sys->i_network_name_size > 255 )
    {
        msg_Warn( p_table, "network name is too large: %s", psz_network_name );
        p_sys->i_network_name_size = 255;
    }

    UpdateTable( p_table );

    p_table->b_defines_program = true;
    p_table->i_program = 0;

    p_table->i_peak_bitrate = T_STD_PEAK_RATE;
    p_table->i_priority = TSPACK_PRIORITY_SI;
    p_table->pf_send = Send;
    tstable_Force( p_table );

    msg_Dbg( p_table, "setting up NIT network ID %u name \"%s\"",
             p_ts_stream->i_nid, psz_network_name );
    free( psz_network_name );

    return VLC_SUCCESS;
}
예제 #28
0
파일: topology.c 프로젝트: stwn/kuliax
void check_topology(int Tflag, int argc, char **argv)
{
    extern int	application_bounds(int *minmsg, int *maxmsg);
    extern int	check_ethernets(BOOL **adj);

    CnetAddr	a;
    NODEATTR	*na;
    int		n, p, nrouters;
    int		x, y, rootn;
    int		appl_minmsg, appl_maxmsg;

    LINK	*lp;
    BOOL	**adj;

    for(n=0, nrouters=0 ; n<_NNODES ; n++) {
	extern char	*random_osname(int randint);

/*  ASSIGN A RANDOM OPERATING SYSTEM TYPE IF NECESSARY */
	if(NP[n].nodetype == NT_ROUTER)
	    ++nrouters;
	else if(NP[n].nattr.osname == (char *)NULL)
	    NP[n].nattr.osname = random_osname((int)nrand48(xsubi));
    }
    n = _NNODES - nrouters;
    if(dflag)
	fprintf(stderr,"%d host%s, %d router%s and %d link%s\n",
				    P(n), P(nrouters), P(_NLINKS) );

/*  ENSURE THAT WE HAVE AT LEAST 2 HOSTS (APPLICATION LAYERS) */
    if(n < 2) {
	fputs("A network must have >= 2 hosts\n",stderr);
	++nerrors;
    }

    application_bounds(&appl_minmsg, &appl_maxmsg);
    for(n=0 ; n<(_NNODES-1) ; n++) {
	int	my_minmsg;
	int	my_maxmsg;

/*  NEXT, CHECK FOR DUPLICATE USER-SPECIFIED NODE ADDRESSES */
	na	= &NP[n].nattr;
	a	= na->address;
	for(p=n+1 ; p<_NNODES ; p++)
	    if(a == NP[p].nattr.address) {
		fprintf(stderr, "%s and %s have the same node address (%u)\n",
				NP[n].nodename, NP[p].nodename, a);
		++nerrors;
	    }

/*  CHECK THAT USER-REQUESTED MESSAGE SIZES ARE NEITHER TOO BIG NOR TOO SMALL */
	my_minmsg	= WHICH(na->minmessagesize, DEFAULTNODE.minmessagesize);
	my_maxmsg	= WHICH(na->maxmessagesize, DEFAULTNODE.maxmessagesize);

	if(my_minmsg < appl_minmsg) {
	    fprintf(stderr,
	    "%s has minmessagesize(=%d) < application layer requirements(=%d)\n",
			    NP[n].nodename, my_minmsg, appl_minmsg);
	    ++nerrors;
	}
	if(my_maxmsg > appl_maxmsg) {
	    fprintf(stderr,
	    "%s has maxmessagesize(=%d) > application layer requirements(=%d)\n",
			    NP[n].nodename, my_maxmsg, appl_maxmsg);
	    ++nerrors;
	}
	if(my_minmsg > my_maxmsg) {
	    fprintf(stderr,"%s has minmessagesize(=%d) > maxmessagesize(=%d)\n",
			    NP[n].nodename, my_minmsg, my_maxmsg);
	    ++nerrors;
	}
    }

/*  ALLOCATE AND INITIALIZE THE ADJACENCY MATIRX (CHECK FOR ETHERNETS TOO) */
    adj		= (BOOL **)malloc(_NNODES * sizeof(BOOL *));
    for(n=0 ; n<_NNODES ; ++n) {
	adj[n]	= (BOOL *)malloc(_NNODES * sizeof(BOOL));
	memset(adj[n], 0, _NNODES * sizeof(BOOL));
    }

    check_ethernets(adj);

/*  IF ANY SIGNIFICANT ERRORS OCCURED, TERMINATE THE PROGRAM */
    if(nerrors)
	cleanup(1);

    rootn	= (int)sqrt((double)(_NNODES-1)) + 1;
    for(n=0 ; n < _NNODES ; n++) {
	na	= &NP[n].nattr;
	x	= n%rootn;
	y	= n/rootn;

/*  GIVE SOME DEFAULT X,Y COORDINATES IF THEY HAVE NOT BEEN SPECIFIED */
	if(na->x == 0)
	    na->x		= (2*x+1) * DEF_NODE_X;
	if(na->y == 0)
	    na->y		= (2*y+1) * DEF_NODE_Y;
	if(na->winx == 0)
	    na->winx		= (5*x+1) * DEF_NODE_X;
	if(na->winy == 0)
	    na->winy		= (5*y+1) * DEF_NODE_Y;

/*  ADD ANY REBOOT ARGUMENTS IF NOT PROVIDED BY TOPOLOGY FILE */
	if(na->reboot_args == (char **)NULL)
	    init_reboot_args(na, argc, argv);
    }

/*  NEXT, PROVIDE A WARNING (ONLY) IF THE TOPOLOGY IS NOT CONNECTED */
    for(n=0, lp=LP ; n<_NLINKS ; n++, lp++)
	if(LP[n].linktype == LT_POINT2POINT) {
	    int	from	= lp->endmin;
	    int	to	= lp->endmax;
	    adj[from][to] = adj[to][from] = TRUE;
	}

    if(!connected(_NNODES, adj))
	fprintf(stderr, "*** warning: this topology is not connected\n");
    for(n=0 ; n<_NNODES ; ++n)
	free(adj[n]);
    free(adj);

/*  IF ATTEMPTING TO DRAWN FRAMES IN A 2-NODE WORLD (ONLY), POSITION NODES */
    if(_NNODES == 2 && gattr.drawframes && nethernets == 0 && !Tflag && Wflag) {
	NP[0].nattr.x		= DEF_NODE_X;
	NP[0].nattr.y		= DEF_NODE_Y;
	NP[1].nattr.x		= DRAWFRAME_WIDTH - DEF_NODE_X;
	NP[1].nattr.y		= DEF_NODE_Y;
    }
    else
	gattr.drawframes	= FALSE;
}
예제 #29
0
void make_graph(long n, long m) {
  V = (node *)calloc(sizeof(node), n);
  E = (long *)calloc(sizeof(long), m);
  long i;
  unsigned short rg0[3] = { 9, 1, 8 };
  unsigned short rg[3];
  for (i = 0; i < 3; i++) { rg[i] = rg0[i]; }
  /* make sure there is at least one edge pointing to v > 0 */
  long v;
  for (v = 1; v < n; v++) {
    long u = nrand48(rg) % v;
    assert(u < v);
    assert(0 <= u);
    assert(u < v);
    assert(v < n);
    V[u].out_degree++;
    V[v].in_degree++;
  }
  /* generate remaining edges randomly */
  for (i = 0; i < m - (n - 1); i++) {
    long u = nrand48(rg) % n;
    long v = nrand48(rg) % (n - 1);
    if (v >= u) v++;
    if (u > v) { u = u ^ v; v = u ^ v; u = u ^ v; }
    assert(0 <= u);
    assert(u < v);
    assert(v < n);
    V[u].out_degree++;
    V[v].in_degree++;
  }
  /* allocate a region in the Edges array for each node */
  long c = 0;
  long u;
  for (u = 0; u < n; u++) {
    long out = V[u].out_degree;
    long in = V[u].in_degree;
    V[u].edges_begin = c;
    V[u].edges_end = c;
    myth_join_counter_init(V[u].jc, 0, in);
    c += out;
  }
  assert(c == m);
  for (i = 0; i < 3; i++) { rg[i] = rg0[i]; }

  for (v = 1; v < n; v++) {
    long u = nrand48(rg) % v;
    assert(u < v);
    long e = V[u].edges_end++;
    E[e] = v;
  }
  for (i = 0; i < m - (n - 1); i++) {
    long u = nrand48(rg) % n;
    long v = nrand48(rg) % (n - 1);
    if (v >= u) v++;
    if (u > v) { u = u ^ v; v = u ^ v; u = u ^ v; }
    assert(u < v);
    long e = V[u].edges_end++;
    E[e] = v;
  }
  /* check if there is at least an edge from each node,
     except for the last node */
  for (u = 0; u < n; u++) {
    assert(V[u].edges_end - V[u].edges_begin == V[u].out_degree);
  }
  assert(V[0].edges_begin == 0);
  for (u = 0; u < n - 1; u++) {
    assert(V[u].edges_end == V[u+1].edges_begin);
  }
  assert(V[n - 1].edges_end == m);
}
예제 #30
-8
파일: topology.c 프로젝트: stwn/kuliax
static void add1link(int n, int **grid, int nnodes, BOOL **adj)
{
    int		from, to;
    int		x, y;
    int		dx, dy;

    for(;;) {
	x	= (int)nrand48(xsubi)%n;
	y	= (int)nrand48(xsubi)%n;
	if(grid[x][y] == UNKNOWN)
	    continue;
	from	= grid[x][y];
	if(NP[from].nlinks == MAXDEGREE)
	    continue;

	do {
	    dx	= ((int)nrand48(xsubi)%3)-1;		/* dx:  -1, 0, +1 */
	    dy	= ((int)nrand48(xsubi)%3)-1;		/* dy:  -1, 0, +1 */
	}
#if	RANDOM_DIAGONALS
	while(dx == 0 && dy == 0);
#else
	while(iabs(dx) == iabs(dy));
#endif

	x	+= dx;
	y	+= dy;
	while(x>=0 && x<n && y>=0 && y<n) {
	    if(grid[x][y] != UNKNOWN) {
		to	= grid[x][y];
		if(NP[to].nlinks == MAXDEGREE || adj[from][to] == TRUE)
		    break;
		add_link(LT_POINT2POINT, from, to, NULL);
		adj[from][to] = adj[to][from] = TRUE;
		return;
	    }
	    x	+= dx;
	    y	+= dy;
	}
    }
}