Example #1
0
void generate()
{
	int room_count = 0;
	int min_size = 5;
	int max_size = 15;
	int x;
	int y;
	int i;

	dungeon.map = malloc(MAP_SIZE * sizeof(int *));
	//Check for null

	for (x = 0; x < MAP_SIZE; x++)
	{
		dungeon.map[x] = malloc(MAP_SIZE * sizeof(int));

		//check null
	}

	for (x = 0; x < MAP_SIZE; ++x)
	{
		for (y = 0; y < MAP_SIZE; ++y)
		{
			dungeon.map[x][y] = 0;
		}
	}

	room_count = get_random(20, 40);

	dungeon.rooms = malloc(room_count * sizeof(struct room_t));
	//Check Null

	for (i = 0; i < room_count; i++)
	{
		dungeon.rooms[i] = malloc(sizeof(struct room_t));
		//Check null

		dungeon.rooms[i].x = get_random(1, MAP_SIZE - max_size - 1);
		dungeon.rooms[i].y = get_random(1, MAP_SIZE - max_size - 1);
		dungeon.rooms[i].w = get_random(min_size, max_size);
		dungeon.rooms[i].h = get_random(min_size, max_size);

	}


}
Example #2
0
// 随机的方法洗牌
void PA_Randomize10(int poker[], int num)
{
    srand((unsigned)time(NULL));
    int times = 6 + rand() % 5;

    // 随机洗牌6~10次
    get_random(poker, num, times);
}
Example #3
0
 void generate_query(const TableDef& t) {
     for(std::size_t i=0; i<t.props.size(); i++) {
         if (rand() % 2 == 0) {
             props.push_back(wrap(t.types[i], t.props[i]));
             values.push_back(get_random(t.types[i]));
         }
     }
 }
Example #4
0
long add_noise(struct aSubRecord *psub)
{
    double *a;
    double *b;
    double *c;
    double noise_level;
    a = (double*) psub->a;
    b = (double*) psub->b;
    c = (double*) psub->c;
    noise_level = c[0];
    for(int i=0; i< psub->noa; i++){
        a[i] = a[i]*(1e9) + get_random()*noise_level;
        b[i] = b[i]*(1e9) + get_random()*noise_level;
    }
    psub->vala = a;
    psub->valb = b;
    return 0;
}
Example #5
0
/** send the TCP queries and print answers */
static void
send_em(const char* svr, int udp, int usessl, int noanswer, int num, char** qs)
{
	sldns_buffer* buf = sldns_buffer_new(65553);
	int fd = open_svr(svr, udp);
	int i;
	SSL_CTX* ctx = NULL;
	SSL* ssl = NULL;
	if(!buf) fatal_exit("out of memory");
	if(usessl) {
		ctx = connect_sslctx_create(NULL, NULL, NULL);
		if(!ctx) fatal_exit("cannot create ssl ctx");
		ssl = outgoing_ssl_fd(ctx, fd);
		if(!ssl) fatal_exit("cannot create ssl");
		while(1) {
			int r;
			ERR_clear_error();
			if( (r=SSL_do_handshake(ssl)) == 1)
				break;
			r = SSL_get_error(ssl, r);
			if(r != SSL_ERROR_WANT_READ &&
				r != SSL_ERROR_WANT_WRITE) {
				log_crypto_err("could not ssl_handshake");
				exit(1);
			}
		}
		if(1) {
			X509* x = SSL_get_peer_certificate(ssl);
			if(!x) printf("SSL: no peer certificate\n");
			else {
				X509_print_fp(stdout, x);
				X509_free(x);
			}
		}
	}
	for(i=0; i<num; i+=3) {
		printf("\nNext query is %s %s %s\n", qs[i], qs[i+1], qs[i+2]);
		write_q(fd, udp, ssl, buf, (uint16_t)get_random(), qs[i],
			qs[i+1], qs[i+2]);
		/* print at least one result */
		if(!noanswer)
			recv_one(fd, udp, ssl, buf);
	}

	if(usessl) {
		SSL_shutdown(ssl);
		SSL_free(ssl);
		SSL_CTX_free(ctx);
	}
#ifndef USE_WINSOCK
	close(fd);
#else
	closesocket(fd);
#endif
	sldns_buffer_free(buf);
	printf("orderly exit\n");
}
Example #6
0
/*
 * Return uniformly distributed random number between
 * low and high.
 */
