triple triple_rand() { return (position) { rand_val(-SPACE_SIZE,SPACE_SIZE), rand_val(-SPACE_SIZE,SPACE_SIZE), rand_val(-SPACE_SIZE,SPACE_SIZE) }; }
cl_float3 vec_rand() { cl_float3 rnd; rnd.s[0] = rand_val(-SPACE_SIZE, SPACE_SIZE); rnd.s[1] = rand_val(-SPACE_SIZE, SPACE_SIZE); rnd.s[2] = rand_val(-SPACE_SIZE, SPACE_SIZE); return rnd; }
int main() { t_infos s; int color; t_vect a; t_vect b; s.mlx_ptr = mlx_init(); s.win_ptr = mlx_new_window(s.mlx_ptr, 500, 500, "lolilol"); a.x = 0; a.y = 0; b.x = 250; b.y = 250; while (1) { color = rand_val(0, 0x00FFFFFF); while (a.x != 500) { color = rand_val(0, 0x00FFFFFF); draw_lines(s, a, b, color); usleep(TIME); a.x++; } while (a.y != 500) { color = rand_val(0, 0x00FFFFFF); draw_lines(s, a, b, color); a.y++; usleep(TIME); } while (a.x != 0) { color = rand_val(0, 0x00FFFFFF); draw_lines(s, a, b, color); a.x--; usleep(TIME); } while (a.y != 0) { color = rand_val(0, 0x00FFFFFF); draw_lines(s, a, b, color); a.y--; usleep(TIME); } } mlx_loop(s.win_ptr); }
void rand_init(rand_ctx *x, uint32_t seed) { uint32_t i; x->a = 0xf1ea5eed, x->b = x->c = x->d = seed; for (i = 0; i < 20; ++i) { (void)rand_val(x); } }
//=========================================================================== //= Function to generate bounded Pareto distributed RVs using = //= - Input: a, k, and p = //= - Output: Returns with bounded Pareto RV = //=========================================================================== double bpareto(double a, double k, double p) { double z; // Uniform random number from 0 to 1 double rv; // RV to be returned // Pull a uniform RV (0 < z < 1) do { z = rand_val(0); } while ((z == 0) || (z == 1)); printf("z: %f", z); // Generate the bounded Pareto rv using the inversion method rv = pow((pow(k, a) / (z*pow((k/p), a) - z + 1)), (1.0/a)); return(rv); }
//=========================================================================== //= Function to generate Zipf (power law) distributed random variables = //= - Input: alpha and N = //= - Output: Returns with Zipf distributed random variable = //=========================================================================== int zipf(double alpha, int n) { static int first = TRUE; // Static first time flag static double c = 0; // Normalization constant double z; // Uniform random number (0 < z < 1) double sum_prob; // Sum of probabilities double zipf_value; // Computed exponential value to be returned int i; // Loop counter // Compute normalization constant on first call only if (first == TRUE) { for (i=1; i<=n; i++) c = c + (1.0 / pow((double) i, alpha)); c = 1.0 / c; first = FALSE; } // Pull a uniform random number (0 < z < 1) do { z = rand_val(0); } while ((z == 0) || (z == 1)); // Map z to the value sum_prob = 0; for (i=1; i<=n; i++) { sum_prob = sum_prob + c / pow((double) i, alpha); if (sum_prob >= z) { zipf_value = i; break; } } // Assert that zipf_value is between 1 and N assert((zipf_value >=1) && (zipf_value <= n)); return(zipf_value); }
int main(int argc, char **argv) { printf("N=%d, CL_DEVICE=%d\n", N, CL_DEVICE); steps = 0; totalTime = 0; // distribute bodies in space (randomly) for(int i=0; i<N; i++) { B[i].m = rand_val(8,20); // (8,20) B[i].pos = vec_rand(); B[i].v = vec_zero(); } // ocl initialization init_ocl(); // ogl stuff init_gl(&argc, argv); glutDisplayFunc(&display); atexit(&exit_cb); glutMainLoop(); return EXIT_SUCCESS; }
//===== Main program ======================================================== void main(void) { char in_string[256]; // Input string FILE *fp; // File pointer to output file double a; // Pareto alpha value double k; // Pareto k value double p; // Pareto p value double pareto_rv; // Pareto random variable int num_values; // Number of values int i; // Loop counter //Output banner printf("---------------------------------------- genpar2.c ----- \n"); printf("- Program to generate bounded Pareto random variables - \n"); printf("- with lower bound value of k and upper bound of p - \n"); printf("-------------------------------------------------------- \n"); // Prompt for output filename and then create/open the file printf("Enter output file name =========================> "); scanf("%s", in_string); fp = fopen(in_string, "w"); if (fp == NULL) { printf("ERROR in creating output file (%s) \n", in_string); exit(1); } // Prompt for random number seed and then use it printf("Random number seed (greater than zero) =========> "); scanf("%s", in_string); rand_val((int) atoi(in_string)); // Prompt for Pareto alpha value printf("Pareto alpha value =============================> "); scanf("%s", in_string); a = atof(in_string); // Prompt for Pareto k value printf("Pareto k value =================================> "); scanf("%s", in_string); k = atof(in_string); // Prompt for Pareto k value printf("Pareto p value =================================> "); scanf("%s", in_string); p = atof(in_string); // Prompt for number of values to generate printf("Number of values to generate ===================> "); scanf("%s", in_string); num_values = atoi(in_string); //Output message and generate samples printf("-------------------------------------------------------- \n"); printf("- Generating samples to file \n"); printf("- * alpha = %f \n", a); printf("- * k = %f \n", k); printf("- * p = %f \n", p); printf("-------------------------------------------------------- \n"); for (i=0; i<num_values; i++) { pareto_rv = bpareto(a, k, p); fprintf(fp, "%f \n", pareto_rv); } //Output message and close the outout file printf("-------------------------------------------------------- \n"); printf("- Done! \n"); printf("-------------------------------------------------------- \n"); fclose(fp); }
//===== Main program ======================================================== void main(void) { FILE *fp; // File pointer to output file char file_name[256]; // Output file name string char temp_string[256]; // Temporary string variable double alpha; // Alpha parameter double n; // N parameter int num_values; // Number of values int zipf_rv; // Zipf random variable int i; // Loop counter // Output banner printf("---------------------------------------- genzipf.c ----- \n"); printf("- Program to generate Zipf random variables - \n"); printf("-------------------------------------------------------- \n"); // Prompt for output filename and then create/open the file printf("Output file name ===================================> "); scanf("%s", file_name); fp = fopen(file_name, "w"); if (fp == NULL) { printf("ERROR in creating output file (%s) \n", file_name); exit(1); } // Prompt for random number seed and then use it printf("Random number seed (greater than 0) ================> "); scanf("%s", temp_string); rand_val((int) atoi(temp_string)); // Prompt for alpha value printf("Alpha value ========================================> "); scanf("%s", temp_string); alpha = atof(temp_string); // Prompt for N value printf("N value ============================================> "); scanf("%s", temp_string); n = atoi(temp_string); // Prompt for number of values to generate printf("Number of values to generate =======================> "); scanf("%s", temp_string); num_values = atoi(temp_string); // Output "generating" message printf("-------------------------------------------------------- \n"); printf("- Generating samples to file - \n"); printf("-------------------------------------------------------- \n"); // Generate and output zipf random variables for (i=0; i<num_values; i++) { zipf_rv = zipf(alpha, n); fprintf(fp, "%d \n", zipf_rv); } // Output "done" message and close the output file printf("-------------------------------------------------------- \n"); printf("- Done! \n"); printf("-------------------------------------------------------- \n"); fclose(fp); }