コード例 #1
0
ファイル: Simulation.cpp プロジェクト: tivtag/TriDraw
void Simulation::mutate_triangles()
{
   if(should_mutate(Constants::AddPolygonMutationRate))
   {
      if((currentVertexCount+3) < Constants::MaximumVertexCount)
      {
         create_random_vertex(currentVertexCount);
      }
   }

   // move triangle
   if(should_mutate(Constants::MovePolygonMutationRate))
   {
      const int triangleCount = currentVertexCount/3;
      if(triangleCount > 0)
      {
         const int triangleIndexA = random_integer(0, triangleCount);
         const int triangleIndexB = random_integer(0, triangleCount);

         const ColorVertex b1 = currentVertices[triangleIndexB + 0];
         const ColorVertex b2 = currentVertices[triangleIndexB + 1];
         const ColorVertex b3 = currentVertices[triangleIndexB + 2];

         currentVertices[triangleIndexB + 0] = currentVertices[triangleIndexA + 0];
         currentVertices[triangleIndexB + 1] = currentVertices[triangleIndexA + 1];
         currentVertices[triangleIndexB + 2] = currentVertices[triangleIndexA + 2];

         currentVertices[triangleIndexA + 0] = b1;
         currentVertices[triangleIndexA + 1] = b2;
         currentVertices[triangleIndexA + 2] = b3;
      }
   }

   // remove triangle   
   if(should_mutate(Constants::RemovePolygonMutationRate))
   {
      const int triangleCount = currentVertexCount/3;
      if(triangleCount > 0)
      {
         const int triangleIndex = random_integer(0, triangleCount);
         const int nextTriangleIndex = triangleIndex + 1;

         for(int vertexIndex = nextTriangleIndex*3; vertexIndex < currentVertexCount; ++vertexIndex)
         {
            currentVertices[vertexIndex - 3] = currentVertices[vertexIndex];
         }

         currentVertexCount -= 3;
      }
   }

   // mutate vertices
   for(int vertexIndex = 0; vertexIndex < currentVertexCount; ++vertexIndex)
   {
      currentVertices[vertexIndex].mutate();
   }
}
コード例 #2
0
ファイル: test.C プロジェクト: SALAM2016/orbiter
void Halls_theorem()
{
	BYTE fname[1000];
	INT xmax = 1000;
	INT ymax = 1000;
	INT Adjacency[] = {
1,1,1,0,0,0,0,0,0,0,0,0,0,
1,0,0,1,1,0,0,0,0,0,0,0,0,
1,0,0,0,0,1,1,0,0,0,0,0,0,
0,1,0,1,0,1,0,0,0,0,0,0,0,
0,1,0,0,1,0,1,0,0,0,0,0,0,
0,0,1,1,0,0,1,0,0,0,0,0,0,
0,0,1,0,0,0,0,1,1,0,0,0,0,
0,0,0,0,1,0,0,0,0,1,1,0,0,
0,0,0,0,0,1,0,0,0,0,0,1,1,
0,0,0,0,0,0,0,1,0,1,0,1,0,
0,0,0,0,0,0,0,1,0,0,1,0,1,
0,0,0,0,0,0,0,0,1,1,0,0,1,
0,0,0,0,0,0,0,0,1,0,1,1,0,
		};
	INT m = 13;
	INT n = 13;
	INT k, a;
	INT f_cartesian = FALSE;
	
	strcpy(fname, "Hall");
	for (k = 0; k < 10; k++) {
		a = random_integer(m * n);
		Adjacency[a] = 1 - Adjacency[a];
		}
	draw_action(fname, xmax, ymax, Adjacency, m, n, f_cartesian);
}
コード例 #3
0
ファイル: game.c プロジェクト: ptigwe/imitation
void game_update_with_proportional_imitate(game_t *game)
{
    int n = game->graph->n;
    
    int i;
    for(i = 0; i < n; ++i)
    {
        int n_i = graph_number_of_neighbours_of(game->graph, i);
        int k = random_integer(n_i);
        
        mpq_t p_i;
        mpq_init(p_i);
        int j = GPOINTER_TO_INT(g_slist_nth_data(game->graph->edges[i], k));
        game_compute_p_i(game, i, j, p_i);
        
        gboolean imitate = random_boolean_with_probability(p_i);
        
        //printf("%d copying %d\n", i, j);
        if(imitate)
        {
            //printf("%d copied %d\n", i, j);
            game->next_config[i] = game->current_config[j];
        }
        else
        {
            game->next_config[i] = game->current_config[i];
        }
        mpq_clear(p_i);
    }
    
    for(i = 0; i < n; ++i)
    {
        game->current_config[i] = game->next_config[i];
    }
}
コード例 #4
0
ファイル: merge.c プロジェクト: stephenbalaban/programming
int *
random_array(int len)
{
	int *arr = malloc(len * sizeof(int));
	for (int i = 0; i < len; i++) {
		arr[i] = random_integer();
	}
	return arr;
}
コード例 #5
0
ファイル: int-set.c プロジェクト: svn2github/gwyddion
static void
int_set_randomize(GwyIntSet *intset,
                  GRand *rng)
{
    guint size = g_rand_int_range(rng, 0, 20) + g_rand_int_range(rng, 0, 10);

    for (guint i = 0; i < size; i++)
        gwy_int_set_add(intset, random_integer(rng));
}
コード例 #6
0
ファイル: inv.c プロジェクト: Callmedoby/ACM_LeetCode
void demand(void)  /* Demand event function. */
{
    /* Decrement the inventory level by a generated demand size. */

    inv_level -= random_integer(prob_distrib_demand);

    /* Schedule the time of the next demand. */

    time_next_event[2] = sim_time + expon(mean_interdemand);
}
コード例 #7
0
ファイル: noise.cpp プロジェクト: cequencer/signal
void Noise::process(sample **out, int num_frames)
{
	for (int channel = 0; channel < this->num_output_channels; channel++)
	{
		if (this->value[channel] == std::numeric_limits<float>::max())
		{
			// TODO: Put this in an init block that is available to all
			// nodes on their first block?
			this->value[channel] = this->min->out[0][0];
		}

		for (int frame = 0; frame < num_frames; frame++)
		{
			float min = this->min->out[channel][frame];
			float max = this->max->out[channel][frame];
			float frequency = this->frequency->out[channel][frame];
			if (!frequency)
				frequency = this->graph->sample_rate;

			if (this->steps_remaining[channel] <= 0)
			{
				// pick a new target value
				float target = random_uniform(min, max);

				if (frequency > 0)
				{
					this->steps_remaining[channel] = random_integer(0, this->graph->sample_rate / (frequency / 2.0));
					if (this->steps_remaining[channel] == 0)
						this->steps_remaining[channel] = 1;
					this->step_change[channel] = (target - this->value[channel]) / this->steps_remaining[channel];
				}
				else
				{
					this->steps_remaining[channel] = 0;
					this->step_change[channel] = target - this->value[channel];
				}

				if (!this->interpolate)
				{
					this->value[channel] = target;
					this->step_change[channel] = 0;
				}
			}

			this->value[channel] += this->step_change[channel];

			this->out[channel][frame] = this->value[channel];

			this->steps_remaining[channel]--;
		}
	}
}
コード例 #8
0
ファイル: test.c プロジェクト: XianB/DS_Algorithm
int
main(int argc, char *argv[])
{
	int ar[1000];
	int i = 0;

	random_integer(ar, 1, 20);

	for (i = 0; i < 10; i++) {
		printf("%d ", ar[i]);
	}

	printf("\n");
	return 0;

}
コード例 #9
0
ファイル: Simulation.cpp プロジェクト: tivtag/TriDraw
bool Simulation::load_content(AssetLoader& assetLoader)
{  
   // Initialize content and effects
   const std::string ImageAssets[] = { 
      std::string("austria-flag.jpg"),
      std::string("metro-1.jpg"),
      std::string("metro-2.jpg")
   };

   const std::size_t ImageAssetCount = sizeof(ImageAssets) / sizeof(ImageAssets[0]);

   // Randomly select one of the assets
   const std::size_t index = random_integer(0U, ImageAssetCount);
   const std::string asset = ImageAssets[index];

   if(!texture.loadFromAsset(asset, assetLoader))
   {
      LOGE("Could not load asset.");
      return false;
   }

   return texturedQuadEffect.load();
}
コード例 #10
0
ファイル: game.c プロジェクト: ptigwe/imitation
void game_set_initial_configuration_position(game_t *game)
{
    random_set_seed(time(NULL));
    
    int n = game->graph->n;
    GArray *players = g_array_new(FALSE, FALSE, sizeof(int));
    
    int i;
    for(i = 0; i < n; ++i)
    {
        game->initial_config[i] = 0;
        game->current_config[i] = 0;
        
        g_array_append_val(players, i);
    }
    
    mpq_t e;
    mpq_init(e);
    mpq_set_si(e, game->graph->n, 1);
    mpq_mul(e, game->p_c, e);
    int m = ROUND(mpq_get_d(e));
    mpq_clear(e);
    for(i = 0; i < m; ++i)
    {
        int j = random_integer(n);
        int x = g_array_index(players, int, j);
        
        game->initial_config[x] = 1;
        game->current_config[x] = 1;
        
        g_array_remove_index_fast(players, j);
        n--;
    }
    
    g_array_free(players, TRUE);
}
コード例 #11
0
ファイル: b.cpp プロジェクト: wty1990000/TY_repository
/* Program extracts from Appendix B of
コード例 #12
0
ファイル: keygen.c プロジェクト: toma2004/Crytography
/*
 * Generate key and print it out to stdout
 */