static int
roll(int low, int high)
{
    int ret;
    ASSERT(low <= high);
    ret = low + (get_random() % (high - low + 1));
    ASSERT(ret >= low && ret <= high);
    return ret;
}
Example #7
0
Matrix* make_diag_dominant(Matrix* matrix)
{
	const fp_t row_sum = get_max_row_sum(matrix);
	for (int i = 0; i < matrix->width; i++) {
		const int index = i * matrix->width + i;
		matrix->elements[index] += get_random(1.5, 2) * (row_sum - fabs(matrix->elements[index]));
	}
	return matrix;
}
Example #8
0
static int _rngfips_rnd(void *_ctx, int level, void *buffer, size_t length)
{
	struct fips_ctx *ctx = _ctx;
	int ret;

	RND_LOCK;
	switch (level) {
	case GNUTLS_RND_RANDOM:
		ret = get_random(&ctx->normal_context, ctx, buffer, length);
	case GNUTLS_RND_KEY:
		ret = get_random(&ctx->strong_context, ctx, buffer, length);
	default:
		ret = get_random(&ctx->nonce_context, ctx, buffer, length);
	}
	RND_UNLOCK;

	return ret;
}
Example #9
0
/*
 * Pull apart a client key exchange message. Decrypt the pre-master key (using
 * our RSA private key) and then work out the master key. Initialise the
 * ciphers.
 */
static int ICACHE_FLASH_ATTR process_client_key_xchg(SSL *ssl)
{
    uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
    int pkt_size = ssl->bm_index;
    int premaster_size, secret_length = (buf[2] << 8) + buf[3];
    uint8_t premaster_secret[MAX_KEY_BYTE_SIZE];
    RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
    int offset = 4;
    int ret = SSL_OK;
    
    if (rsa_ctx == NULL)
    {
        ret = SSL_ERROR_NO_CERT_DEFINED;
        goto error;
    }

    /* is there an extra size field? */
    if ((secret_length - 2) == rsa_ctx->num_octets)
        offset += 2;

    PARANOIA_CHECK(pkt_size, rsa_ctx->num_octets+offset);

    /* rsa_ctx->bi_ctx is not thread-safe */
    SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
    premaster_size = RSA_decrypt(rsa_ctx, &buf[offset], premaster_secret,
            sizeof(premaster_secret), 1);
    SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);

    if (premaster_size != SSL_SECRET_SIZE || 
            premaster_secret[0] != 0x03 ||  /* must be the same as client
                                               offered version */
                premaster_secret[1] != (ssl->client_version & 0x0f))
    {
        /* guard against a Bleichenbacher attack */
        if (get_random(SSL_SECRET_SIZE, premaster_secret) < 0)
            return SSL_NOT_OK;

        /* and continue - will die eventually when checking the mac */
    }

#if 0
    print_blob("pre-master", premaster_secret, SSL_SECRET_SIZE);
#endif

    generate_master_secret(ssl, premaster_secret);

#ifdef CONFIG_SSL_CERT_VERIFICATION
    ssl->next_state = IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION) ?  
                                            HS_CERT_VERIFY : HS_FINISHED;
#else
    ssl->next_state = HS_FINISHED; 
#endif

    ssl->dc->bm_proc_index += rsa_ctx->num_octets+offset;
