/* RANDOM_SET_CLSF 10nov94 wmt: initialize n_used_list 05jan95 wmt: upgrade calculations to ac-x code 17may95 wmt: Convert from srand/rand to srand48/lrand48 This resets the classification 'clsf to size n-classes and uses choose-random-prototype to randomly initializes any added classes, or all classes with 'random-start. Classes are initialized by choosing one class as a prototype and adding 1% from two others for variance. The function modifies the classification and returns the number of classes and the marginal probability. */ int random_set_clsf( clsf_DS clsf, int n_classes, int delete_duplicates, int display_wts, unsigned int initial_cycles_p, FILE *log_file_fp, FILE *stream) { int n, i, n_data, index_0, *used_list, n_used_list = 0, num_atts = 0, n_others; int index_1, *used_cls_list, n_used_cls_list = 0, m; class_DS *classes, class; float proto_wt, wt_0, wt_1; n_classes = max(1, min((int) ceil((double) n_classes), clsf_DS_max_n_classes(clsf))); adjust_clsf_DS_classes(clsf, n_classes); n_data = clsf->database->n_data; for (i=0; i<clsf->database->n_atts; i++) if (eqstring( clsf->database->att_info[i]->type, "dummy") != TRUE) num_atts++; classes = clsf->classes; proto_wt = (float) n_data / n_classes; wt_0 = 0.95 * proto_wt; n_others = min( num_atts, (int) ceil( (double) (n_data / 2))); wt_1 = (0.05 * proto_wt) / n_others; used_list = (int *) malloc( n_classes * sizeof(int)); used_cls_list = (int *) malloc( (n_others + 1) * sizeof(int)); for (i=0; i<n_classes; i++) used_list[i] = 0; /* primary cases for all classes */ srand48( (long) (get_universal_time())); /* re-init random number generator */ for (n=0; n<n_classes; n++) { class = classes[n]; index_0 = new_random( n_data, used_list, n_used_list); used_list[n_used_list] = index_0; n_used_list++; for (i=0; i<n_others; i++) used_cls_list[i] = 0; /* secondary cases for each class */ used_cls_list[0] = index_0; /* printf("\nclass-index %d index_0 %d\n", n, index_0); */ n_used_cls_list = 1; class->wts = fill( class->wts, 0.0, class->num_wts, n_data); class->wts[index_0] = wt_0; for (m=0; m<n_others; m++) { index_1 = new_random( n_data, used_cls_list, n_used_cls_list); /* printf("index_1 %d ", index_1); */ used_cls_list[n_used_cls_list] = index_1; n_used_cls_list++; class->wts[index_1] = wt_1; } /* printf("\n"); */ /* for (i=0; i<class->num_wts; i++) */ /* printf("%f ", class->wts[i]); */ class->w_j = max( proto_wt, clsf->min_class_wt); } free(used_list); free(used_cls_list); return(initialize_parameters(clsf, display_wts, delete_duplicates, initial_cycles_p, log_file_fp, stream)); }
static void _proc_brd_isreg(struct msg_ac_brd_t *msg, int len, int proto) { if(!__uuid_equ(&msg->header.acuuid[0], &sysstat.acuuid[0])) { if(__uuid_equ(&msg->takeover[0], &sysstat.acuuid[0])) { if(sysstat.sock >= 0) { close(sysstat.sock); sysstat.sock = -1; } _proc_brd(msg, len, proto); } else { /* tell the broadcast ac, ap have reg in other ac */ struct msg_ap_resp_t *resp = malloc(sizeof(struct msg_ap_resp_t)); if(resp == NULL) { sys_warn("Malloc for response failed:%s\n", strerror(errno)); return; } fill_msg_header(resp, MSG_AP_RESP, msg->header.acuuid, new_random(msg->header.mac)); /* calculate chap */ chap_fill_msg_md5(resp, sizeof(*resp), msg->header.random); net_send(proto, -1, &msg->header.mac[0], (void *)resp, sizeof(struct msg_ap_resp_t)); free(resp); } } }
void test_random_rand(){ Random* r = new_random(); setSeed(r, 1); int i; for(i = 0; i < 5; ++i){ printf("nextRand = %d\n", nextRand(r)); } }
void populate_random(particle** p_array, int particles, int x, int y, int z, float dx, float dy, float dz) { /* * This populates the given array with particles, determining their * positions using the standard rand() function. */ // PRECONDITIONS assert(particles > 0); assert(x > 0); assert(y > 0); assert(z > 0); assert(dx > 0); assert(dy > 0); assert(dz > 0); assert(p_array != NULL); // Store the total size of the space float x_space, y_space, z_space; x_space = ((float) x) * dx; y_space = ((float) y) * dy; z_space = ((float) z) * dz; // Loop through the particle memory int particle_index; for (particle_index = 0; particle_index < particles; particle_index++) { // Assign the position p_array[0][particle_index].x = new_random()*x_space; p_array[0][particle_index].y = new_random()*y_space; p_array[0][particle_index].z = new_random()*z_space; } // POSTCONDITIONS //assert(check_particles(the_grid, particles) == 0); }
vertex_t* create_vertices(int nsym2, int nvertex, int maxsucc, int nactive, bool print_input){ vertex_t* vertices; nsym = nsym2; bitset_size = 50*(nsym / (sizeof(unsigned int) * 8)) + 1; Random* r = new_random(); setSeed(r, 1); void* tmp; posix_memalign(&tmp, (size_t) ALIGN_CONSTANT, (size_t) sizeof(vertex_t) * nvertex); vertices = tmp; for(int i = 0; i < nvertex; ++i){ vertices[i].index = i; vertices[i].listed = false; //posix_memalign(&tmp, (size_t) ALIGN_CONSTANT, (size_t) maxsucc); vertices[i].pred_list = NULL; vertices[i].pred_count = 0; //posix_memalign(&tmp, (size_t) ALIGN_CONSTANT, (size_t) maxsucc); vertices[i].succ_list = NULL; vertices[i].succ_count = 0; //vertices[i].pred_list = create_node(NULL); //First element = NULL //vertices[i].succ_list = create_node(NULL); //First element = NULL posix_memalign(&tmp, (size_t) ALIGN_CONSTANT, (size_t) bitset_size); vertices[i].in = tmp; posix_memalign(&tmp, (size_t) ALIGN_CONSTANT, (size_t) bitset_size); vertices[i].out = tmp; posix_memalign(&tmp, (size_t) ALIGN_CONSTANT, (size_t) bitset_size); vertices[i].use = tmp; posix_memalign(&tmp, (size_t) ALIGN_CONSTANT, (size_t) bitset_size); vertices[i].def = tmp; // Instead of using a homegrown calloc_align.. for(int j = 0; j < (int)(nsym / (sizeof(unsigned int) * 8)+1 ); ++j){ vertices[i].in[j] = 0; vertices[i].out[j] = 0; vertices[i].use[j] = 0; vertices[i].def[j] = 0; } } generateCFG(vertices, nvertex, maxsucc, r, print_input); generateUseDef(vertices, nvertex, nsym, nactive, r, print_input); return vertices; }
static void _proc_brd(struct msg_ac_brd_t *msg, int len, int proto) { /* send current ipv4 */ struct msg_ap_reg_t *resp = malloc(sizeof(struct msg_ap_reg_t)); if(resp == NULL) { sys_warn("Malloc for response failed:%s\n", strerror(errno)); return; } /* generate random1 */ fill_msg_header((void *)resp, MSG_AP_REG, msg->header.acuuid, new_random(msg->header.mac)); resp->ipv4 = argument.addr; /* calculate chap: md5sum1 = packet + random0 + password */ chap_fill_msg_md5((void *)resp, sizeof(*resp), msg->header.random); net_send(proto, -1, &msg->header.mac[0], (void *)resp, sizeof(struct msg_ap_reg_t)); free(resp); }
int main() { // These are the dimensions of our volume elements float particle_size = (float)1.0; // These are the dimensions of our space as multiples of dx, dy & dz int x = 10; int y = 10; int z = 50; // This is the total number of particles to simulate int particle_number = 100000; // Make the space we are simulating grid the_grid; // Make the particles we're testing with (could be read from a file) particle* p_array = (particle*) malloc( ((unsigned int)particle_number) * sizeof(particle)); // Fill the array with random particles populate_random(&p_array, particle_number, x, y, z, particle_size, particle_size, particle_size); // Allocate memory and assign neighbourhoods grid_particles(&the_grid, p_array, particle_number, particle_size); // DEBUGGING assert(the_grid.particles != NULL); // Choose a particle randomly from the group particle* test_particle = NULL; int test_probe; while (test_particle == NULL) { test_probe = (int)( ((float)particle_number) * new_random()); if (the_grid.particles[test_probe].id > 0) { test_particle = &(the_grid.particles[test_probe]); } } // DEBUGGING /////////////////////////////////////////////////////////////// int test_x, test_y, test_z; get_index_from_position(&the_grid, test_particle, &test_x, &test_y, &test_z); assert(test_x >= 0); assert(test_y >= 0); assert(test_z >= 0); assert(test_x < the_grid.x_size); assert(test_y < the_grid.y_size); assert(test_z < the_grid.z_size); //////////////////////////////////////////////////////////////////////////// // DEBUGGING assert(test_particle != NULL); assert(test_particle->id != -1); // Find its neighbours through the grid // This will point to an array of neighbours particle* neighbour_array; // This will tell us how long the array is int neighbour_number = -1; get_potential_neighbours_for_particle(&the_grid, test_particle, &neighbour_array, &neighbour_number); // DEBUGGING assert(neighbour_number >= 0); fprintf(stderr, "Potentials found: %i\n", neighbour_number); // Now find which of those are true neighbours particle* true_neighbour_array; int true_neighbour_number = -1; get_true_neighbours_for_particle(&the_grid, test_particle, &true_neighbour_array, &true_neighbour_number); fprintf(stderr, "True neighhbours: %i\n", true_neighbour_number); particle* brute_force_neighbours; int total_neighbours = -1; get_neighbours_brute_force(&the_grid, test_particle, &(brute_force_neighbours), &total_neighbours); fprintf(stderr, "True neighbours from all: %i\n", total_neighbours); assert(total_neighbours == true_neighbour_number); assert(total_neighbours < neighbour_number); // Check every particle found float delta_x, delta_y, delta_z; // Check that all of the "true neighbours" were found during // brute force int index1; // Used for indexing the "true" array int index2; // Used for indexing the "brute" array int found; // 0 means no match yet, 1 means match found for (index1 = 0; index1 < true_neighbour_number; index1++) { found = 0; delta_x = true_neighbour_array[index1].x - test_particle->x; delta_y = true_neighbour_array[index1].y - test_particle->y; delta_z = true_neighbour_array[index1].z - test_particle->z; // Check that the particle is a true neighbour assert((delta_x*delta_x)+(delta_y*delta_y)+(delta_z*delta_z) <= (the_grid.dx * the_grid.dx)); for (index2 = 0; index2 < total_neighbours; index2++) { if (found == 0) { if (true_neighbour_array[index1].id == brute_force_neighbours[index2].id) { found = 1; } } } // Ensure the grid particle was found in the brute force assert(found == 1); } // Now do the same but the other way around for (index2 = 0; index2 < total_neighbours; index2++) { found = 0; delta_x = brute_force_neighbours[index2].x - test_particle->x; delta_y = brute_force_neighbours[index2].y - test_particle->y; delta_z = brute_force_neighbours[index2].z - test_particle->z; for (index1 = 0; index1 < true_neighbour_number; index1++) { if (found == 0) { if (true_neighbour_array[index1].id == brute_force_neighbours[index2].id) { found = 1; } } } // Ensure everything found through brute force was found by the grid assert(found == 1); } // Success return 0; }
void test_w_z_seed(){ Random* r = new_random(); setSeed(r, 1); printf("r.w = %d\nr.z = %d\n", r->w, r->z); }