void generate_key(char *pphrase, int period)
{
	char state[] = "abcdefghijklmnopqrstuvwxyz";
	int L = 26;
	/*
	 * Perform all the initialization before calling random integer function
	 */
	/*
	 * Below code is from Professor's HW2 webpage at: http://merlot.usc.edu/cs531-s16/homeworks/hw2/
	 * I did not write the code, but modified to suite my need
	 */
	unsigned char md5_buf[MD5_DIGEST_LENGTH];
	int hash_len = MD5_DIGEST_LENGTH + 2 + strlen(pphrase);
	char *s = malloc(hash_len+1);
	int i = 0;
	MD5((unsigned char*)pphrase, strlen(pphrase), md5_buf); //initialize MD5
	/*
	 * end code I did not write
	 */

	unsigned char *key = NULL;
	unsigned x1 = 0;
	unsigned x2 = 0;
	int r = 0;
	for(int j = 0; j < period; j++)
	{
		for(int k = 0; k < 2; k++)
		{
			while (L > 1)
			{
				if (x1 == 0 && x2 == 0)
				{
					if (key != NULL)
					{
						free(key);
					}
					unsigned char *key = random_integer(pphrase, s, hash_len, i, md5_buf);
					x1 = (unsigned char) *key << 24 | (unsigned char) *(key+1) << 16 | (unsigned char) *(key+2) << 8 | (unsigned char) *(key+3); //Get my first unsigned int
					x2 = (unsigned char) *(key+4) << 24 | (unsigned char) *(key+5) << 16 | (unsigned char) *(key+6) << 8 | (unsigned char) *(key+7); //Get my second unsigned int
					i++;
					if (i == 100)
					{
						i = 0;
					}
				}

				if (x1 != 0)
				{
					r = x1 % L;
					x1 = 0;
				}
				else
				{
					r = x2 % L;
					x2 = 0;
				}

				if (r != L-1)
				{
					char temp = state[r];
					state[r] = state[L-1];
					state[L-1] = temp;
				}
				L -= 1;
			}
			//reset L for next iteration
			L = 26;
		}
		fwrite(state,1,sizeof(state)-1,stdout); //exclude writing the null chacracter at the end of each string
		fwrite("\n", 1, 1, stdout);
		strcpy(state,"abcdefghijklmnopqrstuvwxyz");
	}

	if (key != NULL)
	{
		free(key);
	}
	free(s);
}
コード例 #13
0
ファイル: Random.cpp プロジェクト: MarkZH/Genetic_Chess
bool Random::coin_flip()
{
    return random_integer(0, 1) == 1;
}
コード例 #14
0
ファイル: Random.hpp プロジェクト: tivtag/TriDraw
inline bool should_mutate(const int chance)
{
   return random_integer(0, chance) == 1;
}
コード例 #15
0
ファイル: splay_tree.hpp プロジェクト: cjuniet/pads
 double random_double()
 {
   return (double) random_integer() / M;
 }
