void BlackHoleEffectTest::addObj()
{
	int num = (int)(randnum(100, 300));
	for (int i = 1; i <= num; ++i)
	{
		CCSprite* spt = CCSprite::create("normal_body.png");
		spt->setPositionX(randnum(0, 960));
		spt->setPositionY(randnum(0, 540));
		spt->setRotation(randnum(0, 360));
		this->objList->addObject(spt);
		this->addChild(spt);
	}
}
void randcode(char *line, int length) {
	int current, randomA, randomB, test;
	test = 2;
	for (current = 0; current < length; current++) {
		if (test % 5 == 1 && current > 1) { line[current] = '-'; test++; continue; }
		randomA = randnum(1,2);
		randomB = 60;
		if (randomA == 1) { randomB = randnum(48,57); }
		if (randomA == 2) { randomB = randnum(65,90); }
		line[current] = randomB;
		test++;
	}
	line[length] = '\0';
	return;
}
Exemplo n.º 3
0
/**************************
** SetupCPUEmFloatArrays **
***************************
** Set up the arrays that will be used in the emulated
** floating-point tests.
** This is done by loading abase and bbase elements with
** random numbers.  We use our long-to-floating point
** routine to set them up.
** NOTE: We really don't need the pointer to cbase...cbase
** is overwritten in the benchmark.
*/
void SetupCPUEmFloatArrays(InternalFPF *abase,
                InternalFPF *bbase,
                InternalFPF *cbase,
                ulong arraysize)
{
ulong i;
InternalFPF locFPF1,locFPF2;
/*
** Reset random number generator so things repeat. Inserted by Uwe F. Mayer.
*/
extern int32 randnum(int32 lngval);
randnum((int32)13);

for(i=0;i<arraysize;i++)
{/*       LongToInternalFPF(randwc(50000L),&locFPF1); */
        Int32ToInternalFPF(randwc((int32)50000),&locFPF1);
 /*       LongToInternalFPF(randwc(50000L)+1L,&locFPF2); */
        Int32ToInternalFPF(randwc((int32)50000)+(int32)1,&locFPF2);
        DivideInternalFPF(&locFPF1,&locFPF2,abase+i);
 /*       LongToInternalFPF(randwc(50000L)+1L,&locFPF2); */
        Int32ToInternalFPF(randwc((int32)50000)+(int32)1,&locFPF2);
        DivideInternalFPF(&locFPF1,&locFPF2,bbase+i);
}
return;
}
Exemplo n.º 4
0
/*
 * A program that creates a file of integers that represents a NxN matrix
 * The matrix will be stored in file as row-major order
 * Usage:  matrix_gen size >matrix_file.size
 * Generates a matrix of size "size x size" that contains a random set of integers
 * from -100 .. 100
 */
int main(int argc, char *argv[])
{
    if(argc != 2) 
    {
        fprintf(stderr, "Usage: %s <MATRIX_SIZE>\n", argv[0]);
        return -1;
    }

    int size = atoi(argv[1]);
    int i, j;
    int n;
    if(size <= 0) {
        fprintf(stderr, "Invalid matrix size %d\n", size);
        return -1;
    }
    
    /*
     * populate matrix with data and print out in row-major order
     */
    for(i = 0; i < size; i++) {
        for(j = 0; j < size; j++) {
            n = randnum();
            fwrite(&n, sizeof(int), 1, stdout);
        }
    }

    return 0;
}
Exemplo n.º 5
0
void DataGen(int randnum_list[345])                 //generate the data, return the randnum list
{   
    int valid_count=0;
    int tmp=0;
    
    while(true)
    {
        tmp=randnum(1,1500);                        //required num from 0 to 9. but the randnum() function is not real rand. so the max is 15. larger than reguired.
        
        if(    1<=tmp&&tmp<= 100)tmp= 0;
        if(  101<=tmp&&tmp<= 200)tmp= 1;
        if(  201<=tmp&&tmp<= 300)tmp= 2;
        if(  301<=tmp&&tmp<= 400)tmp= 3;
        if(  401<=tmp&&tmp<= 500)tmp= 4;
        if(  501<=tmp&&tmp<= 600)tmp= 5;
        if(  601<=tmp&&tmp<= 700)tmp= 6;
        if(  701<=tmp&&tmp<= 800)tmp= 7;
        if(  801<=tmp&&tmp<= 900)tmp= 8;
        if(  901<=tmp&&tmp<=1000)tmp= 9;
        if( 1001<=tmp&&tmp<=1500)tmp=10;
        
        if(!CheckColor(tmp))continue;               //check the random number. if it is full, then continue.
        if(tmp>9)tmp=10;                            //if tmp is larger than 9, set it as 10
        randnum_list[valid_count++]=tmp;            //put tmp into randnum_list
        if(valid_count>344)break;                   //if randnum_list if full, then break
    }
}//end of DataGen(int)
Exemplo n.º 6
0
long dice(long howmany, long type)
{
	static long i, total;

	for( i = 1, total = 0; i < howmany; i++ )
		total += randnum(1,type);
	return total;
}
Exemplo n.º 7
0
int randarray(int a[], int asize, int n) /* Inserts random numbers in a[] */
/* 
 * This function inserts random numbers into the array a[] of size asize.
 * The random numbers range from 0 to n-1.
 */
{
int i;

for (i=0; i<asize; i++) a[i]=randnum(n);
}
Exemplo n.º 8
0
void stars_init() {
	int i;
	pvr_poly_cxt_t tmp;

	if (star_x == NULL) {
		star_x = malloc(NS*sizeof(int));
		star_y = malloc(NS*sizeof(int));
		star_z = malloc(NS*sizeof(int));
	}

	for (i=0; i<NS; i++) {
		star_x[i]=randnum(MAXX)-(MAXX/2);
		star_y[i]=randnum(MAXY)-(MAXY/2);
		star_z[i]=randnum(640);
	}

	pvr_poly_cxt_col(&tmp, PVR_LIST_OP_POLY);
	pvr_poly_compile(&stars_header, &tmp);
}
Exemplo n.º 9
0
/*
 * Fills up a matrix with random numbers.
 */
