コード例 #1
0
ファイル: MONST.CPP プロジェクト: nkedel/nate-dartmouth-stuff
void monster::computehp(){
    uchar i;

    for(i=0;i<hd;i++)
        hp+=rnd1(8)+1;
    hpmax=hp;
}
コード例 #2
0
ファイル: RandomStream.cpp プロジェクト: 119/vdc
int RandomBuf::readFromDevice(char* buffer, std::streamsize length)
{
	int n = 0;

#if defined(POCO_OS_FAMILY_WINDOWS)
	HCRYPTPROV hProvider = 0;
	CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
	CryptGenRandom(hProvider, (DWORD) length, (BYTE*) buffer);
	CryptReleaseContext(hProvider, 0);
	n = static_cast<int>(length);
#else
	#if defined(POCO_OS_FAMILY_UNIX)
	int fd = open("/dev/urandom", O_RDONLY, 0);
	if (fd >= 0) 
	{
		n = read(fd, buffer, length);
		close(fd);
	}
	#endif
	if (n <= 0)
	{
		// x is here as a source of randomness, so it does not make
		// much sense to protect it with a Mutex.
		static UInt32 x = 0;
		Random rnd1(256);
		Random rnd2(64);
		x += rnd1.next();
 
		n = 0;
		SHA1Engine engine;
		UInt32 t = (UInt32) std::time(NULL);
		engine.update(&t, sizeof(t));
		void* p = this;
		engine.update(&p, sizeof(p));
		engine.update(buffer, length);
		UInt32 junk[32];
		engine.update(junk, sizeof(junk));
		while (n < length)
		{
			for (int i = 0; i < 100; ++i)
			{
				UInt32 r = rnd2.next();
				engine.update(&r, sizeof(r));
				engine.update(&x, sizeof(x));
				x += rnd1.next();
			}
			DigestEngine::Digest d = engine.digest();
			for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n < length; ++it, ++n)
			{
				engine.update(*it);
				*buffer++ = *it++;
			}
		}
	}
