/*! * interpolate_qls test. * * This function test the interpolation function. */ void interpolate_qls_test(){ int i, status; double a0, f0, df0, a1, f1, df1, a; double quad_tol = 1.e-6; fprintf( stdout, " interpolate_qls: "); status = 0; /* First test the quadratic: it should be exact */ for( i=0; i<10; i++){ if( rand() %2 == 0 ){ a0 = rand_double( 1., 5.); a1 = rand_double( -5., -1.); }else{ a0 = rand_double( -5., -1.); a1 = rand_double( 1., 5.); } f0 = quad_1d( 1, &a0, &df0, NULL); f1 = quad_1d( 1, &a1, &df1, NULL); a = interpolate_qls( a0, f0, df0, a1, f1, df1); if( fabs( a) > quad_tol ){ status += 1; } } /* Test the guassian:*/ if( status == 0 ){ fprintf( stdout, "pass\n" ); }else{ fprintf( stdout, "fail\n" ); } }
void init_rand(double **primary, double **vectors){ debug("init\n"); srand((unsigned int)time((time_t*) NULL)); for (int i = 0; i < options.n; i++){ primary[i] = malloc(options.m * sizeof(double)); if (primary[i] == NULL){ bail_out(EXIT_FAILURE, "malloc primary[%d]", i); } for (int j = 0; j < options.m; j++){ primary[i][j] = rand_double(); } } int vec_len = 0; for (int i = 0; i < NUM_VEC; i++){ if (i%2 == 0){ vec_len = options.m; } else{ vec_len = options.n; } vectors[i] = malloc(vec_len * sizeof(double)); if (vectors[i] == NULL){ bail_out(EXIT_FAILURE, "malloc vectors[%d]\n", i); } for (int j = 0; j < vec_len; j++){ vectors[i][j] = rand_double(); } } debug("in init: %f", vectors[0][0]); }
void Jittered::generate_samples(void) { int n = (int) sqrt((double)num_samples); for (int p = 0; p < num_sets; p++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { Point2D sp((k + rand_double()) / n, (j + rand_double()) / n); samples.push_back(sp); } }
HeadBumpParticle::HeadBumpParticle(Graphics& graphics, Vector<units::Game> center_pos) : sprite_(graphics, "Caret", units::gameToPixel(kSourceX), units::gameToPixel(kSourceY), units::gameToPixel(kSourceWidth), units::gameToPixel(kSourceHeight)), live_timer_(kLifeTime, kTimerStartActive), center_pos_(std::move(center_pos)), particle_a_(0, rand_angle()), particle_b_(0, rand_angle()), game_max_offset_a_{static_cast<units::Game>(rand_double(4.0, 20.0))}, game_max_offset_b_{static_cast<units::Game>(rand_double(4.0, 20.0))} {}
static void gen_path_pixels (void) { int i; srandom (20050728); printf ("static const int babl_num_path_test_pixels = %d;\n\n", BABL_PATH_NUM_TEST_PIXELS); printf ("static const double babl_path_test_pixels[%d] = {\n", BABL_PATH_NUM_TEST_PIXELS * 4); /* add 128 pixels in the valid range between 0.0 and 1.0 */ for (i = 0; i < 256; i++) { printf ("%a, %a, %a, %a,\n", rand_double (), rand_double (), rand_double (), rand_double ()); } /* add 16 pixels between -1.0 and 0.0 */ for (i = 0; i < 16; i++) { printf ("%a, %a, %a, %a,\n", rand_range_double (-1.0, 0.0), rand_range_double (-1.0, 0.0), rand_range_double (-1.0, 0.0), rand_range_double (-1.0, 0.0)); } /* add 16 pixels between 1.0 and 2.0 */ for (i = 0; i < 16; i++) { printf ("%a, %a, %a, %a,\n", rand_range_double (1.0, 2.0), rand_range_double (1.0, 2.0), rand_range_double (1.0, 2.0), rand_range_double (1.0, 2.0)); } for (i = 288; i < BABL_PATH_NUM_TEST_PIXELS; i++) { printf ("%a, %a, %a, %a,\n", rand_double (), rand_double (), rand_double (), rand_double ()); } printf ("};\n\n"); printf ("static const int babl_num_conversion_test_pixels = %d;\n\n", BABL_CONVERSION_NUM_TEST_PIXELS); printf ("static const double *babl_conversion_test_pixels = babl_path_test_pixels;\n\n"); printf ("static const int babl_num_format_test_pixels = %d;\n\n", BABL_FROMAT_NUM_TEST_PIXELS); printf ("static const double *babl_format_test_pixels = babl_path_test_pixels;\n\n"); }
vec_double2 rand_vd(double min, double max) { int i; static v128 val; for (i=0; i<2; i++) val.d[i] = rand_double(min, max); return (val.vd); }
T* balanced_reduction_input_a(void) { T *ptr = new T[BALANCED_REDUCTION_SIZE]; for (int i=0; i<BALANCED_REDUCTION_SIZE; i++) { ptr[i] = (T)rand_double(-1.0, 1.0); } return ptr; }
/** * Generate random literals. * * Generate literals following a power distribution. If `lit_exp` is 1.0, the * distribution is linear. As `lit_exp` grows, the likelihood of small values * increases. * * @param ptr pointer to where to store literals * @param size number of literals to generate * @param lit_exp exponent used for distribution */ static void generate_literals(unsigned char *ptr, size_t size, double lit_exp) { size_t i; for (i = 0; i < size; ++i) { ptr[i] = (unsigned char) (256 * pow(rand_double(), lit_exp)); } }
nbody_solver::nbody_solver( const size_t _DIM, const size_t _N, const real_t _dt_param) : DIM(_DIM), N(_N+1), dt_param(_dt_param), mass(new real_t[N]), pos(new real_t[N*DIM]), vel(new real_t[N*DIM]), acc(new real_t[N*_DIM]), jerk(new real_t[N*_DIM]), old_pos(new real_t[N*_DIM]), old_vel(new real_t[N*_DIM]), old_acc(new real_t[N*_DIM]), old_jerk(new real_t[N*_DIM]), time_elapsed(0), steps_taken(0) { srand (time(NULL)); size_t i,k; for (i = 0; i < _N; ++i) { mass[i] = rand_double(10,1000); for (k = 0; k < DIM; ++k) { real_t r = rand_double(20,100); pos[i*DIM+k] = r; } real_t theta_v = M_PI/2 - atan(pos[i*DIM]/pos[i*DIM+1]); real_t mag_v = 500; vel[i*DIM] = signum(pos[i*DIM + 1]) * cos(theta_v) * mag_v; vel[i*DIM+1] = - signum(pos[i*DIM + 0]) * sin(theta_v) * mag_v; } mass[N-1] = 9999000; pos[DIM*(N-1)] = 60; pos[DIM*(N-1)+1] = 60; vel[DIM*(N-1)] = 0; vel[DIM*(N-1)+1] = 0; advance_step(); }
void House::Build() { double max_step = 8.f; if (sqrt(Surface(*vertices))/City::HumanSize >= 20.f) { max_step = 3.f; } CreatePyramid(rand_double(1.f, max_step)); }
Color Light::Sample(Point const& point, Ray *out_ray, int row, int col) const { Point sample_point(0.0, 0.0, 0.0); double cell_width = 1.0 / cols; double cell_height = 1.0 / rows; if (area) { double offx = rand_double(-0.25, 0.25) / cols; double offy = rand_double(-0.25, 0.25) / rows; double x = (col * cell_width + offx) - 0.5; double y = (row * cell_height + offy) - 0.5; sample_point = Point(x, 0.0, y); } sample_point = transformation.Apply(sample_point); if (out_ray) { *out_ray = Ray(point, Normalized(sample_point - point)); (*out_ray).max_dist = Norm(sample_point - point); } // In future, this may be deferred to a subclass for e.g. multicolored lights return color; }
void stats_report_with_latency(operation_stats* stats, long latency, struct drand48_data* randctx, longlong bytes_read, longlong bytes_written) { int sample_index; stats->total_requests++; stats->total_latency += latency; stats->total_bytes_read += bytes_read; stats->total_bytes_written += bytes_written; stats->interval_requests++; stats->interval_latency += latency; stats->interval_bytes_read += bytes_read; stats->interval_bytes_written += bytes_written; if (latency > stats->interval_max) stats->interval_max = latency; if (latency > stats->total_max) stats->total_max = latency; /* Use Reservoir sampling */ if (stats->interval_requests <= INTERVAL_MAX) sample_index = stats->interval_requests - 1; else sample_index = rand_double(randctx) * stats->interval_requests; if (sample_index < INTERVAL_MAX) stats->interval_latencies[sample_index] = latency; if (stats->total_requests <= TOTAL_MAX) sample_index = stats->total_requests - 1; else sample_index = rand_double(randctx) * stats->total_requests; if (sample_index < TOTAL_MAX) stats->total_latencies[sample_index] = latency; }
void ProduceTree(double measure, opt_binary_region root, unordered_map<vector<string>, opt_binary_region, hasher>& known, unordered_map<vector<string>, double, hasher>& Tree, deque<binary_region >& b_r_Partition, double OPTerV, bool flag_rand){ pair<int, int> OnePartition; double Precise2 = pow(0.5, 30); unordered_map<vector<string>, opt_binary_region, hasher>::iterator itr = known.find(root.code); int partdf = itr->second.postpart; if( partdf==-1 || (!flag_rand && itr->second.postrou > 0.5) || (flag_rand && rand_double()<itr->second.postrou) ){ double density = measure/itr->second.area(); Tree[root.code] = density; if(root.area() > OPTerV-Precise2) { binary_region b_r_region; b_r_region.dim = root.dim; b_r_region.code = root.code; b_r_region.samples = root.samples; b_r_region.ranges = root.ranges; b_r_Partition.push_back(b_r_region); } return; } if(root.area() < OPTerV*2.0-Precise2 && root.area() > OPTerV-Precise2 ){ binary_region b_r_region; b_r_region.dim = root.dim; b_r_region.code = root.code; b_r_region.samples = root.samples; b_r_region.ranges = root.ranges; b_r_Partition.push_back(b_r_region); } opt_binary_region son1, son2; son1 = root; son2 = root; double middle = (root.ranges[partdf].first + root.ranges[partdf].second)/2; son1.ranges[partdf].second = son2.ranges[partdf].first = middle; son1.code[partdf] = son1.code[partdf]+"0"; son2.code[partdf] = son2.code[partdf]+"1"; PtsInReg2sons(son1,son2, root.samples, son1.samples, son2.samples); int num1 = (int)son1.samples.data.size(); int num2 = (int)son2.samples.data.size(); ProduceTree(measure * (num1+0.5) /(num1+num2+1), son1, known, Tree, b_r_Partition,OPTerV, flag_rand); ProduceTree(measure * (num2+0.5) /(num1+num2+1), son2, known, Tree, b_r_Partition, OPTerV, flag_rand); return; }
Complex gaussian() { double x1, x2, r2; do { /* choose x,y in uniform square (-1,-1) to (+1,+1) */ x1 = -1 + 2 * rand_double(); x2 = -1 + 2 * rand_double(); /* see if it is in the unit circle */ r2 = x1 * x1 + x2 * x2; } while (r2 > 1.0 || r2 == 0); /* Box-Muller transform */ double y1 = x1 * sqrt (-2.0 * log (r2) / r2); double y2 = x2 * sqrt (-2.0 * log (r2) / r2); Complex ret = { y1, y2 }; return ret; }
int main(int argc , char ** args){ if(argc < 2){ printf("wrong input\n"); return 0; } double input = atof(args[1]); if(input > 1){ printf("enter number p between 0 and 1\n"); return 0; } srand(time(NULL)); int observe = 0; for (int i = 0; i < RAND_NUM_1; ++i){ if(rand_double() < input) observe++; } printf("for %d number\t\t : %.2lf\n", RAND_NUM_1,(double)observe/RAND_NUM_1); observe=0; for (int i = 0; i < RAND_NUM_2; ++i){ if(rand_double() < input) observe++; } printf("for %d number\t\t : %.2lf\n", RAND_NUM_2,(double)observe/RAND_NUM_2); observe=0; for (int i = 0; i < RAND_NUM_3; ++i){ if(rand_double() < input) observe++; } printf("for %d number\t\t : %.2lf\n", RAND_NUM_3,(double)observe/RAND_NUM_3); observe=0; for (int i = 0; i < RAND_NUM_4; ++i){ if(rand_double() < input) observe++; } printf("for %d number\t : %.2lf\n", RAND_NUM_4,(double)observe/RAND_NUM_4); }
void ip_callback(void *args, void *proc_info_p, struct ip *packet, struct sockaddr *sin) { static size_t idx = 0; divert_t *handle = args; idx++; // drop some inbound packets if (divert_device_inbound(handle, packet)) { if (rand_double() < rate) { printf("Dropped packet %zu\n", idx); return; } } // re-inject packets into TCP/IP stack divert_reinject(handle, packet, -1, sin); }
//Select a solution to use mutation or crossover //using roulette int select_solution(solution* population, int pop_size){ int i; double offset = 0.0; double r = rand_double(); int pick = 0; for (i = 0; i < pop_size; i++) { offset += population[i].relative_fitness; if (r < offset) { pick = i; break; } } return pick; }
void randomvals_int (t_randomvals *x, t_atom_long value) { t_rand_gen *gen = &x->gen; double *means = x->means; double *devs = x->devs; double *weights = x->weights; double *lo_bounds = x->lo_bounds; double *hi_bounds = x->hi_bounds; double randval, mean, dev, lo_bound, hi_bound; long i; long num_params = x->num_params; if (value >= 2) { // Summed windowed gaussians random distribution // Choose a mean and dev pair based on weighting randval = rand_double_n(gen, weights[num_params - 1]); for (i = 0; i < num_params - 1; i++) if (randval < weights[i]) break; // Generate a windowed gaussian number (between 0 and 1) using a fast implementation mean = means[i]; dev = devs[i]; lo_bound = lo_bounds[i]; hi_bound = hi_bounds[i]; randval = ltqnorm(0.5 + 0.5 * rand_double_range(gen, lo_bound, hi_bound)) * dev + mean; if (randval > 1.) randval = 1.; if (randval < 0.) randval = 0.; } else { // Generate a flat distribution random number between 0 and 1 randval = rand_double(gen); } outlet_float(x->the_outlet, randval); }
/* * Initialize a Life grid. Each cell has a [prob] chance * of starting alive. */ void randomize_grid (struct life_t * life, double prob) { int i,j; int ncols = life->ncols; int nrows = life->nrows; int tcols = life->tcols; int ubound = life->ubound; int lbound = life->lbound; for (i = 1; i <= nrows; i++) { for (j = 1; j <= tcols; j++) { if (rand_double() < prob && j <= ubound+1 && j >= lbound+1) life->grid[i][j - lbound] = ALIVE; } } }
/** * Generate random lengths. * * Generate length frequencies following a power distribution. If `len_exp` is * 1.0, the distribution is linear. As `len_exp` grows, the likelihood of small * values increases. * * @note The size of the frequency table is `NUM_LEN`, the range of possible * length values, while `num` is the number of length values to generate. * * @param len_freq pointer to where to store length frequencies * @param num number of lengths to generate * @param len_exp exponent used for distribution */ static void generate_lengths(unsigned int len_freq[NUM_LEN], size_t num, double len_exp) { size_t i; for (i = 0; i < NUM_LEN; ++i) { len_freq[i] = 0; } for (i = 0; i < num; ++i) { size_t len = (size_t) (NUM_LEN * pow(rand_double(), len_exp)); assert(len < NUM_LEN); len_freq[len]++; } }
int main(int argc, char **argv) { srand(time(NULL)); rand(); if (argc == 2 || argc == 3) { char *hostname = argv[1]; int port = DEFAULT_PORT; if (argc == 3) { port = atoi(argv[2]); } db_disable(); client_enable(); client_connect(hostname, port); client_start(); } if (!glfwInit()) { return -1; } create_window(); if (!window) { glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSwapInterval(VSYNC); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetKeyCallback(window, on_key); glfwSetMouseButtonCallback(window, on_mouse_button); glfwSetScrollCallback(window, on_scroll); #ifndef __APPLE__ if (glewInit() != GLEW_OK) { return -1; } #endif if (db_init()) { return -1; } glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glEnable(GL_LINE_SMOOTH); glLogicOp(GL_INVERT); glClearColor(0.53, 0.81, 0.92, 1.00); GLuint texture; glGenTextures(1, &texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); load_png_texture("texture.png"); GLuint block_program = load_program( "shaders/block_vertex.glsl", "shaders/block_fragment.glsl"); GLuint matrix_loc = glGetUniformLocation(block_program, "matrix"); GLuint camera_loc = glGetUniformLocation(block_program, "camera"); GLuint sampler_loc = glGetUniformLocation(block_program, "sampler"); GLuint timer_loc = glGetUniformLocation(block_program, "timer"); GLuint position_loc = glGetAttribLocation(block_program, "position"); GLuint normal_loc = glGetAttribLocation(block_program, "normal"); GLuint uv_loc = glGetAttribLocation(block_program, "uv"); GLuint line_program = load_program( "shaders/line_vertex.glsl", "shaders/line_fragment.glsl"); GLuint line_matrix_loc = glGetUniformLocation(line_program, "matrix"); GLuint line_position_loc = glGetAttribLocation(line_program, "position"); GLuint item_position_buffer = 0; GLuint item_normal_buffer = 0; GLuint item_uv_buffer = 0; int previous_block_type = 0; Chunk chunks[MAX_CHUNKS]; int chunk_count = 0; Player players[MAX_PLAYERS]; int player_count = 0; FPS fps = {0, 0}; float matrix[16]; float x = (rand_double() - 0.5) * 10000; float z = (rand_double() - 0.5) * 10000; float y = 0; float dy = 0; float rx = 0; float ry = 0; double px = 0; double py = 0; int loaded = db_load_state(&x, &y, &z, &rx, &ry); ensure_chunks(chunks, &chunk_count, floorf(roundf(x) / CHUNK_SIZE), floorf(roundf(z) / CHUNK_SIZE), 1); if (!loaded) { y = highest_block(chunks, chunk_count, x, z) + 2; } glfwGetCursorPos(window, &px, &py); double previous = glfwGetTime(); while (!glfwWindowShouldClose(window)) { update_fps(&fps, SHOW_FPS); double now = glfwGetTime(); double dt = MIN(now - previous, 0.2); previous = now; if (exclusive && (px || py)) { double mx, my; glfwGetCursorPos(window, &mx, &my); float m = 0.0025; rx += (mx - px) * m; ry -= (my - py) * m; if (rx < 0) { rx += RADIANS(360); } if (rx >= RADIANS(360)){ rx -= RADIANS(360); } ry = MAX(ry, -RADIANS(90)); ry = MIN(ry, RADIANS(90)); px = mx; py = my; } else { glfwGetCursorPos(window, &px, &py); } int sz = 0; int sx = 0; ortho = glfwGetKey(window, 'F'); fov = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) ? 15.0 : 65.0; if (glfwGetKey(window, 'Q')) break; if (glfwGetKey(window, 'W')) sz--; if (glfwGetKey(window, 'S')) sz++; if (glfwGetKey(window, 'A')) sx--; if (glfwGetKey(window, 'D')) sx++; float m = dt * 1.0; if (glfwGetKey(window, GLFW_KEY_LEFT)) rx -= m; if (glfwGetKey(window, GLFW_KEY_RIGHT)) rx += m; if (glfwGetKey(window, GLFW_KEY_UP)) ry += m; if (glfwGetKey(window, GLFW_KEY_DOWN)) ry -= m; float vx, vy, vz; get_motion_vector(flying, sz, sx, rx, ry, &vx, &vy, &vz); if (glfwGetKey(window, GLFW_KEY_SPACE)) { if (flying) { vy = 1; } else if (dy == 0) { dy = 8; } } if (glfwGetKey(window, 'Z')) { vx = -1; vy = 0; vz = 0; } if (glfwGetKey(window, 'X')) { vx = 1; vy = 0; vz = 0; } if (glfwGetKey(window, 'C')) { vx = 0; vy = -1; vz = 0; } if (glfwGetKey(window, 'V')) { vx = 0; vy = 1; vz = 0; } if (glfwGetKey(window, 'B')) { vx = 0; vy = 0; vz = -1; } if (glfwGetKey(window, 'N')) { vx = 0; vy = 0; vz = 1; } float speed = flying ? 20 : 5; int step = 8; float ut = dt / step; vx = vx * ut * speed; vy = vy * ut * speed; vz = vz * ut * speed; for (int i = 0; i < step; i++) { if (flying) { dy = 0; } else { dy -= ut * 25; dy = MAX(dy, -250); } x += vx; y += vy + dy * ut; z += vz; if (collide(chunks, chunk_count, 2, &x, &y, &z)) { dy = 0; } } if (y < 0) { y = highest_block(chunks, chunk_count, x, z) + 2; } for (int i = 0; i < chunk_count; i++) { Chunk *chunk = chunks + i; chunk->dirty = 0; } if (left_click) { left_click = 0; int hx, hy, hz; int hw = hit_test(chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz); if (hy > 0 && is_destructable(hw)) { set_block(chunks, chunk_count, hx, hy, hz, 0, 1); } } if (right_click) { right_click = 0; int hx, hy, hz; int hw = hit_test(chunks, chunk_count, 1, x, y, z, rx, ry, &hx, &hy, &hz); if (is_obstacle(hw)) { if (!player_intersects_block(2, x, y, z, hx, hy, hz)) { set_block(chunks, chunk_count, hx, hy, hz, block_type, 1); } } } if (middle_click) { middle_click = 0; int hx, hy, hz; int hw = hit_test(chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz); if (is_selectable(hw)) { block_type = hw; } } if (teleport) { teleport = 0; if (player_count) { int index = rand_int(player_count); Player *player = players + index; x = player->x; y = player->y; z = player->z; rx = player->rx; ry = player->ry; ensure_chunks(chunks, &chunk_count, floorf(roundf(x) / CHUNK_SIZE), floorf(roundf(z) / CHUNK_SIZE), 1); } } client_position(x, y, z, rx, ry); char buffer[RECV_BUFFER_SIZE]; while (client_recv(buffer, RECV_BUFFER_SIZE)) { float ux, uy, uz, urx, ury; if (sscanf(buffer, "U,%*d,%f,%f,%f,%f,%f", &ux, &uy, &uz, &urx, &ury) == 5) { x = ux; y = uy; z = uz; rx = urx; ry = ury; ensure_chunks(chunks, &chunk_count, floorf(roundf(x) / CHUNK_SIZE), floorf(roundf(z) / CHUNK_SIZE), 1); y = highest_block(chunks, chunk_count, x, z) + 2; } int bx, by, bz, bw; if (sscanf(buffer, "B,%*d,%*d,%d,%d,%d,%d", &bx, &by, &bz, &bw) == 4) { set_block(chunks, chunk_count, bx, by, bz, bw, 0); if ((int)roundf(x) == bx && (int)roundf(z) == bz) { y = highest_block(chunks, chunk_count, x, z) + 2; } } int pid; float px, py, pz, prx, pry; if (sscanf(buffer, "P,%d,%f,%f,%f,%f,%f", &pid, &px, &py, &pz, &prx, &pry) == 6) { Player *player = find_player(players, player_count, pid); if (!player && player_count < MAX_PLAYERS) { player = players + player_count; player_count++; player->id = pid; player->position_buffer = 0; player->normal_buffer = 0; player->uv_buffer = 0; printf("%d other players are online\n", player_count); } if (player) { update_player(player, px, py, pz, prx, pry); } } if (sscanf(buffer, "D,%d", &pid) == 1) { delete_player(players, &player_count, pid); printf("%d other players are online\n", player_count); } } for (int i = 0; i < chunk_count; i++) { Chunk *chunk = chunks + i; if (chunk->dirty) { update_chunk(chunk); } } int p = floorf(roundf(x) / CHUNK_SIZE); int q = floorf(roundf(z) / CHUNK_SIZE); ensure_chunks(chunks, &chunk_count, p, q, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); update_matrix_3d(matrix, x, y, z, rx, ry); // render chunks glUseProgram(block_program); glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, matrix); glUniform3f(camera_loc, x, y, z); glUniform1i(sampler_loc, 0); glUniform1f(timer_loc, glfwGetTime()); for (int i = 0; i < chunk_count; i++) { Chunk *chunk = chunks + i; if (chunk_distance(chunk, p, q) > RENDER_CHUNK_RADIUS) { continue; } if (!chunk_visible(chunk, matrix)) { continue; } draw_chunk(chunk, position_loc, normal_loc, uv_loc); } // render players for (int i = 0; i < player_count; i++) { Player *player = players + i; draw_player(player, position_loc, normal_loc, uv_loc); } // render focused block wireframe int hx, hy, hz; int hw = hit_test( chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz); if (is_obstacle(hw)) { glUseProgram(line_program); glLineWidth(1); glEnable(GL_COLOR_LOGIC_OP); glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix); GLuint cube_buffer = make_cube_buffer(hx, hy, hz, 0.51); draw_lines(cube_buffer, line_position_loc, 3, 48); glDeleteBuffers(1, &cube_buffer); glDisable(GL_COLOR_LOGIC_OP); } update_matrix_2d(matrix); // render crosshairs glUseProgram(line_program); glLineWidth(4); glEnable(GL_COLOR_LOGIC_OP); glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix); GLuint line_buffer = make_line_buffer(); draw_lines(line_buffer, line_position_loc, 2, 4); glDeleteBuffers(1, &line_buffer); glDisable(GL_COLOR_LOGIC_OP); // render selected item update_matrix_item(matrix); if (block_type != previous_block_type) { previous_block_type = block_type; make_single_cube( &item_position_buffer, &item_normal_buffer, &item_uv_buffer, 0, 0, 0, 0.5, block_type); } glUseProgram(block_program); glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, matrix); glUniform3f(camera_loc, 0, 0, 5); glUniform1i(sampler_loc, 0); glUniform1f(timer_loc, glfwGetTime()); glDisable(GL_DEPTH_TEST); draw_single_cube( item_position_buffer, item_normal_buffer, item_uv_buffer, position_loc, normal_loc, uv_loc); glEnable(GL_DEPTH_TEST); glfwSwapBuffers(window); glfwPollEvents(); } client_stop(); db_save_state(x, y, z, rx, ry); db_close(); glfwTerminate(); return 0; }
T gaussian_allvars_input_c(void) { return (T)rand_double(1.0, 3.0); }
T gaussian_allvars_input_b(void) { return (T)rand_double(-1.0, 1.0); }
/** * @brief Gets a random double following the exponential function for a given lambda. */ double rand_double_exponential( double lambda ) { return (-1./lambda) * log(rand_double()); }
/** * @brief Gets a random boolean. */ int rand_bool (void) { return (rand_double() > 0.5); }
int main(int argc, char **argv) { srand(time(NULL)); rand(); if (!glfwInit()) { return -1; } create_window(); if (!window) { glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSwapInterval(VSYNC); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetKeyCallback(window, on_key); glfwSetMouseButtonCallback(window, on_mouse_button); #ifndef __APPLE__ if (glewInit() != GLEW_OK) { return -1; } #endif if (db_init()) { return -1; } glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glEnable(GL_LINE_SMOOTH); glLogicOp(GL_INVERT); glClearColor(0.53, 0.81, 0.92, 1.00); GLuint vertex_array; glGenVertexArrays(1, &vertex_array); glBindVertexArray(vertex_array); GLuint texture; glGenTextures(1, &texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); load_png_texture("texture.png"); GLuint block_program = load_program( "shaders/block_vertex.glsl", "shaders/block_fragment.glsl"); GLuint matrix_loc = glGetUniformLocation(block_program, "matrix"); GLuint camera_loc = glGetUniformLocation(block_program, "camera"); GLuint sampler_loc = glGetUniformLocation(block_program, "sampler"); GLuint timer_loc = glGetUniformLocation(block_program, "timer"); GLuint position_loc = glGetAttribLocation(block_program, "position"); GLuint normal_loc = glGetAttribLocation(block_program, "normal"); GLuint uv_loc = glGetAttribLocation(block_program, "uv"); GLuint line_program = load_program( "shaders/line_vertex.glsl", "shaders/line_fragment.glsl"); GLuint line_matrix_loc = glGetUniformLocation(line_program, "matrix"); GLuint line_position_loc = glGetAttribLocation(line_program, "position"); GLuint item_position_buffer = 0; GLuint item_normal_buffer = 0; GLuint item_uv_buffer = 0; int previous_block_type = 0; Chunk chunks[MAX_CHUNKS]; int chunk_count = 0; FPS fps = {0, 0}; float matrix[16]; float x = (rand_double() - 0.5) * 10000; float z = (rand_double() - 0.5) * 10000; float y = 0; float dy = 0; float rx = 0; float ry = 0; double px = 0; double py = 0; int loaded = db_load_state(&x, &y, &z, &rx, &ry); ensure_chunks(chunks, &chunk_count, floorf(roundf(x) / CHUNK_SIZE), floorf(roundf(z) / CHUNK_SIZE), 1); if (!loaded) { y = highest_block(chunks, chunk_count, x, z) + 2; } glfwGetCursorPos(window, &px, &py); double previous = glfwGetTime(); while (!glfwWindowShouldClose(window)) { update_fps(&fps, SHOW_FPS); double now = glfwGetTime(); double dt = MIN(now - previous, 0.2); previous = now; if (exclusive && (px || py)) { double mx, my; glfwGetCursorPos(window, &mx, &my); float m = 0.0025; rx += (mx - px) * m; ry -= (my - py) * m; if (rx < 0) { rx += RADIANS(360); } if (rx >= RADIANS(360)){ rx -= RADIANS(360); } ry = MAX(ry, -RADIANS(90)); ry = MIN(ry, RADIANS(90)); px = mx; py = my; } else { glfwGetCursorPos(window, &px, &py); } if (left_click) { left_click = 0; int hx, hy, hz; int hw = hit_test(chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz); if (hy > 0 && is_destructable(hw)) { set_block(chunks, chunk_count, hx, hy, hz, 0); } } if (right_click) { right_click = 0; int hx, hy, hz; int hw = hit_test(chunks, chunk_count, 1, x, y, z, rx, ry, &hx, &hy, &hz); if (is_obstacle(hw)) { if (!player_intersects_block(2, x, y, z, hx, hy, hz)) { set_block(chunks, chunk_count, hx, hy, hz, block_type); } } } int sz = 0; int sx = 0; ortho = glfwGetKey(window, 'F'); fov = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) ? 15.0 : 65.0; if (glfwGetKey(window, 'Q')) break; if (glfwGetKey(window, 'W')) sz--; if (glfwGetKey(window, 'S')) sz++; if (glfwGetKey(window, 'A')) sx--; if (glfwGetKey(window, 'D')) sx++; if (dy == 0 && glfwGetKey(window, GLFW_KEY_SPACE)) { dy = 8; } float vx, vy, vz; get_motion_vector(flying, sz, sx, rx, ry, &vx, &vy, &vz); if (glfwGetKey(window, 'Z')) { vx = -1; vy = 0; vz = 0; } if (glfwGetKey(window, 'X')) { vx = 1; vy = 0; vz = 0; } if (glfwGetKey(window, 'C')) { vx = 0; vy = -1; vz = 0; } if (glfwGetKey(window, 'V')) { vx = 0; vy = 1; vz = 0; } if (glfwGetKey(window, 'B')) { vx = 0; vy = 0; vz = -1; } if (glfwGetKey(window, 'N')) { vx = 0; vy = 0; vz = 1; } float speed = flying ? 20 : 5; int step = 8; float ut = dt / step; vx = vx * ut * speed; vy = vy * ut * speed; vz = vz * ut * speed; for (int i = 0; i < step; i++) { if (flying) { dy = 0; } else { dy -= ut * 25; dy = MAX(dy, -250); } x += vx; y += vy + dy * ut; z += vz; if (collide(chunks, chunk_count, 2, &x, &y, &z)) { dy = 0; } } int p = floorf(roundf(x) / CHUNK_SIZE); int q = floorf(roundf(z) / CHUNK_SIZE); ensure_chunks(chunks, &chunk_count, p, q, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); update_matrix_3d(matrix, x, y, z, rx, ry); // render chunks glUseProgram(block_program); glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, matrix); glUniform3f(camera_loc, x, y, z); glUniform1i(sampler_loc, 0); glUniform1f(timer_loc, glfwGetTime()); for (int i = 0; i < chunk_count; i++) { Chunk *chunk = chunks + i; if (chunk_distance(chunk, p, q) > RENDER_CHUNK_RADIUS) { continue; } if (!chunk_visible(chunk, matrix)) { continue; } draw_chunk(chunk, position_loc, normal_loc, uv_loc); } // render focused block wireframe int hx, hy, hz; int hw = hit_test(chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz); if (is_obstacle(hw)) { glUseProgram(line_program); glLineWidth(1); glEnable(GL_COLOR_LOGIC_OP); glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix); GLuint buffer = make_cube_buffer(hx, hy, hz, 0.51); draw_lines(buffer, line_position_loc, 3, 48); glDeleteBuffers(1, &buffer); glDisable(GL_COLOR_LOGIC_OP); } update_matrix_2d(matrix); // render crosshairs glUseProgram(line_program); glLineWidth(4); glEnable(GL_COLOR_LOGIC_OP); glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix); GLuint buffer = make_line_buffer(); draw_lines(buffer, line_position_loc, 2, 4); glDeleteBuffers(1, &buffer); glDisable(GL_COLOR_LOGIC_OP); // render selected item update_matrix_item(matrix); if (block_type != previous_block_type) { previous_block_type = block_type; make_single_cube( &item_position_buffer, &item_normal_buffer, &item_uv_buffer, block_type); } glUseProgram(block_program); glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, matrix); glUniform3f(camera_loc, 0, 0, 5); glUniform1i(sampler_loc, 0); glUniform1f(timer_loc, glfwGetTime()); glDisable(GL_DEPTH_TEST); draw_single_cube( item_position_buffer, item_normal_buffer, item_uv_buffer, position_loc, normal_loc, uv_loc); glEnable(GL_DEPTH_TEST); glfwSwapBuffers(window); glfwPollEvents(); } db_save_state(x, y, z, rx, ry); db_close(); glfwTerminate(); return 0; }
static void fill_ctdb_req_control_data(TALLOC_CTX *mem_ctx, struct ctdb_req_control_data *cd, uint32_t opcode) { cd->opcode = opcode; switch (opcode) { case CTDB_CONTROL_PROCESS_EXISTS: cd->data.pid = rand32(); break; case CTDB_CONTROL_STATISTICS: break; case CTDB_CONTROL_PING: break; case CTDB_CONTROL_GETDBPATH: cd->data.db_id = rand32(); break; case CTDB_CONTROL_GETVNNMAP: break; case CTDB_CONTROL_SETVNNMAP: cd->data.vnnmap = talloc(mem_ctx, struct ctdb_vnn_map); assert(cd->data.vnnmap != NULL); fill_ctdb_vnn_map(mem_ctx, cd->data.vnnmap); break; case CTDB_CONTROL_GET_DEBUG: break; case CTDB_CONTROL_SET_DEBUG: cd->data.loglevel = rand_int(5); break; case CTDB_CONTROL_GET_DBMAP: break; case CTDB_CONTROL_PULL_DB: cd->data.pulldb = talloc(mem_ctx, struct ctdb_pulldb); assert(cd->data.pulldb != NULL); fill_ctdb_pulldb(mem_ctx, cd->data.pulldb); break; case CTDB_CONTROL_PUSH_DB: cd->data.recbuf = talloc(mem_ctx, struct ctdb_rec_buffer); assert(cd->data.recbuf != NULL); fill_ctdb_rec_buffer(mem_ctx, cd->data.recbuf); break; case CTDB_CONTROL_GET_RECMODE: break; case CTDB_CONTROL_SET_RECMODE: cd->data.recmode = rand_int(2); break; case CTDB_CONTROL_STATISTICS_RESET: break; case CTDB_CONTROL_DB_ATTACH: fill_ctdb_string(mem_ctx, &cd->data.db_name); assert(cd->data.db_name != NULL); break; case CTDB_CONTROL_SET_CALL: break; case CTDB_CONTROL_TRAVERSE_START: cd->data.traverse_start = talloc(mem_ctx, struct ctdb_traverse_start); assert(cd->data.traverse_start != NULL); fill_ctdb_traverse_start(mem_ctx, cd->data.traverse_start); break; case CTDB_CONTROL_TRAVERSE_ALL: cd->data.traverse_all = talloc(mem_ctx, struct ctdb_traverse_all); assert(cd->data.traverse_all != NULL); fill_ctdb_traverse_all(mem_ctx, cd->data.traverse_all); break; case CTDB_CONTROL_TRAVERSE_DATA: cd->data.rec_data = talloc(mem_ctx, struct ctdb_rec_data); assert(cd->data.rec_data != NULL); fill_ctdb_rec_data(mem_ctx, cd->data.rec_data); break; case CTDB_CONTROL_REGISTER_SRVID: break; case CTDB_CONTROL_DEREGISTER_SRVID: break; case CTDB_CONTROL_GET_DBNAME: cd->data.db_id = rand32(); break; case CTDB_CONTROL_ENABLE_SEQNUM: cd->data.db_id = rand32(); break; case CTDB_CONTROL_UPDATE_SEQNUM: cd->data.db_id = rand32(); break; case CTDB_CONTROL_DUMP_MEMORY: break; case CTDB_CONTROL_GET_PID: break; case CTDB_CONTROL_GET_RECMASTER: break; case CTDB_CONTROL_SET_RECMASTER: cd->data.recmaster = rand_int(32); break; case CTDB_CONTROL_FREEZE: break; case CTDB_CONTROL_THAW: break; case CTDB_CONTROL_GET_PNN: break; case CTDB_CONTROL_SHUTDOWN: break; case CTDB_CONTROL_GET_MONMODE: break; case CTDB_CONTROL_TCP_CLIENT: cd->data.conn = talloc(mem_ctx, struct ctdb_connection); assert(cd->data.conn != NULL); fill_ctdb_connection(mem_ctx, cd->data.conn); break; case CTDB_CONTROL_TCP_ADD: cd->data.conn = talloc(mem_ctx, struct ctdb_connection); assert(cd->data.conn != NULL); fill_ctdb_connection(mem_ctx, cd->data.conn); break; case CTDB_CONTROL_TCP_REMOVE: cd->data.conn = talloc(mem_ctx, struct ctdb_connection); assert(cd->data.conn != NULL); fill_ctdb_connection(mem_ctx, cd->data.conn); break; case CTDB_CONTROL_STARTUP: break; case CTDB_CONTROL_SET_TUNABLE: cd->data.tunable = talloc(mem_ctx, struct ctdb_tunable); assert(cd->data.tunable != NULL); fill_ctdb_tunable(mem_ctx, cd->data.tunable); break; case CTDB_CONTROL_GET_TUNABLE: fill_ctdb_string(mem_ctx, &cd->data.tun_var); assert(cd->data.tun_var != NULL); break; case CTDB_CONTROL_LIST_TUNABLES: break; case CTDB_CONTROL_MODIFY_FLAGS: cd->data.flag_change = talloc(mem_ctx, struct ctdb_node_flag_change); assert(cd->data.flag_change != NULL); fill_ctdb_node_flag_change(mem_ctx, cd->data.flag_change); break; case CTDB_CONTROL_GET_ALL_TUNABLES: break; case CTDB_CONTROL_GET_TCP_TICKLE_LIST: cd->data.addr = talloc(mem_ctx, ctdb_sock_addr); assert(cd->data.addr != NULL); fill_ctdb_sock_addr(mem_ctx, cd->data.addr); break; case CTDB_CONTROL_SET_TCP_TICKLE_LIST: cd->data.tickles = talloc(mem_ctx, struct ctdb_tickle_list); assert(cd->data.tickles != NULL); fill_ctdb_tickle_list(mem_ctx, cd->data.tickles); break; case CTDB_CONTROL_DB_ATTACH_PERSISTENT: fill_ctdb_string(mem_ctx, &cd->data.db_name); assert(cd->data.db_name != NULL); break; case CTDB_CONTROL_UPDATE_RECORD: cd->data.recbuf = talloc(mem_ctx, struct ctdb_rec_buffer); assert(cd->data.recbuf != NULL); fill_ctdb_rec_buffer(mem_ctx, cd->data.recbuf); break; case CTDB_CONTROL_SEND_GRATUITOUS_ARP: cd->data.addr_info = talloc(mem_ctx, struct ctdb_addr_info); assert(cd->data.addr_info != NULL); fill_ctdb_addr_info(mem_ctx, cd->data.addr_info); break; case CTDB_CONTROL_TRANSACTION_START: cd->data.tid = rand32(); break; case CTDB_CONTROL_TRANSACTION_COMMIT: cd->data.tid = rand32(); break; case CTDB_CONTROL_WIPE_DATABASE: cd->data.transdb = talloc(mem_ctx, struct ctdb_transdb); assert(cd->data.transdb != NULL); fill_ctdb_transdb(mem_ctx, cd->data.transdb); break; case CTDB_CONTROL_UPTIME: break; case CTDB_CONTROL_START_RECOVERY: break; case CTDB_CONTROL_END_RECOVERY: break; case CTDB_CONTROL_RELOAD_NODES_FILE: break; case CTDB_CONTROL_TRY_DELETE_RECORDS: cd->data.recbuf = talloc(mem_ctx, struct ctdb_rec_buffer); assert(cd->data.recbuf != NULL); fill_ctdb_rec_buffer(mem_ctx, cd->data.recbuf); break; case CTDB_CONTROL_ENABLE_MONITOR: break; case CTDB_CONTROL_DISABLE_MONITOR: break; case CTDB_CONTROL_ADD_PUBLIC_IP: cd->data.addr_info = talloc(mem_ctx, struct ctdb_addr_info); assert(cd->data.addr_info != NULL); fill_ctdb_addr_info(mem_ctx, cd->data.addr_info); break; case CTDB_CONTROL_DEL_PUBLIC_IP: cd->data.addr_info = talloc(mem_ctx, struct ctdb_addr_info); assert(cd->data.addr_info != NULL); fill_ctdb_addr_info(mem_ctx, cd->data.addr_info); break; case CTDB_CONTROL_RUN_EVENTSCRIPTS: fill_ctdb_string(mem_ctx, &cd->data.event_str); assert(cd->data.event_str != NULL); break; case CTDB_CONTROL_GET_CAPABILITIES: break; case CTDB_CONTROL_START_PERSISTENT_UPDATE: break; case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE: break; case CTDB_CONTROL_RECD_PING: break; case CTDB_CONTROL_RELEASE_IP: cd->data.pubip = talloc(mem_ctx, struct ctdb_public_ip); assert(cd->data.pubip != NULL); fill_ctdb_public_ip(mem_ctx, cd->data.pubip); break; case CTDB_CONTROL_TAKEOVER_IP: cd->data.pubip = talloc(mem_ctx, struct ctdb_public_ip); assert(cd->data.pubip != NULL); fill_ctdb_public_ip(mem_ctx, cd->data.pubip); break; case CTDB_CONTROL_GET_PUBLIC_IPS: break; case CTDB_CONTROL_GET_NODEMAP: break; case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS: cd->data.event = rand_int(CTDB_EVENT_MAX); break; case CTDB_CONTROL_TRAVERSE_KILL: cd->data.traverse_start = talloc(mem_ctx, struct ctdb_traverse_start); assert(cd->data.traverse_start != NULL); fill_ctdb_traverse_start(mem_ctx, cd->data.traverse_start); break; case CTDB_CONTROL_RECD_RECLOCK_LATENCY: cd->data.reclock_latency = rand_double(); break; case CTDB_CONTROL_GET_RECLOCK_FILE: break; case CTDB_CONTROL_SET_RECLOCK_FILE: fill_ctdb_string(mem_ctx, &cd->data.reclock_file); assert(cd->data.reclock_file != NULL); break; case CTDB_CONTROL_STOP_NODE: break; case CTDB_CONTROL_CONTINUE_NODE: break; case CTDB_CONTROL_SET_LMASTERROLE: cd->data.role = rand_int(2); break; case CTDB_CONTROL_SET_RECMASTERROLE: cd->data.role = rand_int(2); break; case CTDB_CONTROL_ENABLE_SCRIPT: fill_ctdb_string(mem_ctx, &cd->data.script); assert(cd->data.script != NULL); break; case CTDB_CONTROL_DISABLE_SCRIPT: fill_ctdb_string(mem_ctx, &cd->data.script); assert(cd->data.script != NULL); break; case CTDB_CONTROL_SET_BAN_STATE: cd->data.ban_state = talloc(mem_ctx, struct ctdb_ban_state); assert(cd->data.ban_state != NULL); fill_ctdb_ban_state(mem_ctx, cd->data.ban_state); break; case CTDB_CONTROL_GET_BAN_STATE: break; case CTDB_CONTROL_SET_DB_PRIORITY: cd->data.db_prio = talloc(mem_ctx, struct ctdb_db_priority); assert(cd->data.db_prio != NULL); fill_ctdb_db_priority(mem_ctx, cd->data.db_prio); break; case CTDB_CONTROL_GET_DB_PRIORITY: cd->data.db_prio = talloc(mem_ctx, struct ctdb_db_priority); assert(cd->data.db_prio != NULL); fill_ctdb_db_priority(mem_ctx, cd->data.db_prio); break; case CTDB_CONTROL_TRANSACTION_CANCEL: break; case CTDB_CONTROL_REGISTER_NOTIFY: cd->data.notify = talloc(mem_ctx, struct ctdb_notify_data); assert(cd->data.notify != NULL); fill_ctdb_notify_data(mem_ctx, cd->data.notify); break; case CTDB_CONTROL_DEREGISTER_NOTIFY: cd->data.srvid = rand64(); break; case CTDB_CONTROL_TRANS3_COMMIT: cd->data.recbuf = talloc(mem_ctx, struct ctdb_rec_buffer); assert(cd->data.recbuf != NULL); fill_ctdb_rec_buffer(mem_ctx, cd->data.recbuf); break; case CTDB_CONTROL_GET_DB_SEQNUM: cd->data.db_id = rand32(); break; case CTDB_CONTROL_DB_SET_HEALTHY: cd->data.db_id = rand32(); break; case CTDB_CONTROL_DB_GET_HEALTH: cd->data.db_id = rand32(); break; case CTDB_CONTROL_GET_PUBLIC_IP_INFO: cd->data.addr = talloc(mem_ctx, ctdb_sock_addr); assert(cd->data.addr != NULL); fill_ctdb_sock_addr(mem_ctx, cd->data.addr); break; case CTDB_CONTROL_GET_IFACES: break; case CTDB_CONTROL_SET_IFACE_LINK_STATE: cd->data.iface = talloc(mem_ctx, struct ctdb_iface); assert(cd->data.iface != NULL); fill_ctdb_iface(mem_ctx, cd->data.iface); break; case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE: cd->data.conn = talloc(mem_ctx, struct ctdb_connection); assert(cd->data.conn != NULL); fill_ctdb_connection(mem_ctx, cd->data.conn); break; case CTDB_CONTROL_GET_STAT_HISTORY: break; case CTDB_CONTROL_SCHEDULE_FOR_DELETION: cd->data.key = talloc(mem_ctx, struct ctdb_key_data); assert(cd->data.key != NULL); fill_ctdb_key_data(mem_ctx, cd->data.key); break; case CTDB_CONTROL_SET_DB_READONLY: cd->data.db_id = rand32(); break; case CTDB_CONTROL_CHECK_SRVIDS: cd->data.u64_array = talloc(mem_ctx, struct ctdb_uint64_array); assert(cd->data.u64_array != NULL); fill_ctdb_uint64_array(mem_ctx, cd->data.u64_array); break; case CTDB_CONTROL_TRAVERSE_START_EXT: cd->data.traverse_start_ext = talloc(mem_ctx, struct ctdb_traverse_start_ext); assert(cd->data.traverse_start_ext != NULL); fill_ctdb_traverse_start_ext(mem_ctx, cd->data.traverse_start_ext); break; case CTDB_CONTROL_GET_DB_STATISTICS: cd->data.db_id = rand32(); break; case CTDB_CONTROL_SET_DB_STICKY: cd->data.db_id = rand32(); break; case CTDB_CONTROL_RELOAD_PUBLIC_IPS: break; case CTDB_CONTROL_TRAVERSE_ALL_EXT: cd->data.traverse_all_ext = talloc(mem_ctx, struct ctdb_traverse_all_ext); assert(cd->data.traverse_all_ext != NULL); fill_ctdb_traverse_all_ext(mem_ctx, cd->data.traverse_all_ext); break; case CTDB_CONTROL_RECEIVE_RECORDS: cd->data.recbuf = talloc(mem_ctx, struct ctdb_rec_buffer); assert(cd->data.recbuf != NULL); fill_ctdb_rec_buffer(mem_ctx, cd->data.recbuf); break; case CTDB_CONTROL_IPREALLOCATED: break; case CTDB_CONTROL_GET_RUNSTATE: break; case CTDB_CONTROL_DB_DETACH: cd->data.db_id = rand32(); break; case CTDB_CONTROL_GET_NODES_FILE: break; case CTDB_CONTROL_DB_FREEZE: cd->data.db_id = rand32(); break; case CTDB_CONTROL_DB_THAW: cd->data.db_id = rand32(); break; case CTDB_CONTROL_DB_TRANSACTION_START: cd->data.transdb = talloc(mem_ctx, struct ctdb_transdb); assert(cd->data.transdb != NULL); fill_ctdb_transdb(mem_ctx, cd->data.transdb); break; case CTDB_CONTROL_DB_TRANSACTION_COMMIT: cd->data.transdb = talloc(mem_ctx, struct ctdb_transdb); assert(cd->data.transdb != NULL); fill_ctdb_transdb(mem_ctx, cd->data.transdb); break; case CTDB_CONTROL_DB_TRANSACTION_CANCEL: cd->data.db_id = rand32(); break; case CTDB_CONTROL_DB_PULL: cd->data.pulldb_ext = talloc(mem_ctx, struct ctdb_pulldb_ext); assert(cd->data.pulldb_ext != NULL); fill_ctdb_pulldb_ext(mem_ctx, cd->data.pulldb_ext); break; case CTDB_CONTROL_DB_PUSH_START: cd->data.pulldb_ext = talloc(mem_ctx, struct ctdb_pulldb_ext); assert(cd->data.pulldb_ext != NULL); fill_ctdb_pulldb_ext(mem_ctx, cd->data.pulldb_ext); break; case CTDB_CONTROL_DB_PUSH_CONFIRM: cd->data.db_id = rand32(); break; } }
void generate_fractal(double cx, double cy, double rwidth, double rheight, Pixmap* out, int iters, long long points) { printf("generate\n"); const int width = out->width; const int height = out->height; const double pixelWidth = rwidth / width; const double pixelHeight = rheight / height; #pragma omp parallel { unsigned* priv_px = calloc(width * height, sizeof(unsigned)); double* trace = calloc(iters * 2, sizeof(double)); #pragma omp for for (long long i = 0; i < points; i++) { // 'C' in mandelbrot formula const double sx = rand_double(); const double sy = rand_double(); double x = 0.0; double y = 0.0; for (int j = 0; j < iters; j++) { // Mandelbrot formulas in R const double nx = x * x - y * y + sx; const double ny = 2 * x * y + sy; x = nx; y = ny; // Store point in trace trace[2 * j] = x; trace[2 * j + 1] = y; if (x * x + y * y > 6.0) { // Draw trace const int length = j * 2; for (int k = 0; k < length; k += 2) { const int qx = (int) (((trace[k] - cx) + rwidth * 0.5) / pixelWidth); const int qy = (int) (((trace[k + 1] - cy) + rheight * 0.5) / pixelHeight); if (qx >= 0 && qy >= 0 && qx < width && qy < height) { priv_px[width * qy + qx]++; } } break; } } } #pragma omp critical { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { px_set(out, x, y, px_get(out, x, y) + priv_px[width * y + x]); } } } free(trace); free(priv_px); } }
static value_t fl_randd(value_t *args, u_int32_t nargs) { (void)args; (void)nargs; return mk_double(rand_double()); }
/** * @brief Gets a random double in the range [low,high]. */ double rand_double_range( double low, double high ) { return (low + (high-low)*rand_double()); }