error:
    return ret;
}
/* Clause 2.8.1 */
int generate_stock_level_data(int w_id, int d_id, struct stock_level_t *data)
{
	bzero(data, sizeof(struct stock_level_t));
	data->w_id = w_id;
	data->d_id = d_id;
	data->threshold = get_random(11) + 10;

	return OK;
}
Example #11
0
lorawan_status_t LoRaPHYCN470::set_next_channel(channel_selection_params_t *params,
                                                uint8_t *channel, lorawan_time_t *time,
                                                lorawan_time_t *aggregate_timeoff)
{
    uint8_t channel_count = 0;
    uint8_t delay_tx = 0;

    uint8_t enabled_channels[CN470_MAX_NB_CHANNELS] = {0};

    lorawan_time_t next_tx_delay = 0;
    band_t *band_table = (band_t *) phy_params.bands.table;

    if (num_active_channels(phy_params.channels.mask, 0,
                            phy_params.channels.mask_size) == 0) {

        // Reactivate default channels
        copy_channel_mask(phy_params.channels.mask,
                          phy_params.channels.default_mask,
                          phy_params.channels.mask_size);
    }

    if (params->aggregate_timeoff
            <= _lora_time->get_elapsed_time(params->last_aggregate_tx_time)) {
        // Reset Aggregated time off
        *aggregate_timeoff = 0;

        // Update bands Time OFF
        next_tx_delay = update_band_timeoff(params->joined,
                                            params->dc_enabled,
                                            band_table, phy_params.bands.size);

        // Search how many channels are enabled
        channel_count = enabled_channel_count(params->current_datarate,
                                              phy_params.channels.mask,
                                              enabled_channels, &delay_tx);
    } else {
        delay_tx++;
        next_tx_delay = params->aggregate_timeoff -
                        _lora_time->get_elapsed_time(params->last_aggregate_tx_time);
    }

    if (channel_count > 0) {
        // We found a valid channel
        *channel = enabled_channels[get_random(0, channel_count - 1)];
        *time = 0;
        return LORAWAN_STATUS_OK;
    }

    if (delay_tx > 0) {
        // Delay transmission due to AggregatedTimeOff or to a band time off
        *time = next_tx_delay;
        return LORAWAN_STATUS_DUTYCYCLE_RESTRICTED;
    }

    *time = 0;
    return LORAWAN_STATUS_NO_CHANNEL_FOUND;
}
Example #12
0
File: random.c Project: chyyuu/recc
unsigned int get_input_space_partitioned_random(void){
	unsigned int cl = get_random() % (unsigned int)5;
	switch (cl){
		case 0:{
			return get_random();
		}case 1:{
			return (unsigned int)0;
		}case 2:{
			return (unsigned int)1;
		}case 3:{
			return (unsigned int)4294967295;
		}case 4:{
			return get_random() % (unsigned int)300;
		}default:{
			return 0;
		}
	}
}
Example #13
0
/*
 * Send the initial client hello.
 */