#endif
	return n;
}
コード例 #3
0
int GAConjProblemForORGroupSolver::roulette( int num , double* d)
{
  int i;
  int max  = 100;
  int part = rnd1( max );
  double sum = 0 , s = 0;
  for( i=0 ; i<num ; ++i ) sum += d[i];
  for( i=0 ; i<num ; ++i ) {
    s += d[i];
    if( part <= s*max/sum && d[i]!=0 )
      return i;
  }
  return num-1;
}
コード例 #4
0
ファイル: txs.c プロジェクト: hclander/daemons
int runSimulator(char *ip, int port) {

	int bear;
	int knots;
	int mps=0;
	int myTime =0;
	int lapse=defaultLapse;
	long x, y, d;
	float lat, lon;
	frm_cmd_gps_t gps;
	char buff[TRANS_MAX_BUFF_SIZE];
	size_t transLen;
	int seq =0;
	int sckt;
	struct sockaddr_in server;

	// INIT SOCKET
	printf("Incializando...\n");
	memset(&server,0,sizeof(server));

	sckt= socket(AF_INET,SOCK_DGRAM,0);

	if (sckt<0) {
			die("Error en la creacion del socket");
	}

	server.sin_family = AF_INET;

	server.sin_addr.s_addr = inet_addr(ip);
	server.sin_port = htons(port);



	y = lat2y(42.22);
	x = lon2x(-8.65);
	printf("Empezando simulacion...(lapso=%ds)\n\n",lapse);

	while (true) {

		if (myTime<=0) {
			myTime = rnd1(30);
			bear = rnd0(359);
			mps  = rnd1(MAX_MPS);
		}

		d = mps * lapse;

		// Ojo origen de rumbo PI
		y += round( d * cos(bear * DEG_RAD));
		x += round( d * sin(bear * DEG_RAD));

		lat = y2lat(y);
		lon = x2lon(x);
		knots = mps * MPS_TO_KNOTS;

//		gps.cmd = 0x11;
//		gps.len =0;
//		gps.seq_l= seq++;
//		gps.seq_s=0;
//		gps.bearing = GPS_ENCODE_BEARING(bear);
//		gps.knots = knots;
//		gps.lat_sign = lat<0?1:0;
//		gps.lat_deg = gps.lat_sign?-lat:lat;
//		gps.lat_min =htons(GPS_ENCODE_LOCMIN(lat));
//		gps.lon_sign = lon<0?1:0;
//		gps.lon_deg = gps.lon_sign?-lon:lon;
//		gps.lon_min = htons(GPS_ENCODE_LOCMIN(lon));
//
//		gps.fix = 1;
//		gps.hdop = rnd1(20);
//		gps.time.epoch = htonl(time(NULL));

		int offset=0;
		transLen = sizeof(buff);

		frame_encode_gps(seq++,bear,knots,lat,lon,1,rnd1(20),0,buff,&transLen);

		offset +=transLen;

		if (addCnxFrame) {

			transLen = sizeof(buff) - offset;
			frame_encode_cnx("1234567890",0,1,0,buff+offset,&transLen);
			offset +=transLen;

		}

		transLen = sizeof(buff);

		if ( frame_encode_transport(1,NULL,offset,buff,&transLen) ) {

//			if (frame_test_transport(buff,transLen)) {
//				printf("Trama de transporte verificada\n");
//			}

			printf("Enviando: %d lat=%f lon=%f bear=%d knots=%d time=%d\n",seq,
					lat,lon,bear,knots,ntohl(gps.time.epoch));

			sendto(sckt,buff,transLen,0,(struct sockaddr *)&server,sizeof(server));

		}

		myTime -=lapse;
		sleep(lapse);
	}

	close(sckt);
	return 0;

}
コード例 #5
0
ファイル: 10428.c プロジェクト: DavidToca/acm
double rnd(double a, double b)
{
	return (a + ((b - a) * rnd1()));
}
コード例 #6
0
ファイル: txs_prj.c プロジェクト: hclander/daemons
int runSimulator(char *ip, int port) {

	int bear;
	int knots;
	int mps=0;
	int myTime =0;
	int lapse=defaultLapse;
	long x, y, d;
	float lat, lon;
	frm_cmd_gps_t gps;
	char buff[TRANS_MAX_BUFF_SIZE];
	size_t transLen;
	int seq =0;
	int sckt;
	struct sockaddr_in server;
	time_t timestamp;


	PROJ *p;
	PROJ_XY xy;
	PROJ_LP lp;

	// INIT SOCKET
	printf("Incializando...\n");
	memset(&server,0,sizeof(server));

	sckt= socket(AF_INET,SOCK_DGRAM,0);

	if (sckt<0) {
			die("Error en la creacion del socket");
	}

	server.sin_family = AF_INET;

	server.sin_addr.s_addr = inet_addr(ip);
	server.sin_port = htons(port);


	//Projection initialization

	//WGS84
	p = proj_initstr("proj=merc ellps=WGS84");

	lp.phi = ORIG_LAT * D_TO_R;
	lp.lam = ORIG_LON * D_TO_R;

	xy = proj_fwd(lp,p);

	printf("Empezando simulacion...(lapso=%ds)\n\n",lapse);

	while (true) {

		if (myTime<=0) {
			myTime = rnd1(30);
			bear = rnd0(359);
			mps  = rnd1(MAX_MPS);
		}

		d = mps * lapse;

		// Ojo origen de rumbo PI
		xy.y += round( d * cos(bear * D_TO_R));
		xy.x += round( d * sin(bear * D_TO_R));

		lp = proj_inv(xy,p);

		knots = mps * MPS_TO_KNOTS;

		int offset=0;
		transLen = sizeof(buff);


		lat = lp.phi* R_TO_D;
		lon = lp.lam * R_TO_D;

		timestamp = time(NULL);

		frame_encode_gps(seq++,bear,knots,lat,lon,1,rnd1(20),timestamp,buff,&transLen);

		offset +=transLen;

		if (addCnxFrame) {

			transLen = sizeof(buff) - offset;
			frame_encode_cnx("1234567890",0,1,0,buff+offset,&transLen);
			offset +=transLen;

		}

		transLen = sizeof(buff);

		if ( frame_encode_transport(1,NULL,offset,buff,&transLen) ) {

//			if (frame_test_transport(buff,transLen)) {
//				printf("Trama de transporte verificada\n");
//			}

			printf("Enviando: %d lat=%f lon=%f bear=%d knots=%d time=%d\n",seq,
					lat,lon,bear,knots,timestamp);

			sendto(sckt,buff,transLen,0,(struct sockaddr *)&server,sizeof(server));

		}

		myTime -=lapse;
		sleep(lapse);
	}

	proj_free(p);
	close(sckt);
	return 0;

}
コード例 #7
0
bool GAConjProblemForORGroupSolver::tournament( GACPforORGSolverGene &gene )
{
  double newFit = gene.fitness( );

  // number of last iterations with conjecture
  // I do next iteration through 1000 iterations
  static int it = theIter1;

  if( newFit==0 ) {
    bestFit = 0;
    lastImprovement = 0;
    if( file )
    *file << "Generation " << theIter1 << ", best fitness " << bestFit << endl << endl;
    return false;
  }

  if( gene.getHasShorterWords( ) ) {
    if( file )
      *file << "Given words can be shortened." << endl << endl;
    toStart( gene.getWord1( ) , gene.getWord2( ) );
    theIter2 = 0;
    return true;
  }

  //Conjecture
  if( gene.getHasConjecture( ) && theIter1 - it>1000 ) {
    Word conjWord = gene.getConjectureWord( );

    int nCheck = 1;
    if( checkedWords.bound(conjWord) )
      nCheck = checkedWords.valueOf( conjWord )+1;

    if( theIter1 - it > 1000*nCheck ) {
      checkedWords.bind( conjWord , nCheck );
      
      if( file ) {
	*file << "The algorithm will try to check whether ";
	theGroup.printWord( *file , conjWord );
	*file << " is trivial.";
	*file << endl << endl;
      }

      GAConjProblemForORGroupConjecture conjecture( theGroup , conjWord , file );
      bool conjResult = conjecture.isConj( 1000 , theIter1 );
      it = theIter1;

      if( conjResult ) {
	bestFit = 0;
	lastImprovement = 0;
	if( file ) {
	  *file << "The word is trivial." << endl << endl;
	  *file << "Generation " << theIter1 << ", best fitness " << bestFit << endl << endl;
	}
	return false;
      }

      if( file ) 
	*file << "The algorithm couldn't determine whether the word was trivial." << endl << endl;    
    }
  }
  
  // get random chromosome and compare it with the new one by roulette wheel
  int g = rnd1( numGenes );
  
  if( newFit<bestFit ) {
    delete genes[g];
    genes[g] = new GACPforORGSolverGene( gene );
    bestFit = newFit;
    lastImprovement = 0;
    if( file )
      *file << "Generation " << theIter1 << ", best fitness " << bestFit << endl << endl;
    return false;
  }
  
  if( !genes[g] )
    genes[g] = new GACPforORGSolverGene( gene );
  else {
    double prob1 = newFit, prob2 = genes[g]->fitness( );
    double min = ( prob1<prob2 ? prob1 : prob2 );
    if( min>5 ) { 
      prob1 -= min - 5;
      prob2 -= min - 5;
    }
    prob1 += fitnessRate;
    prob2 += fitnessRate;

    if( (roulette( prob1 , prob2 )==1 ) ) {
      delete genes[g];
      genes[g] = new GACPforORGSolverGene( gene );
    }
  }
  return false;
}