void matrix_random(struct matrix *m)
{
	int i, j;
	
	SANITY_CHECK();
	
	/* Fill matrix. */
	for (i = 0; i < m->height; i++)
	{
		for (j = 0; j < m->width; j++)
			MATRIX(m, i, j) = randnum();
	}
}
Exemplo n.º 10
0
// same as randnum, but allows negative numbers.
long randneg(long start, long end)
{
	long fromzero = 0;
	long neg=0;
	long test=0;

	if (start < 0 && end < 0)
		neg = 1;
	else if (start < 0)
		neg = 2;
		
	if (neg == 2)
	{
		fromzero = 0 - start;
		test = randnum(start+fromzero,end+fromzero);
	}
	else if (neg == 1)
		test = randnum(start*-1,end*-1);
	else
		test = randnum(start,end);

	return neg > 0 ? neg > 1 ? test - fromzero : test*-1 : test;
}
Exemplo n.º 11
0
pixel randcolor() {
  pixel result;
  result.r = randnum(0, 255);
  result.g = randnum(0, 255);
  result.b = randnum(0, 255);
  /*
  color = color % 5;
  switch (color++) {
    case 0:
      result.r = 0;
      result.g = 0;
      result.b = 0;
      break;
    case 1:
      result.r = 255;
      result.g = 0;
      result.b = 0;
      break;
    case 2:
      result.r = 0;
      result.g = 255;
      result.b = 0;
      break;
    case 3:
      result.r = 0;
      result.g = 0;
      result.b = 255;
      break;
    case 4:
      result.r = 255;
      result.g = 255;
      result.b = 255;
      break;
  }
  */
  return result;
}
Exemplo n.º 12
0
/*
 * Runs benchmark.
 */
int main(int argc, char **argv)
{
	int i;              /* Loop index. */
	double *mask;       /* Mask.       */
	uint64_t end;       /* End time.   */
	uint64_t start;     /* Start time. */
	unsigned char *img; /* Image.      */
	
	readargs(argc, argv);
	
	timer_init();
	srandnum(seed);
	
	/* Benchmark initialization. */
	if (verbose)
		printf("initializing...\n");
	start = timer_get();
	img = smalloc(p->imgsize*p->imgsize*sizeof(char));
	for (i = 0; i < p->imgsize*p->imgsize; i++)
		img[i] = randnum() & 0xff;
	mask = smalloc(p->masksize*p->masksize*sizeof(double));
	generate_mask(mask);
	end = timer_get();
	if (verbose)
		printf("  time spent: %f\n", timer_diff(start, end)*MICROSEC);
		
	/* Apply filter. */
	if (verbose)
		printf("applying filter...\n");
	start = timer_get();
	gauss_filter(img, p->imgsize, mask, p->masksize);
	end = timer_get();
	
	total = timer_diff(start, end);

	/* Print timing statistics. */
	printf("timing statistics:\n");
	printf("  master:        %f\n", master*MICROSEC);
	for (i = 0; i < nclusters; i++)
		printf("  slave %d:      %f\n", i, slave[i]*MICROSEC);
	printf("  communication: %f\n", communication*MICROSEC);
	printf("  total time:    %f\n", total*MICROSEC);
	
	/* House keeping. */
	free(mask);
	free(img);
	
	return (0);
}
Exemplo n.º 13
0
Arquivo: car.c Projeto: dn5/vircar
void* check_pump_flow()
{
	pthread_t id = pthread_self();

	// Check if (pthread_equal(id, tid[0])) first thread?

	srand(time(NULL));

	while (1)
	{
		int r = randnum(1, 13);
		Log("Thread #pump_flow# exec ..");

		sleep(r);
	}
}
Exemplo n.º 14
0
/*
 * Runs benchmark.
 */