static int send_client_hello(SSL *ssl)
{
    uint8_t *buf = ssl->bm_data;
#if defined(CONFIG_PLATFORM_ESP8266)
    time_t tm = rand();
#else
    time_t tm = time(NULL);
#endif
    uint8_t *tm_ptr = &buf[6]; /* time will go here */
    int i, offset;

    buf[0] = HS_CLIENT_HELLO;
    buf[1] = 0;
    buf[2] = 0;
    /* byte 3 is calculated later */
    buf[4] = 0x03;
    buf[5] = ssl->version & 0x0f;

    /* client random value - spec says that 1st 4 bytes are big endian time */
    *tm_ptr++ = (uint8_t)(((long)tm & 0xff000000) >> 24);
    *tm_ptr++ = (uint8_t)(((long)tm & 0x00ff0000) >> 16);
    *tm_ptr++ = (uint8_t)(((long)tm & 0x0000ff00) >> 8);
    *tm_ptr++ = (uint8_t)(((long)tm & 0x000000ff));
    get_random(SSL_RANDOM_SIZE-4, &buf[10]);
    memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE);
    offset = 6 + SSL_RANDOM_SIZE;

    /* give session resumption a go */
    if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))    /* set initially by user */
    {
        buf[offset++] = ssl->sess_id_size;
        memcpy(&buf[offset], ssl->session_id, ssl->sess_id_size);
        offset += ssl->sess_id_size;
        CLR_SSL_FLAG(SSL_SESSION_RESUME);       /* clear so we can set later */
    }
    else
    {
        /* no session id - because no session resumption just yet */
        buf[offset++] = 0;
    }

    buf[offset++] = 0;              /* number of ciphers */
    buf[offset++] = NUM_PROTOCOLS*2;/* number of ciphers */

    /* put all our supported protocols in our request */
    for (i = 0; i < NUM_PROTOCOLS; i++)
    {
        buf[offset++] = 0;          /* cipher we are using */
        buf[offset++] = ssl_prot_prefs[i];
    }

    buf[offset++] = 1;              /* no compression */
    buf[offset++] = 0;
    buf[3] = offset - 4;            /* handshake size */

    return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
}
//*******************************
void *Init_Data(int count, void *lock, param_t *params)
{
    int ii;
    unsigned long seed = init_random_seed(); // random();
    unsigned long value;

    Values = (unsigned long *)malloc(count*sizeof(unsigned long));
    My_Tree = (rbtree_t *)malloc(sizeof(rbtree_t));
    rb_create(My_Tree, lock);

#ifndef VALIDATE
    if (params->mode == MODE_TRAVERSE || params->mode == MODE_TRAVERSENLN)
    {
        rb_insert(My_Tree, -1, (void *)-1);
        rb_insert(My_Tree, params->scale+1, (void *)(long)params->scale+1);
        //count -= 2;
    }
#endif

#ifdef VALIDATE
    for (value=1; value<=count; value++)
    {
        rb_insert(My_Tree, value, (void *)(value));
    }
#else
    for (ii=0; ii<count; ii++)
    {
        value = get_random(&seed) % params->scale + 1;
        while ( !rb_insert(My_Tree, value, (void *)value) )
        {
            value = get_random(&seed) % params->scale + 1;
        }
#ifdef DEBUG_INSERT
        if (ii%1000 == 0)
        printf("Insert %d %ld\n", ii, value);
        //check_tree();
#endif

        Values[ii] = value;
    }
#endif

    return My_Tree;
}
int Insert(unsigned long *random_seed, param_t *params)
{
    int errors = 0;
    long int_value;

    int_value = get_random(random_seed) % params->scale + 1;
    if (!rb_insert(My_Tree, int_value, (void *)int_value) ) errors++;

    return errors;
}
Example #16
0
int UniformGen::generate(Command *cmd)
{
	if (get_random() < read_frac) {
		cmd->opcode = Command::READ;
	} else {
		cmd->opcode = Command::WRITE;
	}

    unsigned long long int a = (unsigned long long int) ((get_random() * (upper_limit - lower_limit) + lower_limit) * vol_size);
	if (a > max_addr) {
		a = max_addr;
	}
	cmd->lba = a;

	cmd->asu = asu;
	cmd->size = transfer_size;

	return 0;
}
Example #17
0
static void
check_timeout_random_component_dowork (struct context *c)
{
  const int update_interval = 10; /* seconds */
  c->c2.update_timeout_random_component = now + update_interval;
  c->c2.timeout_random_component.tv_usec = (time_t) get_random () & 0x0003FFFF;
  c->c2.timeout_random_component.tv_sec = 0;

  dmsg (D_INTERVAL, "RANDOM USEC=%d", (int) c->c2.timeout_random_component.tv_usec);
}
Example #18
0
int CAimProto::aim_set_pd_info(HANDLE hServerConn, unsigned short &seqno)
{
    unsigned short offset = 0;
    char buf[SNAC_SIZE + TLV_HEADER_SIZE * 3 + 20];
    unsigned short req = 0x09;
    if (m_pd_info_id == 0) {
        m_pd_info_id = get_random();
        req = 0x08;
    }
    aim_writesnac(0x13, req, offset, buf, get_random()); // SSI Edit/Add
    aim_writeshort(0, offset, buf);                      // name length
    aim_writeshort(0, offset, buf);                      // group id (root)
    aim_writeshort(m_pd_info_id, offset, buf);           // buddy id
    aim_writeshort(0x4, offset, buf);                    // pd info id
    aim_writeshort(0x15, offset, buf);                   // size
    aim_writetlvchar(0xca, m_pd_mode, offset, buf);      // pd mode
    aim_writetlvlong(0xcb, 0xffffffff, offset, buf);     // pd mask
    aim_writetlvlong(0xcc, m_pd_flags, offset, buf);     // pd flags
    return aim_sendflap(hServerConn, 0x02, offset, buf, seqno);
}
Example #19
0
/**
 * Set a series of bytes with a random number. Individual bytes are not zero.
 */
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
{
    int i;
    get_random(num_rand_bytes, rand_data);

    for (i = 0; i < num_rand_bytes; i++)
    {
        while (rand_data[i] == 0)  /* can't be 0 */
            rand_data[i] = (uint8_t)(rand());
    }
}
Example #20
0
//returns a value surrounding 0
double gaussian_noise::gaussian_distribution(const double &stddev)
{
    // Box–Muller transform (Marsaglia polar method)
    if (m_hasSpare) {
        m_hasSpare = false;
        return stddev * m_spare;
    }
 
    m_hasSpare = true;
    double u, v, s;
    do {
        u = (get_random() / ((double) get_random_max())) * 2 - 1;
        v = (get_random() / ((double) get_random_max())) * 2 - 1;
        s = u * u + v * v;
    } while(s >= 1 || s == 0);
 
    s = sqrt(-2.0 * log(s) / s);
    m_spare = v * s;
    return stddev * u * s;
}
Example #21
0
/**
 * \brief
 * randomly shuffles an array of chars of size n
 */
