int main(int argc,char *argv[]) { double x,y,pi; int n_trials,i_trial,n_hits; if (argc != 3) { fprintf(stderr,"%s <N trials> <seed>\n",argv[0]); exit(1); } n_trials = atoi(argv[1]); random_seed(atoi(argv[2])); i_trial = 0; n_hits = 0; while (i_trial < n_trials) { /* Add statements here to generate two random coordinates within the square in the double precision variables x and y, and increment the integer variable n_hits if x and y represent a point within the unit circle Hint: save computation time by NOT calling the sqrt() function! */ x = 2.0*(random_gen() - 0.5); y = 2.0*(random_gen() - 0.5); if (x*x + y*y <= 1) n_hits++; i_trial++; } pi = 4.0 * ((double) n_hits) / ((double) n_trials); printf("%.8f\n",pi); exit(0); }
float random_float() { #ifdef WIN32 return ((float)random_gen() / (float)UINT_MAX); #else return ((float)random_gen() / (float)RAND_MAX); #endif }
int main() { std::string temp; while(std::cin >> temp) std::cout << std::hex << random_gen(19, 1, 10) << std::endl; return 0; }
static uint8_t random_readByte(void) { #ifdef CRYPTO_RANDOM_ENABLED if (0 == prng_buf_len) // if no prng bytes available, go generate some more random_gen(); #endif return prng_buf[--prng_buf_len]; // consume one prng byte }
/** * Return coordinates and speed of orbiting body. * Returned value can change between two calls. */ std::tuple<double, double, double, double> orbit::OrbitalTrajectory::position() const { double pos_x = 0., pos_y = 0., speed_x = 0., speed_y = 0.; if(this->is_orbit()) { // Random value generator std::random_device random_rd; // only used once to initialise (seed) engine std::mt19937 random_gen(random_rd()); // random-number engine used (Mersenne-Twister in this case) // Compute a random position pos_x = std::uniform_real_distribution<double>(-semimajoraxis, semimajoraxis)(random_gen); // equation of an ellipse: y = sqrt(b*b - b*b * x*x / (a*a)) pos_y = sqrt(semiminoraxis*semiminoraxis - semiminoraxis*semiminoraxis * pos_x*pos_x / (semimajoraxis * semimajoraxis)); // Compute the speed associated with position, in this particular case double real_distance = sqrt(pos_x*pos_x + pos_y*pos_y); double real_speed = orbit::orbital_speed(mass, parent_mass, real_distance, semimajoraxis); // Now, determine the speed on x axis and on y axis. // Equation of the tangent to an ellipse: // 1 = x*α / (a*a) + y*(b/a)β / (b*b) // With (α;(b/a)β) the vertex on the ellipse, a the semi-major axis, // b the semi-minor axis and (x;y) the vertex on the tangent. // Here is the tangent equation: // y = -(b*α / (β*a*a))*x + b/β // The slope is then -(b*α / (β*a*a)). // The slope equals to tan(þ), with þ the angle given by the line and the abscissa. // By construction, speed is the hypothenuse of the triangle formed by // - the position of astre (x, y), // - the vertex at a distance 'speed' (x', y') on the ellipse tangent, // - the vertex at (x', y) or (x, y'). // Then, the side [x', x] gives the speed on x axis, // and the side [y, y'] gives the speed on y axis. // These two values are computable as sin(þ) = [y, y'] / hypothenuse // and cos(þ) = [x, x'] / hypothenuse. // Here are the final equations: // speed_x = speed * cos(atan(slope)) // speed_y = speed * sin(atan(slope)) // Nota bene: cos(atan(x)) <=> 1 / sqrt(x*x + 1) // sin(atan(x)) <=> x / sqrt(x*x + 1) double slope(tangent_slope(semiminoraxis, semimajoraxis, pos_x, pos_y)); speed_x = 1 * real_speed / sqrt(slope * slope + 1); speed_y = slope * real_speed / sqrt(slope * slope + 1); // Above or below the parent ? (considering y axis) if(std::bernoulli_distribution()(random_gen)) { // without position swap, astre could'nt be placed above the parent pos_y *= -1; // without speed swap, astre will orbit clockwise if spawned below the parent speed_x *= -1; } } else if(this->is_precise_placement()) { pos_x = this->positionX; pos_y = this->positionY; speed_x = this->speedX; speed_y = this->speedY; } return std::make_tuple(pos_x, pos_y, speed_x, speed_y); }
int create_relation_nonunique(relation_t *relation, int64_t num_tuples, const int64_t maxid) { check_seed(); relation->num_tuples = num_tuples; if (!relation->tuples) { perror("memory must be allocated first"); return -1; } random_gen(relation, maxid); return 0; }
int main(int argc, char *argv[]) { int *array = random_gen(ARRAY_SIZE); printf("\n# ----- Original Data ----- #\n\n"); print_array(array, ARRAY_SIZE); insertion_int(array, ARRAY_SIZE); printf("\n# ----- Sorted Data ----- #\n\n"); print_array(array, ARRAY_SIZE); printf("\n"); free_array(array); return 0; }
int main(int argc,char *argv[]) { int i, j, m; double x, per; int n = 5000; int iprt = 1000; m = 0; if (argc != 2) { fprintf(stderr,"%s <seed>\n",argv[0]); exit(1); } fprintf(stderr,"i: per:\n"); random_seed(atoi(argv[1])); for (i = 1; i <= n; i++) { x = 1.0; for (j = 1; j <= 7; j++) { x = x + cos(PI * random_gen()); if (x <= 0.0) { break; } if (x >= 5.0) { m = m + 1; break; } } if (i % iprt == 0) { per = (100.0 * ((double)m / (double)i)); printf("%d %lf\n", i, per); } } exit(0); }
float rand_scale(float s) { float scale = rand_uniform_strong(1, s); if(random_gen()%2) return scale; return 1./scale; }
void CSeqVector_CI::SetRandomizeAmbiguities(Uint4 seed) { CRandom random_gen(seed); x_InitRandomizer(random_gen); }
double exponential(randgen rg, double lambda){ float rand; rand = random_gen(rg); return -(1/lambda)*log(rand); }
unsigned int random_irange(unsigned int min, unsigned int max) { unsigned interval = max - min; unsigned i = (interval + 1.0f) * random_gen(); return min + (i < interval ? i : interval); }
float random_range(float min, float max) { float interval = max - min; float d = interval * random_gen(); return min + (d < interval ? d : interval); }