int main(int argc, char **argv)
{
	int i;          /* Loop index.         */
	int *a;         /* Array to be sorted. */
	uint64_t end;   /* End time.           */
	uint64_t start; /* Start time.         */
	
	readargs(argc, argv);
	
	timer_init();
	srandnum(seed);
	
	/* Benchmark initialization. */
	if (verbose)
		printf("initializing...\n");
	start = timer_get();
	a = smalloc(p->n*sizeof(int));
	for (i = 0; i < p->n; i++)
		a[i] = randnum() & 0xfffff;
	end = timer_get();
	if (verbose)
		printf("  time spent: %f\n", timer_diff(start, end)*MICROSEC);
	
	/* Cluster data. */
	if (verbose)
		printf("sorting...\n");
	start = timer_get();
	bucketsort(a, p->n);
	end = timer_get();

	total = timer_diff(start, end);

	/* Print timing statistics. */
	printf("timing statistics:\n");
	printf("  master:        %f\n", master*MICROSEC);
	for (i = 0; i < nclusters; i++)
		printf("  slave %d:      %f\n", i, slave[i]*MICROSEC);
	printf("  communication: %f\n", communication*MICROSEC);
	printf("  total time:    %f\n", total*MICROSEC);
	
	/* House keeping. */
	free(a);
	
	return (0);
}
Exemplo n.º 15
0
// 1> 初始化随机序列;2>建立规模为SUM的初始种群;3>建立log链表头,其中指针head记录立案表头的位置,end始终指向链表的尾部
void initiate() {
  int i, stime;
  long ltime;
  ltime = time(NULL);
  stime = (unsigned) ltime / 2;
  srand(stime);
  for (i = 0; i < SUM; i++)
    gen_group[i].info = randnum();   // 调用randnum 函数建立初始种群
  gen_result.suitability = 1000;
  result_unchange_time = 0;
  head = end = (struct log *) malloc(sizeof(struct log)); // 初始化log链
  if (head == NULL) {
    printf("\nmalloc fail\n");
    exit(0);
  }
  end->next = NULL;
  log_num = 1;
}
Exemplo n.º 16
0
int main(int argc, char **argv) {
  if (argc != 7) {
    printf("Usage: npoints dimension ncentroids mindistance seed nthreads\n");
    exit(1);
  }

  npoints = strtol(argv[1], NULL, 0);
  dimension = strtol(argv[2], NULL, 0);
  ncentroids = strtol(argv[3], NULL, 0);
  mindistance = strtol(argv[4], NULL, 0);
  seed = strtol(argv[5], NULL, 0);
  nthreads = strtol(argv[6], NULL, 0);

  srandnum(seed);

  data = calloc((unsigned int)npoints, sizeof(vector_t));
  for (int i = 0; i < npoints; i++) {
    data[i] = calloc((unsigned int)dimension, sizeof(float));
    for (int j = 0; j < dimension; j++) {
      data[i][j] = randnum() & 65535U;
    }
  }

  map = kmeans();

  for (int i = 0; i < ncentroids; i++) {
    printf("Partition %d:\n", i);
    for (int j = 0; j < npoints; j++) {
      if (map[j] == i) {
        printf("%d ", j);
      }
    }
    printf("\n");
  }

  free(map);
  for (int i = 0; i < npoints; i++) {
    free(data[i]);
  }
  free(data);

  return 0;
}
Exemplo n.º 17
0
int main() {
  int sum, maxsum, i, j, k, m;
  int* a;
  clock_t begin, end;
  double time_spent;
  int n[9] = {100, 200, 300, 400, 500, 600, 700, 800, 900};

  srand(time(NULL));

  sum = 0;
  maxsum = 0;

  for(m=0; m<9; m++) {
    a = malloc(sizeof(int)*n[m]);

    for(i=0; i<n[m]; i++) {
      a[i] = randnum();
      //printf("%d ", a[i]);
    }
    //printf("\n");

    begin = clock();
    for(i=0; i<(n[m]-1); i++) {
      for(j=1; j<n[m]; j++) {
        sum = 0;
        for(k=i; k<(j+1); k++) {
          sum += a[k];
        }
        if(sum > maxsum) {
          maxsum = sum;
        }
      }
    }
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

    printf("time spent: %f\n", time_spent);
  }

  return 0;
}
JNIEXPORT void JNICALL
Java_ph_edu_dlsu_orbflow_CameraActivity_process(JNIEnv *env, jobject instance,
                                                jobject pTarget, jbyteArray pSource) {
    uint32_t start;
    cv::Mat srcBGR;

    cv::RNG randnum(12345); // for random color
    cv::Scalar color;

    AndroidBitmapInfo bitmapInfo;
    uint32_t *bitmapContent;

    if (AndroidBitmap_getInfo(env, pTarget, &bitmapInfo) < 0) abort();
    if (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888) abort();
    if (AndroidBitmap_lockPixels(env, pTarget, (void **) &bitmapContent) < 0) abort();

    /// Access source array data... OK
    jbyte *source = (jbyte *) env->GetPrimitiveArrayCritical(pSource, 0);
    if (source == NULL) abort();

    /// cv::Mat for YUV420sp source and output BGRA
    cv::Mat srcGray(bitmapInfo.height, bitmapInfo.width, CV_8UC1, (unsigned char *) source);
    cv::Mat src(bitmapInfo.height + bitmapInfo.height / 2, bitmapInfo.width, CV_8UC1,
                (unsigned char *) source);
    cv::Mat mbgra(bitmapInfo.height, bitmapInfo.width, CV_8UC4, (unsigned char *) bitmapContent);


/***********************************************************************************************/
    /// Native Image Processing HERE...

    start = static_cast<uint32_t>(cv::getTickCount());

    if (srcBGR.empty())
        srcBGR = cv::Mat(bitmapInfo.height, bitmapInfo.width, CV_8UC3);

    // RGB frame input
    cv::cvtColor(src, srcBGR, CV_YUV420sp2RGB);

    /// If previous frame doesn't exist yet, initialize to srcGray
    if (previous_gray_frame.empty()) {
        srcGray.copyTo(previous_gray_frame);
        LOGI("Initializing previous frame...");
    }

    if (detector == NULL)
        detector = cv::ORB::create();

    // create(int nfeatures=500, float scaleFactor=1.2f, int nlevels=8, int edgeThreshold=31,
    // int firstLevel=0, int WTA_K=2, int scoreType=ORB::HARRIS_SCORE, int patchSize=31, int fastThreshold=20);


    // Detect the strong corners on an image.
    detector->detect(previous_gray_frame, previous_keypoints, cv::Mat());


    // Debugging
    //LOGI("previous_keypoints.size() = %d", previous_keypoints.size());

    // Convert Keypoints to Points
    keypoints2points(previous_keypoints, previous_features);

    //LOGI("previous_features.size() = %d", previous_features.size());

    // Refines the corner locations
     cornerSubPix(previous_gray_frame, previous_features,
                 cv::Size(win_size, win_size), cv::Size(-1, -1),
                cv::TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03));

    std::vector<uchar> features_found;
    // The output status vector. Each element of the vector is set to 1 if the flow for
    // the corresponding features has been found, 0 otherwise

    // Calculates the optical flow for a sparse feature set using the iterative Lucas-Kanade method with
    // pyramids
    cv::calcOpticalFlowPyrLK(previous_gray_frame, srcGray,
                             previous_features, current_features, features_found,
                             cv::noArray(), cv::Size(win_size * 4 + 1, win_size * 4 + 1), 0,
                             cv::TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3));

    for (int i = 0; i < (int) previous_features.size(); i++) {
        if (features_found[i]) {
            // Randomize color and display the velocity vectors
            color = cv::Scalar(randnum.uniform(0, 255), randnum.uniform(0, 255),
                               randnum.uniform(0, 255));
            line(srcBGR, previous_features[i], current_features[i], color);
        }
    }


    LOGI("Processing took %0.2f ms.", 1000*(static_cast<uint32_t>(cv::getTickCount()) - start)/(float)cv::getTickFrequency());

    cvtColor(srcBGR, mbgra, CV_BGR2BGRA);

    // Copy the current gray frame into previous_gray_frame
    srcGray.copyTo(previous_gray_frame);