コード例 #16
0
ファイル: jobshop.c プロジェクト: Salimlou/Class-Work
void arrive(int new_job)  /* Function to serve as both an arrival event of a job
                             to the system, as well as the non-event of a job's
                             arriving to a subsequent station along its
                             route. */
{
    int station;

    /* If this is a new arrival to the system, generate the time of the next
       arrival and determine the job type and task number of the arriving
       job. */


    if (new_job == 1) {

        event_schedule(sim_time + expon(mean_interarrival, STREAM_INTERARRIVAL),
                       EVENT_ARRIVAL);
        job_type = random_integer(prob_distrib_job_type, STREAM_JOB_TYPE);
        task     = 1;
    }

    /* Determine the station from the route matrix. */

    station = route[job_type][task];

    /* Check to see whether all machines in this station are busy. */

    if (num_machines_busy[station] == num_machines[station]) {

        /* All machines in this station are busy, so place the arriving job at
           the end of the appropriate queue. Note that the following data are
           stored in the record for each job:
             1. Time of arrival to this station.
             2. Job type.
             3. Current task number. */

        transfer[1] = sim_time;
        transfer[2] = job_type;
        transfer[3] = task;
        list_file(LAST, station);
    }

    else {

        /* A machine in this station is idle, so start service on the arriving
           job (which has a delay of zero). */

        sampst(0.0, station);                              /* For station. */
        sampst(0.0, num_stations + job_type);              /* For job type. */
        ++num_machines_busy[station];
        timest((float) num_machines_busy[station], station);

        /* Schedule a service completion.  Note defining attributes beyond the
           first two for the event record before invoking event_schedule. */

        transfer[3] = job_type;
        transfer[4] = task;
        event_schedule(sim_time
                       + erlang(2, mean_service[job_type][task],
                                STREAM_SERVICE),
                       EVENT_DEPARTURE);
    }
}
コード例 #17
0
ファイル: random.hpp プロジェクト: mnewhouse/tselements
 T random_integer()
 {
   return random_integer(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
 }
コード例 #18
0
int Random::random_integer(int low, int high)
/*Post: A random integer number between [low, high)*/
{
	if(low>high) return random_integer(high,low);
	else return((int)((high-low+1)*random_real()))+low;
}
コード例 #19
0
ファイル: sctp.c プロジェクト: TopPano/librtcdc
struct sctp_transport *
create_sctp_transport(struct rtcdc_peer_connection *peer)
{
    if (peer == NULL || peer->transport == NULL)
        return NULL;

    struct sctp_transport *sctp = (struct sctp_transport *)calloc(1, sizeof *sctp);
    if (sctp == NULL)
        return NULL;
    peer->transport->sctp = sctp;
    sctp->local_port = random_integer(10000, 60000);

    if (g_sctp_ref == 0) {
        usrsctp_init(0, sctp_data_ready_cb, NULL);
        usrsctp_sysctl_set_sctp_ecn_enable(1);
    }
    g_sctp_ref++;

    usrsctp_register_address(sctp);
    struct socket *s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP,
            sctp_data_received_cb, NULL, 0, peer);
    if (s == NULL)
        goto trans_err;
    sctp->sock = s;

    BIO *bio = BIO_new(BIO_s_mem());
    if (bio == NULL)
        goto trans_err;
    BIO_set_mem_eof_return(bio, -1);
    sctp->incoming_bio = bio;

    bio = BIO_new(BIO_s_mem());
    if (bio == NULL)
        goto trans_err;
    BIO_set_mem_eof_return(bio, -1);
    sctp->outgoing_bio = bio;


#ifdef DEBUG_SCTP
    int sd;
    struct sockaddr_in stub_addr;
    memset(&stub_addr, 0, sizeof stub_addr);
    inet_pton(AF_INET, "127.0.0.1", &stub_addr.sin_addr);
    stub_addr.sin_family = AF_INET;

    sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    stub_addr.sin_port = htons(60001);
    bind(sd, (const struct sockaddr *)&stub_addr, sizeof stub_addr);
    stub_addr.sin_port = htons(60002);
    connect(sd, (const struct sockaddr *)&stub_addr, sizeof stub_addr);
    sctp->incoming_stub = sd;

    sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    stub_addr.sin_port = htons(60002);
    bind(sd, (const struct sockaddr *)&stub_addr, sizeof stub_addr);
    stub_addr.sin_port = htons(60001);
    connect(sd, (const struct sockaddr *)&stub_addr, sizeof stub_addr);
    sctp->outgoing_stub = sd;
#endif

    struct linger lopt;
    lopt.l_onoff = 1;
    lopt.l_linger = 0;
    usrsctp_setsockopt(s, SOL_SOCKET, SO_LINGER, &lopt, sizeof lopt);

    struct sctp_paddrparams peer_param;
    memset(&peer_param, 0, sizeof peer_param);
    peer_param.spp_flags = SPP_PMTUD_DISABLE;
    peer_param.spp_pathmtu = 1200;
    usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, &peer_param, sizeof peer_param);

    struct sctp_assoc_value av;
    av.assoc_id = SCTP_ALL_ASSOC;
    av.assoc_value = 1;
    usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET, &av, sizeof av);

    uint32_t nodelay = 1;
    usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, sizeof nodelay);

    struct sctp_initmsg init_msg;
    memset(&init_msg, 0, sizeof init_msg);
    init_msg.sinit_num_ostreams = RTCDC_MAX_OUT_STREAM;
    init_msg.sinit_max_instreams = RTCDC_MAX_IN_STREAM;
    usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_INITMSG, &init_msg, sizeof init_msg);

    struct sockaddr_conn sconn;
    memset(&sconn, 0, sizeof sconn);
    sconn.sconn_family = AF_CONN;
    sconn.sconn_port = htons(sctp->local_port);
    sconn.sconn_addr = (void *)sctp;
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
    sconn.sconn_len = sizeof *sctp;
