예제 #1
0
파일: util.c 프로젝트: cran/BigQuic
/*************************************************************************
* This function initializes the random number generator
**************************************************************************/
void InitRandom(int seed)
{
  //SEED SET IN R BEFORE CALLING... ALWAYS CHECK THIS FOR DEBUGGING!
  //For Debugging: 
  //seed = -1;
  //Temp Disabled for debugging, SWITCH BACK TO PCG FOR FINAL
  if (seed == -1)
  {
    // Seed with a fixed constant, unset for production
    pcg32_srandom(42u, 54u);
  }
  else
  {
    // Seed with external entropy -- the time and some program addresses
    // (which will actually be somewhat random on most modern systems).
    // A better solution, entropy_getbytes, using /dev/random, is provided
    // in the full library. 
    pcg32_srandom(seed, seed);
  }
  
  /*
  if (seed == -1) {
#ifndef __VC__
    srand48(7654321L);  
#endif
    srand(4321);  
  }
  else {
#ifndef __VC__
    srand48(seed);  
#endif
    srand(seed);  
  }
  */
}
예제 #2
0
int main(int argc, char** argv)
{
    // Read command-line options

    int count = atoi(argv[1]);
    // use int64_t for Java convenience
    int64_t initState = strtoll(argv[2], NULL, 10);
    int64_t initSeq = strtoll(argv[3], NULL, 10);
    int i;

    // In this version of the code, we'll use the global rng, rather than a
    // local one.

    // You should *always* seed the RNG.  The usual time to do it is the
    // point in time when you create RNG (typically at the beginning of the
    // program).
    //
    // pcg32_srandom_r takes two 64-bit constants (the initial state, and the
    // rng sequence selector; rngs with different sequence selectors will
    // *never* have random sequences that coincide, at all) - the code below
    // shows three possible ways to do so.

    pcg32_srandom((uint64_t) initState, (uint64_t) initSeq);

    for (i = 0; i < count; ++i) {
        printf("%d\n", (int32_t) pcg32_random());
    }

    return 0;
}
예제 #3
0
int main(int argc, char** argv)
{

  unsigned long seeds[] = {1, 589494289, 11, 31415, 2415065787, 111111,
               589494289+1000, 520326001786743721, 54553810781164123,
               845796262492190295, 944310181042360313,
               7, 17, 314159265, 271828182845, 0xfffffff};
 
  int seedi = 1;
  unsigned long seed;
  long inc = 1;
  int crush=0;
  int smallcrush=1;
  int i;

  for(i = 1; i < argc; i++ ) {
    if (0 == strcmp("--seedi", argv[i])) {
      i++;
      seedi = atoi(argv[i]);
    } else if (0 == memcmp("--seedi=", argv[i], strlen("--seedi="))) {
      seedi = atoi(&argv[i][strlen("--seedi=")]);
    } else if(0 == strcmp("--smallcrush", argv[i]) ||
              0 == strcmp("--smallcrush=true", argv[i])) {
      smallcrush=1;
      crush=0;
    } else if(0 == strcmp("--crush", argv[i]) ||
              0 == strcmp("--crush=true", argv[i]) ) {
      smallcrush=0;
      crush=1;
    }
  }

  seed = seeds[seedi-1];

  printf("seed is %lu inc is %lu\n", seed, inc);

  pcg32_srandom(seed, inc);

  if (smallcrush) run_smallcrush_testu01_uint("PCG-C-32");
  if (crush) run_crush_testu01_uint("PCG-C-32");

  return 0;
}
예제 #4
0
int main(int argc, char const * argv[]) {
	const char * config_file;
	int connection_numer = 0, ret, pid;
	short vals[SEM_SIZE] = {1, 1};
	key_t key = ftok("database.sql", KEY_ID);
	key_t key_db = ftok("database.sql", KEY_DB_ID);

	switch(argc) {
		case 1: {
			config_file = CONFIG_FILE_DEFAULT;
		} break;

		case 2: {
			config_file = argv[1];
		} break;

		default: {
			fprintf(stderr, "Usage: 'server.app [config_file]'.\n");
			exit(EXIT_FAILURE);
		}
	}

	pcg32_srandom(time(NULL), (intptr_t)&connection_numer);

	server_sems = sem_make(key, SEM_SIZE, vals);
	if(server_sems == -1) {
		fprintf(stderr, "1 Can't create neccessary data to operate.\n");
		exit(EXIT_FAILURE);
	}

	bettors = mmap(NULL, sizeof(*bettors), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
	if(bettors == MAP_FAILED) {
		fprintf(stderr, "2 Can't create neccessary data to operate.\n");
		exit(EXIT_FAILURE);
	}

	clients = mmap(NULL, sizeof(*clients), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
	if(clients == MAP_FAILED) {
		fprintf(stderr, "3 Can't create neccessary data to operate.\n");
		exit(EXIT_FAILURE);
	}

	winner = mmap(NULL, sizeof(*winner) * MAX_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
	if(winner == MAP_FAILED) {
		fprintf(stderr, "4 Can't create neccessary data to operate.\n");
		exit(EXIT_FAILURE);
	}

	*bettors = 0;
	*clients = 0;
	memset(winner, 0, sizeof(*winner) * MAX_SIZE);

	if(!log_open()) {
		fprintf(stderr, "Can't connect logging server.\n");
		//exit(EXIT_FAILURE);
	}

	database = smemory_open(key_db); // TODO: define 8080
	if(database == NULL) {
		fprintf(stderr, "Can't reach the database.\n");
		exit(EXIT_FAILURE);
	}

	connection = server_open(config_file);
	if(connection == NULL) {
		fprintf(stderr, "Can't create the main connection.\n");
		exit(EXIT_FAILURE);
	}

	signal(SIGINT, handle_int);
	signal(SIGCHLD, handle_int);

	while(TRUE) {
		connection_t connection_accepted;

		connection_accepted = server_accept(connection);
		if(connection_accepted == NULL) {
			log_send(LEVEL_ERROR, "[MAIN SV] Can't connect to client.");
			exit(EXIT_FAILURE); // TODO: Exit?
		}

		connection_numer++;
		(*clients)++;

		pid = fork();
		if(pid == -1) {
			log_send(LEVEL_ERROR, "[MAIN SV] Can't assign resources to client.");
			exit(EXIT_FAILURE); // TODO: Exit?
		}

		if(!pid) { // Child process
			// sem_lock(server_sems, 1);
			// (*clients)++;
			// sem_unlock(server_sems, 1);
			server_ajar(connection);
			log_send(LEVEL_INFO, "[CHILD SV] Disconnecting main connection.");

			ret = handle(connection_accepted);
			if(ret == EXIT_FAILURE) {
				log_send(LEVEL_ERROR, "[CHILD SV] There was an error handling client.");
			}

			server_close(connection_accepted);
			log_send(LEVEL_INFO, "[CHILD SV] Closed client connection.");
			// sem_lock(server_sems, 1);
			// (*clients)--;
			// sem_unlock(server_sems, 1);
			exit(ret);
		}

		log_send(LEVEL_INFO, "[MAIN SV] Disconnecting new connection.");
		server_ajar(connection_accepted);
	}

	server_close(connection);
	if(!db_close(database)) {
		log_send(LEVEL_ERROR, "[MAIN SV] Couldn't correctly logout from database.");
	}
	log_close();
	sem_remove(server_sems);

	return 0;
}
예제 #5
0
파일: space.c 프로젝트: pt300/spaceshit
int main(int argc, char* argv[]) {
	char flags = 0;
	//char* box;
	uint16_t* scr; //also going to store info about whether it's '.' or '*'. 9 bit values dun exist :/ will try doing that later. It's too much butt pain inducing
	char* msgbox = NULL;
	int bxs = 0;
	int bhs[5];
	int mv[3];
	struct winsize w;
	ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
	if(argc>1) {
		//parse arggggggggggggggggggggsss
		int ac;
		while((ac = getopt (argc, argv, "hsb:")) != -1) {
			switch(ac) {
			case 'h':
				; //fug you stupid standards
				char* ctp = strrchr(argv[0], '/');
				if(!ctp)
					ctp = argv[0];
				else
					ctp++;
				printf("%s: Generate colorful space in console.\n"
						" Optional arguments:\n"
						"   -s\tSkip generation phase and instantly print out entire space.\n"
						"   -b\tPrint out floating window with text provided as argument to that option.\n", ctp);
				exit(0);
				break;
			case 's':
				flags|=1;
				break;
			case 'b':
				if(!(flags&2)) {
					int stl = strlen(optarg);
					bxs = (stl > (w.ws_col/2)) ? w.ws_col/2 : stl;
					msgbox = malloc((bxs+8)*6+(bxs+6)*3+6*3+5);
					bxs += 6;
					char* bci = msgbox;
					int bcfi = bxs;
					strcpy(bci, "┌");
					bci+=3;
					while(bcfi--) {
						strcpy(bci, "─");
						bci+=3;
					}
					strcpy(bci, "┐");
					bci+=3;
					*(bci++) = '\0';
					strcpy(bci, "│");
					bci+=3;
					bcfi = bxs;
					while(bcfi--) {
						*(bci++) = ' ';
					}
					strcpy(bci, "│");
					bci+=3;
					*(bci++) = '\0';
					strcpy(bci, "│");
					bci+=3;
					bcfi = 3;
					while(bcfi--) {
						*(bci++) = ' ';
					}
					strncpy(bci, optarg, bxs-6);
					bci+=bxs-6;
					bcfi = 3;
					while(bcfi--) {
						*(bci++) = ' ';
					}
					strcpy(bci, "│");
					bci+=3;
					*(bci++) = '\0';
					strcpy(bci, "│");
					bci+=3;
					bcfi = bxs;
					while(bcfi--) {
						*(bci++) = ' ';
					}
					strcpy(bci, "│");
					bci+=3;
					*(bci++) = '\0';
					strcpy(bci, "└");
					bci+=3;
					bcfi = bxs;
					while(bcfi--) {
						strcpy(bci, "─");
						bci+=3;
					}
					strcpy(bci, "┘");
					bci+=3;
					*(bci++) = '\0';
					bxs += 2;
					bhs[0] = bxs*3;
					bhs[1] = bxs+4;
					bhs[2] = bxs+4;
					bhs[3] = bxs+4;
					bhs[4] = bxs*3;
					//oh god that's looking so awful
				}
				flags|=2;
				break;
			case '?':
				if (optopt == 'b') {
					exit(0);
				}
				break;
			}
		}
	}
	//scr = calloc(((((w.ws_col-1)*(w.ws_row-1))*9)/16)+1, sizeof(uint16_t));
	scr = calloc((w.ws_col+2)*(w.ws_row+2), sizeof(uint16_t));
	signal(SIGINT, finish);
	struct termios new_settings;
	tcgetattr(0,&stored_settings);
	new_settings = stored_settings;
	new_settings.c_lflag &= ~(ECHO | ICANON);
	tcsetattr(0,TCSANOW,&new_settings);
	pcg32_srandom(time(NULL) ^ (intptr_t)&printf, rand());
	int cnt = w.ws_row;
	puts("\e[?25l");
	while(--cnt) {
		putchar('\n');
	}
	cnt = w.ws_row*w.ws_col/15;
	w.ws_col++;
	w.ws_row++;
	int cpc[4];
	while(cnt--) {
		cpc[0] = (int)pcg32_boundedrand(w.ws_row);
		cpc[1] = (int)pcg32_boundedrand(w.ws_col);
		cpc[2] = (int)pcg32_boundedrand(216)+17;
		cpc[3] = pcg32_boundedrand(10)?1:0;
		*(scr+cpc[1]+cpc[0]*w.ws_col) = cpc[2]|(cpc[3]<<8);
		printf("\e[%i;%iH\e[38;5;%im%c", cpc[0], cpc[1], cpc[2], cpc[3] ?'.':'*');
/** Sets an integer as random seed */
void   random_seed(int seed)
{
	pcg32_srandom((0x853c49e6748fea9bULL ^ seed) | 1, 0xda3e39cb94b95bdbULL);
}