/************************************************************************************************/

    /// Release Java byte buffer and unlock backing bitmap
    //env-> ReleasePrimitiveArrayCritical(pSource,source,0);
    /*
     * If 0, then JNI should copy the modified array back into the initial Java
     * array and tell JNI to release its temporary memory buffer.
     *
     * */

    env->ReleasePrimitiveArrayCritical(pSource, source, JNI_COMMIT);
    /*
 * If JNI_COMMIT, then JNI should copy the modified array back into the
 * initial array but without releasing the memory. That way, the client code
 * can transmit the result back to Java while still pursuing its work on the
 * memory buffer
 *
 * */

    /*
     * Get<Primitive>ArrayCritical() and Release<Primitive>ArrayCritical()
     * are similar to Get<Primitive>ArrayElements() and Release<Primitive>ArrayElements()
     * but are only available to provide a direct access to the target array
     * (instead of a copy). In exchange, the caller must not perform blocking
     * or JNI calls and should not hold the array for a long time
     *
     */


    if (AndroidBitmap_unlockPixels(env, pTarget) < 0) abort();
}
int main(int argc, char *argv[])
{
  /* seed rand */
  struct timeval t1;
  gettimeofday(&t1, NULL);
  srand(t1.tv_usec * t1.tv_sec);

  /* throw out the first couple of outputs */
  for (int i = 0; i < 10; ++i) {
    randnum(10);
  }

  int i = 0;
  int l1;
  int l2;
  int max;
  double percent = 0.0;
  long num_samples = 0;
  unsigned long total = 0;

  kseq_t *seq1;
  kseq_t *seq2;
  kseq_t *seqU;
  gzFile fp1;
  gzFile fp2;
  gzFile fpU;

  char fname_buf[1000];

  if (argc != 8) {
    fprintf(stderr,
            "VERSION: %s\nUsage: %s <1: percent to sample> "
            "<2: number of samples> <3: outdir> "
            "<4: basename for outfiles> "
            "<5: in.1.f{a,q}> <6: in.2.f{a,q}> <7: in.U.f{a,q}>\n",
            VERSION,
            argv[0]);

    return 1;
  }

  percent = strtod(argv[1], NULL);
  num_samples = strtol(argv[2], NULL, 10);

  outf_t *outfiles1[num_samples];
  outf_t *outfiles2[num_samples];
  outf_t *outfilesU[num_samples];
  for (i = 0; i < num_samples; ++i) {
    sprintf(fname_buf,
            "%s/%s.sample_%d.1.fq",
            argv[3],
            argv[4],
            i);
    outfiles1[i] = outf_init(fname_buf);

    sprintf(fname_buf,
            "%s/%s.sample_%d.2.fq",
            argv[3],
            argv[4],
            i);
    outfiles2[i] = outf_init(fname_buf);

    sprintf(fname_buf,
            "%s/%s.sample_%d.U.fq",
            argv[3],
            argv[4],
            i);
    outfilesU[i] = outf_init(fname_buf);
  }


  max = 100 / percent;

  fp1 = gzopen(argv[5], "r");
  fp2 = gzopen(argv[6], "r");
  fpU = gzopen(argv[7], "r");

  if (!fp1) {
    fprintf(stderr,
            "ERROR - could not open %s\n",
            argv[5]);

    return 2;
  }

  if (!fp2) {
    fprintf(stderr,
            "ERROR - could not open %s\n",
            argv[6]);

    return 2;
  }

  if (!fpU) {
    fprintf(stderr,
            "ERROR - could not open %s\n",
            argv[7]);

    return 2;
  }

  seq1 = kseq_init(fp1);
  seq2 = kseq_init(fp2);
  seqU = kseq_init(fpU);

  while ((l1 = kseq_read(seq1)) >= 0) {
    /* TODO: might be faster to read all of one, mark the records
       taken and then read all of the second? */
    l2 = kseq_read(seq2);
    if (l2 < 0) {
      fprintf(stderr,
              "ERROR -- not enough reads in reverse file %s\n",
              argv[3]);
    }

    ++total;
    if (total % 100000 == 0) {
      fprintf(stderr,
              "INFO -- reading paired files: %lu\r",
              total);
    }

    for (i = 0; i < num_samples; ++i) {
      if (randnum(max) == 1) {
        print_record(seq1, outfiles1[i]->fp);
        print_record(seq2, outfiles2[i]->fp);
      }
    }
  }

  total = 0;
  while ((l1 = kseq_read(seqU)) >= 0) {
    ++total;
    if (total % 100000 == 0) {
      fprintf(stderr,
              "INFO -- reading unpaired file: %lu\r",
              total);
    }

    for (i = 0; i < num_samples; ++i) {
      if (randnum(max) == 1) {
        print_record(seqU, outfilesU[i]->fp);
      }
    }
  }

  kseq_destroy(seq1);
  kseq_destroy(seq2);
  kseq_destroy(seqU);

  gzclose(fp1);
  gzclose(fp2);
  gzclose(fpU);

  for (i = 0; i < num_samples; ++i) {
    outf_destroy(outfiles1[i]);
    outf_destroy(outfiles2[i]);
    outf_destroy(outfilesU[i]);
  }

  return 0;
}
Exemplo n.º 20
0
/*
 * Runs benchmark.
 */