#endif
    usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof sconn);

    sctp->deferred_messages = g_async_queue_new();

    // modified by uniray7
    sctp->peer = peer;
    //====================

    if (0) {
trans_err:
        peer->transport->sctp = NULL;
        usrsctp_finish();
        free(sctp);
        sctp = NULL;
    }

    return sctp;
}
コード例 #20
0
ファイル: generator.C プロジェクト: SALAM2016/orbiter
void generator::test_identify(INT level, INT nb_times, INT verbose_level)
{
	INT f_v = (verbose_level >= 1);
	//INT f_vv = (verbose_level >= 2);
	INT *transporter;
	INT f_implicit_fusion = FALSE;
	INT final_node;
	INT *Elt;
	INT nb_orbits, cnt, r, r2;
	INT *set1;
	INT *set2;
	sims *S;
	longinteger_object go;

	if (f_v) {
		cout << "generator::test_identify, level = " << level << " nb_times = " << nb_times << endl;
		}

	Elt = NEW_INT(A->elt_size_in_INT);
	transporter = NEW_INT(A->elt_size_in_INT);
	nb_orbits = nb_orbits_at_level(level);
	set1 = NEW_INT(level);
	set2 = NEW_INT(level);

	S = Strong_gens->create_sims(0 /*verbose_level*/);

	S->group_order(go);
	cout << "Group of order " << go << " created" << endl;




	for (cnt = 0; cnt < nb_times; cnt++) {
		r = random_integer(nb_orbits);
		if (f_v) {
			cout << "random orbit " << r << " / " << nb_orbits << endl;
			}
		get_set_by_level(level, r, set1);
		if (f_v) {
			cout << "random orbit " << r << " / " << nb_orbits << " is represented by ";
			INT_vec_print(cout, set1, level);
			cout << endl;
			}
		A->random_element(S, Elt, 0 /* verbose_level */);
		A2->map_a_set_and_reorder(set1, set2, level, Elt, 0 /* verbose_level */);
		cout << "mapped set is ";
		INT_vec_print(cout, set2, level);
		cout << endl;

		recognize(this, set2, level, transporter, f_implicit_fusion, 
			final_node, verbose_level);
		
		r2 = final_node - first_oracle_node_at_level[level];
		if (r2 != r) {
			cout << "recognition fails" << endl;
			exit(1);
			}
		else {
			cout << "recognition is successful" << endl;
			}
		}

	delete S;
	FREE_INT(Elt);
	FREE_INT(transporter);
	FREE_INT(set1);
	FREE_INT(set2);
	if (f_v) {
		cout << "generator::test_identify done" << endl;
		}
}