void random_shuffle(uint8_t *p, int n)
{
	uint8_t temp;
	int i;
	for (i = n - 1; i > 0; --i) {
		int r = get_random(0, i);
		temp = p[r];
		p[r] = p[i];
		p[i] = temp;
	}
}
int Update(unsigned long *random_seed, param_t *params)
{
    int errors = 0;
    void *value;
    long int_value;

    int_value = get_random(random_seed) % params->scale + 1;
    value = rb_update(My_Tree, int_value);
    if (value == NULL) errors++;

    return errors;
}
int RRead(unsigned long *random_seed, param_t *params)
{
    long int_value;
    void *value;

    int_value = get_random(random_seed) % params->scale + 1;
    value = rb_find(My_Tree, int_value);
    if ((unsigned long)value == int_value)
        return 0;
    else
        return 1;
}
int Read(unsigned long *random_seed, param_t *params)
{
    int read_elem;
    void *value;

    read_elem = get_random(random_seed) % params->size;
    value = rb_find(My_Tree, Values[read_elem]);
    if ((unsigned long)value == Values[read_elem])
        return 0;
    else
        return 1;
}
Example #25
0
/* clients thread */
static void *fn_clients(void *p_data)
{
	int nb = (int)p_data;

	while (1)
	{
		int val = get_random(6);
		psleep(get_random(3));
		pthread_mutex_lock(&store.mutex_stock);
		if (val > store.stock)
		{
			pthread_cond_signal(&store.cond_stock);
			pthread_cond_wait(&store.cond_clients, &store.mutex_stock);
		}
		store.stock = store.stock - val;
		printf("Client %d prend %d du stock, reste, %d en stock!\n",
				nb, val, store.stock);
		pthread_mutex_unlock(&store.mutex_stock);
	}
	return NULL;
}
Example #26
0
triple
norm_dist(void)
{

  float   u1, u2, v1, v2, ss, rad;
  triple  pert;

  ss = 2.0;
  while (ss >= 1.0) {
    u1 = get_random()/100.0;
    u2 = get_random()/100.0;
    v1 = 2.0*u1 - 1.0;  v2 = 2.0*u2 - 1.0;
    ss = v1*v1 + v2*v2;
  }
  if (ss == 0.0) ss += .1;
  rad = -2.0*log(ss)/ss;
  pert.x = v1 * sqrt(rad);
  pert.y = v2 * sqrt(rad);

  return(pert);
}
Example #27
0
	void step_attach_layer()
	{
		MyGUI::Widget* widget = get_random(all_widgets);
		if (!widget) return;
		if (widget->isRootWidget())
		{
			std::string layername = get_layer();
			if (!layername.empty())
				MyGUI::LayerManager::getInstance().attachToLayerNode(layername, widget);
		}
		test_widgets();
	}
Example #28
0
string AmSession::getNewId() {
  struct timeval t;
  gettimeofday(&t,NULL);

  string id = "";

  id += int2hex(get_random()) + "-";
  id += int2hex(t.tv_sec) + int2hex(t.tv_usec) + "-";
  id += int2hex((unsigned int)((unsigned long)pthread_self()));

  return id;
}
Example #29
0
void randomize_ball() {
	unsigned char random;
	if (!game_running) {
		/* Don't want to spoil the surprise during the menu screen */
		return;
	}
	random = get_random() % 2 + 1;
	if (ball_motion.y < 0) {
		random = -random;
	}
	ball_motion.y = random;
}
Example #30
0
void leitor(){
    char* filename = ficheiros[get_random(NB_FILES)];
    int fd = open(filename,O_RDONLY);
    
    
    if (flock(fd,LOCK_SH) == -1){
        perror ("Error Locking the file");
        exit(EXIT_FAILURE);
    }
    
    
    printf("Monitor will check if file %s is consistent...\n", filename);
    
    if (fd == -1) {
        perror ("Error opening file");
        exit (-1);
    }
    
    else {
        char string_to_read[STRING_SZ];
        char first_string[STRING_SZ];
        int  i;
        
        if (read (fd, first_string, STRING_SZ) == -1) {
            perror ("Error reading file");
            exit (-1);
        }
        for (i=0; i<NB_ENTRIES-1; i++) {
            
            if (read (fd, string_to_read, STRING_SZ) == -1) {
                perror ("Error reading file");
                exit (-1);
            }
            
            if (strncmp(string_to_read, first_string, STRING_SZ)) {
                fprintf (stderr, "Inconsistent file: %s\n", filename);
                exit (-1);
            }
        }
        
        if (flock(fd,LOCK_UN) == -1){
            perror ("Error Unlocking the file");
            exit (EXIT_FAILURE);
        }
        
        if (close (fd) == -1)  {
            perror ("Error closing file");
            exit (-1);
        }
    }
    printf("File %s is consistent\n",filename);
}