int main(int argc, char **argv)
{
	int i;              /* Loop index.            */
	int *mask;       	/* Mask.                  */
	uint64_t end;       /* End time.              */
	uint64_t start;     /* Start time.            */
	char *img; 			/* Image.                 */
	int numcorners=0;	/* Total corners detected */
	
#ifdef _XEON_PHI_
	double power;
#endif	
	
	readargs(argc, argv);
	
	timer_init();
	srandnum(seed);
	omp_set_num_threads(nthreads);
	
	/* Benchmark initialization. */
	if (verbose)
		printf("initializing...\n");
	start = timer_get();
	img = smalloc(p->imgsize*p->imgsize*sizeof(char));
	for (i = 0; i < p->imgsize*p->imgsize; i++){
		char val = randnum() & 0xff;
		img[i] = (val>0) ? val : val*(-1);
	}	
	mask = smalloc(p->maskrows*p->maskcolumns*sizeof(int));
	generate_mask(mask);
	end = timer_get();
	if (verbose)
		printf("  time spent: %f\n", timer_diff(start, end)*MICROSEC);
	
#ifdef _XEON_PHI_
	power_init();
#endif		
		
	/* Detect corners. */
	if (verbose)
		printf("detecting corners...\n");
	start = timer_get();
	numcorners = fast(img, p->imgsize, mask);
	end = timer_get();
	
#ifdef _XEON_PHI_
	power = power_end();
#endif

	printf("timing statistics:\n");
	printf("  total time:       %f\n", timer_diff(start, end)*MICROSEC);

#ifdef _XEON_PHI_
	printf("  average power: %f\n", power*0.000001);
#endif

	printf("  corners detected: %d\n", numcorners);
	
	/* House keeping. */
	free(mask);
	free(img);
	
	return (0);
}
Exemplo n.º 21
0
long percent(void)
{
	return randnum(1,100);
}
Exemplo n.º 22
0
unsigned long GetSpeed(char *szHost) {
	if(strlen(szHost) > MAXHOSTNAME) return 0;
	unsigned long lBufSize=NUM_KILOBYTES*1024;
	SOCKET sSock;
	SOCKADDR_IN ssin;

	memset(&ssin, 0, sizeof(ssin));
	ssin.sin_family = AF_INET;
	if ((ssin.sin_addr.s_addr = ResolveAddress(szHost)) == INADDR_NONE) return 0;
	ssin.sin_port = fhtons(80);

	if ((sSock = fsocket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) return 0;
	if (fconnect(sSock, (LPSOCKADDR)&ssin, sizeof(ssin)) == SOCKET_ERROR) return 0;

	char *szBuf=(char*)malloc(lBufSize+3);

	srand(GetTickCount());
	int iChar=(char)(randnum(255));
	memset(szBuf,	0,		lBufSize+1	);
	memset(szBuf,	iChar,	lBufSize	);

	unsigned long lStrLen=strlen(szBuf);

	char *szPostReq=(char*)malloc(lBufSize+1002);
	sprintf(szPostReq, "POST / HTTP/1.0\r\n"
		"Host: %s\r\n"
		"Content-Length: %d\r\n"
		"\r\n",
		szHost, lStrLen);
	strcat(szPostReq, szBuf);
	strcat(szPostReq, "\r\n");
	
	lStrLen=strlen(szPostReq);

	unsigned long lStartMS=GetTickCount();

	for(unsigned long l=0; l<lStrLen; l+=1024) {
		if(lStrLen-l < 1024) {
			if(fsend(sSock, szPostReq+l, lStrLen-l,0) == SOCKET_ERROR) { 
				fclosesocket(sSock);
				free(szBuf); 
				free(szPostReq);
				return 0; 
			}
		} else {
			if(fsend(sSock, szPostReq+l, 1024,0) == SOCKET_ERROR) { 
				fclosesocket(sSock);
				free(szBuf); 
				free(szPostReq);
				return 0; 
			}
		}
	}

	unsigned long lEndMS=GetTickCount();

	float fElapsedS=(float)(lEndMS-lStartMS)/1000.0f;
	if(fElapsedS==0.0f) fElapsedS=1.0f;

	float fBytesPS=(float)lStrLen/fElapsedS;	// Get bytes per second
	float fKBytesPS=fBytesPS/1024.0f;			// Get kilobytes per second
	float fBitsPS=fBytesPS*8.0f;				// Get bits per second
	float fKBitsPS=fBitsPS/1024.0f;				// Get kilobits per se

	fclosesocket(sSock);
	free(szBuf); 
	free(szPostReq);

	return (unsigned long)fKBitsPS;
}
Exemplo n.º 23
0
main()
{
	int i;
	printf("%d  ",randnum(1,10));
	printf("%d  ",randnum(1,10));
}
Exemplo n.º 24
0
void DoSpeedTest(SOCKET sock, char chan[128], BOOL notice) {
	unsigned long lProbe1=0, lProbe2=0;
	int iDivide=3;
	char sendbuf[IRCLINE];
	char *EU[] = {
		"www.schlund.net",
		"www.utwente.nl",
		"verio.fr",
		"www.1und1.de",
		"www.switch.ch",
		"www.belwue.de",
		"de.yahoo.com",
		"www.google.it"
	};
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
	char *US[] = {
		"www.xo.net",
		"www.stanford.edu",
		"www.verio.com",
		"www.nocster.com",
		"www.rit.edu",
		"www.cogentco.com",
		"www.burst.net",
		"nitro.ucsc.edu",
		"www.level3.com",
		"www.above.net",
		"www.easynews.com",
		"www.google.com"
	};
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
	char *ASIA[] = {
		"www.lib.nthu.edu.tw",
		"www.st.lib.keio.ac.jp",
		"www.d1asia.com",
		"www.nifty.com",
		"yahoo.co.jp",
		"www.google.co.jp"
	};
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
	lProbe1=GetSpeed(EU[randnum(sizeof(EU)/sizeof(LPTSTR))]);
	lProbe2=GetSpeed(EU[randnum(sizeof(EU)/sizeof(LPTSTR))]);
	unsigned long lEUSpeed=0;
	if (lProbe1 && lProbe2) lEUSpeed=(lProbe1+lProbe2)/2;
	else { if (lProbe1) lEUSpeed=lProbe1; else lEUSpeed=lProbe2; }
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
	unsigned long lUSSpeed=0;
	lProbe1=GetSpeed(US[randnum(sizeof(US)/sizeof(LPTSTR))]);
	lProbe2=GetSpeed(US[randnum(sizeof(US)/sizeof(LPTSTR))]);
	if (lProbe1 && lProbe2) lUSSpeed=(lProbe1+lProbe2)/2;
	else { if (lProbe1) lUSSpeed=lProbe1; else lUSSpeed=lProbe2; }
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
	lProbe1=GetSpeed(ASIA[randnum(sizeof(ASIA)/sizeof(LPTSTR))]);
	lProbe2=GetSpeed(ASIA[randnum(sizeof(ASIA)/sizeof(LPTSTR))]);
	unsigned long lASIASpeed=0;
	if (lProbe1 && lProbe2) lASIASpeed=(lProbe1+lProbe2)/2;
	else { if (lProbe1) lASIASpeed=lProbe1; else lASIASpeed=lProbe2; }
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
	unsigned long lTotalSpeed=0;
	if (lEUSpeed) lTotalSpeed=lEUSpeed; else iDivide--;
	if (lUSSpeed) lTotalSpeed+=lUSSpeed; else iDivide--;
	if (lASIASpeed) lTotalSpeed+=lASIASpeed; else iDivide--;
	lTotalSpeed=lTotalSpeed/iDivide;
	sprintf(sendbuf, "9-1::0[12 SpEeD TeSt 0]: Europe[ %d kbit/s] USA[ %d kbit/s] Asia[ %d kbit/s] Average[ %d kbit/s]",
		lEUSpeed, lUSSpeed, 
		lASIASpeed, lTotalSpeed);
	irc_privmsg(sock, chan, sendbuf, notice);
	addlog(sendbuf);
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
	return;
}
Exemplo n.º 25
0
main() {
   int x, y, loop = 1, fact = 0;
   char instring[200], outstring[200], sub[200], vrb[200], rst[200], qwd[200];
   char osub[200], ovrb[200], orst[200];
   fam = 0;
   printf("Hello there.  My name is Eliza and I was written by Mohan Embar.\n");
   printf("Please type \"END\" to end this session.\n");
   printf("I'm here to help you if I can.  What seems to be the trouble?\n");
   while (loop) {
      printf("\n");
      gets(instring);
      printf("\n");
      parse(instring);
      if (numwords == 0) {
         switch (x = randnum(2)) {
            case 1 : printf("Don't you have anything to say?\n");
               break;
            case 2 : printf("Cat got your tongue?\n");
               break;
         }
         continue;
      }
      if (!strcmp(s[1],"END")) {
         printf("Goodbye.  Please come again.\n");
         break;
      }
      agree();
      if (bad_word())
         printf(b_word_resp());
      else if (naughty_word())
         printf(n_word_resp());
      else if (x = family()) {
         fam = x;
         printf(fam_resp());
         strcpy(fam_member,s[fam]);
      }
      else if (sword("ALIKE",1))
         printf(alike_resp());
      else if (sword("ALWAYS",1))
         printf(always_resp());
      else if (sword("BECAUSE",1))
         printf(because_resp());
      else if (sword("YES",1))
         printf(yes_resp());
      else if (sword("NO",1) || sword("NOT",1))
         printf(neg_resp());
      else if (x = i_am()) { /* If occurrence of I AM x.. */
         get_til_stop(x,outstring);    /* Get I AM x .. into outstring */
         printf(i_am_resp(),outstring); /* Print reponse for this */
      }
      else if (real_quest() ||
              (is_helper(s[1]) && is_sub_pronoun(s[2])) ||
              sub_and_helper()) {
         if (real_quest()) {
            strcpy(qwd,s[1]);
            strcpy(vrb,s[2]);
            strcpy(sub,s[3]);
            get_til_stop(4,rst);
         }
         else if (is_helper(s[1]) && is_sub_pronoun(s[2])) {
            strcpy(vrb,s[1]);
            strcpy(sub,s[2]);
            get_til_stop(3,rst);
            strcpy(qwd,"YES");
         }
         else if (sub_and_helper()) {
            x = find_helper();
            y = search_back_sub(x);
            strcpy(vrb,s[x]);
            get_til_stop(x+1,rst);
            getrange(y,x-1,sub);
            strcpy(qwd,"NO");
         }
         make_lower(qwd);
         if (strcmp(sub,"I")) make_lower(sub);
         make_lower(vrb);
         make_lower(rst);
         /* First do x verb y responses */

         /*
	 printf("\n*** %s\n",sub);
         */
	 
	 if (!strcmp(sub," I") || !strcmp(sub,"I")) {
            printf(you_resp());
         }
         else if (!strcmp(qwd,"no")) {
            /* Record this statement for later use. */
            fact = 1;
            strcpy(osub,sub); strcpy(ovrb,vrb); strcpy(orst,rst);
            if (is_be(vrb) && !strcmp(sub," you") && (y = sad_word())) {
               getrange(y,y,outstring);
               x = randnum(5)+6;
            }
            else if (is_be(vrb) && (y = sad_word())) {
               getrange(y,y,outstring);
               x = randnum(2)+11;
            }
            else if (is_be(vrb))
               x = randnum(6);
            else x = randnum(4);
            switch (x) {
               case 1 : printf("How do you feel about%s?\n",cnnv(sub));
                  break;
               case 2 : printf("Why %s%s%s?\n",vrb,sub,rst);
                  break;
               case 3 : for (y=1;sub[y]=sub[y--];y=y+2);
                  sub[0] = toupper(sub[0]);
                  printf("%s %s%s?\n",sub,vrb,rst);
                  break;
               case 4 : printf("Could you describe%s for me?\n",cnnv(sub));
                  break;
               case 5 : printf("What if%s were not%s?\n",sub,rst);
                  break;
               case 6 : printf("Would you be happy if%s were not%s?\n",sub,
                                rst);
                  break;
               case 7 : printf("I'm sorry to hear that you are%s.\n",outstring);
                  break;
               case 8 : printf("Do you think that coming here will help you not to be%s?\n",outstring);
                  break;
               case 9 : printf("Let's talk about why you feel%s.\n",outstring);
                  break;
               case 10 : printf("What happened that made you feel%s?\n",outstring);
                  break;
               case 11 : printf("What could be the reason for your feeling%s?\n",outstring);
                  break;
               case 12 : printf("What could cause%s to be%s?\n",cnnv(sub),outstring);
                  break;
               case 13 : printf("If%s came here, would it help%s not to be%s?\n",sub,cnnv(sub),outstring);
                  break;
            }
         }
         else if (!strcmp(sub,"you"))
            printf(you_know());
         else if (!strcmp(qwd,"yes")) {
            x = randnum(8);
            switch (x) {
               case 1 : printf("You want to know if %s %s%s.\n",sub,vrb,rst);
                  break;
               case 2 : printf("If %s %s%s, does that concern you?\n",sub,vrb,rst);
                  break;
               case 3 : printf("What are the consequences if %s %s%s?\n",sub,vrb,rst);
                  break;
               case 4 : printf("Why does %s concern you?\n",sub);
                  break;
               case 5 : printf("Why are you thinking of %s?\n",cnnv(sub));
                  break;
               case 6 : printf("Tell me more about %s.\n",cnnv(sub));
                  break;
               case 7 : printf("To answer that, I'd need to know more about %s.\n",cnnv(sub));
                  break;
               case 8 : printf("What is the relationship between you and %s?\n",cnnv(sub));
                  break;
               case 9 : printf("Why don't you ask %s?\n",cnnv(sub));
                  break;
            }
         }
         else {
            x = randnum(8);
            switch (x) {
               case 1 : printf("You want to know %s %s %s%s.\n",qwd,sub,vrb,rst);
                  break;
               case 2 : printf("If %s %s%s, does that concern you?\n",sub,vrb,rst);
                  break;
               case 3 : printf("What are the consequences if %s %s%s?\n",sub,vrb,rst);
                  break;
               case 4 : printf("Why does %s concern you?\n",sub);
                  break;
               case 5 : printf("Why are you thinking of %s?\n",cnnv(sub));
                  break;
               case 6 : printf("Tell me more about %s.\n",cnnv(sub));
                  break;
               case 7 : printf("To answer that, I'd need to know more about %s.\n",cnnv(sub));
                  break;
               case 8 : printf("What is the relationship between you and %s?\n",cnnv(sub));
                  break;
               case 9 : printf("Why don't you ask %s?\n",cnnv(sub));
                  break;
            }
         }
      }
      else if (is_command())
         printf(command_resp());
      else if (vague_quest())
         printf(question());
      else if ((s[numwords][0] == '?') && !real_quest())
         printf(question());
      else if (x = sad_word()) {
         getrange(x,x,outstring);
         for (y=1;outstring[y]=outstring[y--];y=y+2);
         outstring[0] = toupper(outstring[0]);
         printf("%s?\n",outstring);
      }
      else if (x = can_spit_out()) {
         if (x<=(numwords-2) && is_sub_pronoun(s[x])
                             && (matches("NEED",s[x+1]) ||
                                 matches("WANT",s[x+1]))) {
            get_til_stop(x+2,outstring);
            strcpy(sub,s[x]);
            if (strcmp(sub,"I")) make_lower(sub);
            if (strcmp(s[x],"I")) make_lower(s[x]);
            x = randnum(6);
            switch (x) {
               case 1 : printf("What would it mean to %s if %s got%s?\n",cnnv2(s[x]),sub,outstring);
                  break;
               case 2 : printf("Would %s really be happy if %s got%s?\n",sub,sub,outstring);
                  break;
               case 3 : printf("Why is getting%s so desirable?\n",outstring);
                  break;
               case 4 : printf("Okay.  Suppose %s got%s.  Then what?\n",sub,outstring);
                  break;
               case 5 : printf("Why is this important to %s?\n",cnnv2(sub));
                  break;
               case 6 : printf("What price would %s pay to achieve this?\n",sub);
                  break;
            }
         }
         else {
            get_til_stop(x,outstring);
            outstring[1]=toupper(outstring[1]);
            printf("%s.\n",outstring+1);
         }
      }
      else if (fam) {
         make_lower(fam_member);
         printf(family_resp(),fam_member);
         fam = 0;
      }
      else if (fact && (randnum(5)==3)) {
         printf(old_fact(),osub,ovrb,orst);
         fact = 0;
      }
      else {
         printf(go_on());
      }
   }
}
Exemplo n.º 26
0
void createExplosion(vec pos)
{
    playSound(explosion, pos, 20,randgauss(1,.1),3000);
    
    
    // deal damage to nearby objects
    // apply explosion force to objects
    for (int i = 0; i < activeObjects.size(); i++) {
        GameObject* obj = activeObjects[i];
        if (obj->isEnemy || obj == p) {
            float dist = (obj->pos - pos).getLength();
            if (dist < 250) {
                float damage = (250 - dist)*.7;
                float force = damage * .8;
                if (force > 25) force = 25;
                //say("F: " << force);
                obj->pos += vec(force, 0).rotated((obj->pos - pos).getAngle());
                if (obj == p) damage *= .4; // decrease explosion damage for player
                obj->hurt(damage);
            }
        }
    }

    // create particles
    for (int i = 0; i < 10; i++) {
        vec smokeVel = randVec(1, 1);
        Color smokeColor = hsvtorgb(0, 0, randnum(0, 1));
        Particle* p = new Particle(pos + smokeVel*randnum(80, 135),
                                   smokeVel*randnum(0, 1) + randVec(0.1, 1),
                                   randint(100, 200),
                                   randnum(0.8, 1.2),
                                   randnum(2, 3),
                                   Color(smokeColor.r, smokeColor.g, smokeColor.b, 80),
                                   Color(smokeColor.r, smokeColor.g, smokeColor.b, 0),
                                   randgauss(-.01,.002),
                                   0.998);
        p->setTexture(blob);
        p->angle = randnum(0, 360);
        p->angVel = randgauss(0, 1);
        p->layer = 1;
        //p->blendMode = BlendAdd;

    }

    Color flashColor = hsvtorgb(randnum(30, 60), randnum(0.5, 1), randnum(0.5, 1));
    for (int i = 0; i < 10; i++) {
        Particle* p = new Particle(pos,
                                   randVec(0, 1),
                                   randint(4, 7), 0.1, randnum(2.5, 4.5),
                                   Color(flashColor.r, flashColor.g, flashColor.b, 255),
                                   Color(flashColor.r, flashColor.g, flashColor.b, 0));
        p->setTexture(blob);
        p->blendMode = BlendAdd;
        p->layer = 1;
    }

    // break terrain
    float playerDist = (p->pos - pos).getLength();
    screenShake += 500.f / playerDist;
    if (screenShake > 5) screenShake = 5;
    destroyTerrainCircle(pos, randgauss(100, 5));
}