void choose_direction_for_ghost(char * map, struct pacghost * pacman, struct pacghost * ghost, int difficulty, int is_pacman_powered_up){ if (!is_pacman_powered_up){ if (difficulty == 0){ move_randomly(ghost); } else if (difficulty == 1){ int random = random_in_range(0,10); if (random < 5) move_randomly(ghost); else chase_after_pacman(pacman, ghost); } else if (difficulty == 2){ int random = random_in_range(0,10); if (random < 2) move_randomly(ghost); else chase_after_pacman(pacman, ghost); } else { chase_after_pacman(pacman, ghost); } /* The ghost move to the point nearer to him than pacman when pacman powered */ } else { /* As the point search in order. The ghost tend to stuck when came to top left */ for (int rows = 0; rows < height; rows++) { for (int cols = 0; cols < width; cols++){ if (isValidMoveCell(rows, cols)){ if (distance_between_two_points(pacman->xLocation,pacman->yLocation,rows,cols) > distance_between_two_points(ghost->xLocation,ghost->yLocation,rows,cols)){ ghost->direction = direction_from_node_to_node(ghost->xLocation,ghost->yLocation,rows,cols); return; } } } } } }
void do_sierpinski(short unsigned* shm, short unsigned* local,int* n_gen) { int x,y; int des,sin,su; inuso=P(sem_id,0); if( (* n_gen) == 0 ) { // inizializza la matrice con una sola cella viva for ( y=0; y < N_Y * N_X; ++y ) local[y]=0; fiat( random_in_range(0,N_X), random_in_range(0,N_Y), local); ++(* n_gen); } else { for( y=0; y < N_Y * N_X; ++y ) local[y]=shm[y]; } for( y=0; y < N_Y; ++y ) { for( x=0; x < N_X; ++x ) { // rendiamo l'universo un toro \m/ if(x==0) sin=N_X-1; else sin=x-1; if(x==N_X-1) des=0; else des=x+1; if(y==0) su=N_Y-1; else su=y-1; switch( get_stato(x,y,local) ) { case MORTO: if // XOR ( (get_stato(sin,su,local)==VIVO) ^ (get_stato(des,su,local)==VIVO) ) fiat(x,y,shm); else uccidi(x,y,shm); break; case VIVO: if // condizione da rivedere ( (get_stato(sin,su,local)==VIVO) && (get_stato(des,su,local)==VIVO) ) uccidi(x,y,shm); else fiat(x,y,shm); break; defaut: ; break; } } } ++(*n_gen); inuso=V(sem_id,0); }
// // Calculate the random influent each round // void RandomInfluent(void) { Influent.Flow = random_in_range(9, 30) / 10.0; Influent.Cbod = random_in_range(1900, 4000); Influent.Tss = random_in_range(2000, 5000); Influent.Tn = random_in_range(490, 750); Influent.Tp = random_in_range(75, 150); }
char *gen_random_str(int min, int max) { int length = random_in_range(min, max); char *s = calloc(length); for(int i=0; i<length - 2; i++) { s[i] = random_in_range(0x30, 0x7e); } return s; }
Vec3d Particle_source_box::uniform_position_in_cube( const double xleft, const double ytop, const double zfar, const double xright, const double ybottom, const double znear, std::default_random_engine &rnd_gen ) { return vec3d_init( random_in_range( xright, xleft, rnd_gen ), random_in_range( ybottom, ytop, rnd_gen ), random_in_range( znear, zfar, rnd_gen ) ); }
Vec3d Particle_source_cylinder::uniform_position_in_cylinder( std::default_random_engine &rnd_gen ) { // random point in cylinder along z Vec3d cyl_axis = vec3d_init( ( axis_end_x - axis_start_x ), ( axis_end_y - axis_start_y ), ( axis_end_z - axis_start_z ) ); double cyl_axis_length = vec3d_length( cyl_axis ); double x, y, z; double r, phi; r = sqrt( random_in_range( 0.0, 1.0, rnd_gen ) ) * radius; phi = random_in_range( 0.0, 2.0 * M_PI, rnd_gen ); z = random_in_range( 0.0, cyl_axis_length, rnd_gen ); // x = r * cos( phi ); y = r * sin( phi ); z = z; Vec3d random_pnt_in_cyl_along_z = vec3d_init( x, y, z ); // rotate: // see https://en.wikipedia.org/wiki/Rodrigues'_rotation_formula // todo: Too complicated. Try rejection sampling. Vec3d random_pnt_in_rotated_cyl; Vec3d unit_cyl_axis = vec3d_normalized( cyl_axis ); Vec3d unit_along_z = vec3d_init( 0, 0, 1.0 ); Vec3d rotation_axis = vec3d_cross_product( unit_along_z, unit_cyl_axis ); double rotation_axis_length = vec3d_length( rotation_axis ); if ( rotation_axis_length == 0 ) { if ( copysign( 1.0, vec3d_z( unit_cyl_axis ) ) >= 0 ){ random_pnt_in_rotated_cyl = random_pnt_in_cyl_along_z; } else { random_pnt_in_rotated_cyl = vec3d_negate( random_pnt_in_cyl_along_z ); } } else { Vec3d unit_rotation_axis = vec3d_normalized( rotation_axis ); double rot_cos = vec3d_dot_product( unit_cyl_axis, unit_along_z ); double rot_sin = rotation_axis_length; random_pnt_in_rotated_cyl = vec3d_add( vec3d_times_scalar( random_pnt_in_cyl_along_z, rot_cos ), vec3d_add( vec3d_times_scalar( vec3d_cross_product( unit_rotation_axis, random_pnt_in_cyl_along_z ), rot_sin ), vec3d_times_scalar( unit_rotation_axis, ( 1 - rot_cos ) * vec3d_dot_product( unit_rotation_axis, random_pnt_in_cyl_along_z ) ) ) ); } // shift: Vec3d shifted = vec3d_add( random_pnt_in_rotated_cyl, vec3d_init( axis_start_x, axis_start_y, axis_start_z ) ); return shifted; }
static void allocate_directories(directory_t *dir, unsigned allow_zero) { unsigned n_directories = random_in_range(arg_subdirs); while (!allow_zero && n_directories == 0) { n_directories = random_in_range(arg_subdirs); } dir->n_directories = n_directories; dir->directories = allocate_directory(n_directories); }
//This is a super-uniform function, much better than (rand() % max) +min int random_in_range (unsigned int min, unsigned int max) { int base_random = rand(); /* in [0, RAND_MAX] */ if (RAND_MAX == base_random) return random_in_range(min, max); /* now guaranteed to be in [0, RAND_MAX) */ unsigned int range = max - min, remainder= RAND_MAX % range, // These are range buckets, plus one smaller interval bucket = RAND_MAX / range; // within remainder of RAND_MAX */ if (base_random < RAND_MAX - remainder) return min + base_random/bucket; else return random_in_range (min, max); }
void InitFilesystem(void) { char root_passwd[32]; char secure_passwd[32]; uint8_t i; const char *rand_page = (const char *)0x4347C000; bzero(FS, sizeof(FS)); bzero(FH, sizeof(FH)); // generate a random root password bzero(root_passwd, 32); for (i = 0; i < 10; i++) { root_passwd[i] = (char)random_in_range(65,122); } root_passwd[i] = '\0'; // generate a random secure user password bzero(secure_passwd, 32); for (i = 0; i < 31; i++) { secure_passwd[i] = (char)random_in_range(65,122); } secure_passwd[i] = '\0'; // passwd file strcpy(FS[0].Filename, "passwd"); strcpy(FS[0].Owner, "root"); strcpy(FS[0].Group, "root"); FS[0].Perms = 0x700; sprintf(FS[0].Data, "$s:secure:secure\ncrs:crs:crs\n$s:root:root", secure_passwd, root_passwd); FS[0].Size = cgc_strlen(FS[0].Data); // confidential data file strcpy(FS[1].Filename, "confidential"); strcpy(FS[1].Owner, "secure"); strcpy(FS[1].Group, "secure"); FS[1].Perms = 0x700; // cgc_read starting at 4th byte since we already used the first 4 to seed the prng // but don't use any 4-byte chunks that have NULL's or newlines rand_page+=4; i = 0; while (i < 4) { if (*rand_page == '\0' || *rand_page == '\n') { i = 0; rand_page++; continue; } FS[1].Data[i++] = *rand_page; rand_page++; } FS[1].Size = 4; }
void swarm_to_digit(int digit, int start_idx, int end_idx, int offset_x, int offset_y) { int end = minimum(end_idx, NUM_PARTICLES); for(int i=start_idx; i<end; i++) { GBitmap bitmap = *number_bitmaps[digit]; const uint8_t *pixels = bitmap.addr; // pick a random point in the four image // get the image // pick a random number the length of the array // if the value is zero, pick a differnet number increment until you get there // move to that position int image_length = bitmap.row_size_bytes * bitmap.bounds.size.h - 1; int idx = random_in_range(0, image_length); uint8_t pixel = pixels[idx]; // guarantee we're on a non-zero byte while(pixel == 0x00) { idx = random_in_range(0, image_length); pixel = pixels[idx]; } // pick a non-zero bit int bit_pos = (idx * 8); int bit_add = random_in_range(0,7); int tries = 8; while(!(pixel & (1 << bit_add))) { if(tries < 0) break; bit_add = random_in_range(0,7); tries--; } bit_pos += bit_add; int row_size_bits = bitmap.row_size_bytes * 8; int pixel_row = bit_pos / row_size_bits; int pixel_col = bit_pos % row_size_bits; float scale = 1.0F; GPoint goal = GPoint(scale*pixel_col+offset_x, scale*pixel_row+offset_y); // switch row & col particles[i].grav_center = FPoint(goal.x, goal.y); particles[i].power = TIGHT_POWER; particles[i].goal_size = random_in_rangef(2.0F, 3.5F); particles[i].swarming = true; } }
int random_in_range (int min, int max){ int base_random = rand(); /* in [0, RAND_MAX] */ if (RAND_MAX == base_random) return random_in_range(min, max); /* now guaranteed to be in [0, RAND_MAX) */ int range = max - min, remainder = RAND_MAX % range, bucket = RAND_MAX / range; /* There are range buckets, plus one smaller interval within remainder of RAND_MAX */ if (base_random < RAND_MAX - remainder) { return min + base_random/bucket; } else { return random_in_range (min, max); } }
vector<double> PageRank::metodoPotencia() { int n = A.size(); // creo el x aleatorio vector<double> x(n, 0); for (int i = 0; i < n; i++) { x[i] = random_in_range(1,50); } // traspongo x para poder multiplicarlo por A vector< vector<double> > aux = row2Column(x); vector< vector<double> > B = multiply(A, A); vector< vector<double> > C = A; double delta = phi(column2Row(multiply(B, aux))) / phi(column2Row(multiply(C, aux))); double last_delta = INFINITY; while (fabs(delta - last_delta) > precision) { C = B; B = multiply(B, A); last_delta = delta; delta = phi(column2Row(multiply(B, aux))) / phi(column2Row(multiply(C, aux))); } vector<double> v = column2Row(multiply(B, aux)); return scaleVector(v, 1/norma1(v)); }
int main() { int x, i, arr[100]; while ((x<=0) || (x>=10)) { pritnf("Vavedete x: "); scanf("%d", &x); } for(i=0; i<100; i++) { arr[i]=random_in_range (0,100); printf("%d ", arr[i]); } printf("\n End of Random Numbers \n"); int z, n, f, m=0; for(n=0; n<10; n++) { for(i=0; i<100; i++) { z=x+10*n; if (arr[i] == z) { f=arr[m]; arr[m]=arr[i]; arr[i]=f; m=m+1; } } } for(i=0; i<100; i++) { printf("%d ", arr[i]); } printf("\n"); }
static void allocate_sources(directory_t *dir) { const unsigned n_sources = random_in_range(arg_sources); dir->n_sources = n_sources; dir->sources = allocate_source(n_sources); }
static unsigned int random_in_range(unsigned int min, unsigned int max) { int base_random = rand(); if (RAND_MAX == base_random) { return random_in_range(min, max); } unsigned int range = max - min; unsigned int remainder = RAND_MAX % range; unsigned int bucket = RAND_MAX / range; if ((unsigned int) base_random < RAND_MAX - remainder) { return min + ((unsigned int) base_random / bucket); } return random_in_range(min, max); }
int random_in_range(int min, int max) { int base_random = rand(); if(RAND_MAX == base_random) return random_in_range(min, max); int range = max - min; int remainder = RAND_MAX % range; int bucket = RAND_MAX / range; if(base_random < RAND_MAX - remainder) { return min + base_random/bucket; } else { return random_in_range(min, max); } }
// Shuffle the deck using the Knuth shuffle agorithm with array initialization void shuffle(standard_deck *deck) { for (int i=0; i<SIZE_OF_DECK; i++) { int j = random_in_range(0, i); deck->deck[i] = deck->deck[j]; // Initialize array with the numbers 1 to 52 deck->deck[j] = (char)(i + 1); } deck->top = 0; }
static Location enemy_random_location (void) { Location location; location.x = get_random_location ().x; location.y = random_in_range (25, 100); return location; }
void test_random_port_in_range() { jnx_term_printf_in_color(JNX_COL_WHITE, "\ttest_random_port_in_range(): "); int i, count = 10, min = 10000, max = 20000, current; for (i = 0; i < count; i++) { current = random_in_range(min, max); assert(min <= current && current <= max); } jnx_term_printf_in_color(JNX_COL_GREEN, "Pass\n"); }
void calculate_grid_backtrack(void) { int next_cell; for (int m = 0; m < 9; m++) { for (int n = 0; n < 9; n++) { next_cell = (int)random_in_range(9); insert_cell(m, n, next_cell); } } }
static Acceleration enemy_random_acceleration () { Acceleration acceleration = {1.5, 0.5}; //50% of enemy ships will go accelerate to the other side(heading towards up) if (random_in_range (0, 2)) acceleration.y *= -1; return acceleration; }
user *gen_random_user() { user *new_user = calloc(sizeof(user)); new_user->name = gen_random_str(5, 32); char *hn = gen_random_str(5, 32); strcpy(new_user->hostname, hn); free(hn); new_user->idletime = rand() & 0xffffff; new_user->realname = gen_random_str(5, 64); new_user->phone = gen_random_str(9,9); new_user->online = random_in_range(0,1); return new_user; }
uint32_t AddConnection(CGCRPC_Endpoint *endpoint) { for (int i=0; i < MAX_CGCRPC_CONNECTIONS; i++) { if (connections[i].bindID == 0) { connections[i].bindID = random_in_range(1, 0xffffffff); connections[i].endpoint = endpoint; return connections[i].bindID; } } return 0; }
static void add_texture(int x, int y, gal_uint8 color) { Texture *t; t = (Texture *) malloc(sizeof(Texture)); t->mid_x = x << 16; t->mid_y = y << 16; t->millis = 0; t->color = color; t->speed = speed + (fixed_speed ? 0 : random_in_range(-(speed/2), +(speed/2))); t->succ = texture_list; texture_list = t; }
address *pick_address() { int i = 0; address *next = abook->root; while (next != NULL) { i++; next = next->next; } int pick = random_in_range(0, i-1); next = abook->root; while (next->next != NULL && pick > 0) { next = next->next; pick--; } return next; }
void init_enemies (void) { int i; ALLEGRO_BITMAP *shared_enemy_ship_image = al_load_bitmap ("assets/png/enemyShip.png"); for (i = 0; i < NUM_ENEMIES; i++) { enemies[i].health_pts = ENEMY_HP; enemies[i].location = enemy_random_location (); enemies[i].live = 0; enemies[i].follow_player = random_in_range (0, 2); //50% will follow the player enemies[i].acceleration = enemy_random_acceleration (); enemies[i].image = shared_enemy_ship_image; enemies[i].puller = NULL; } }
address *add_random_addressbook_entry() { address *a = calloc(sizeof(address)); a->name = gen_random_string(5, 32); a->home_server = gen_random_string(5, 32); a->mode = random_in_range(0,1); address *n = abook->root; if (n == NULL) { // First Entry abook->root = a; } else { while (n->next != NULL) { n = n->next; } n->next = a; } abook->num_entries++; return a; }
int sort_one(int a[], int length, int left, int right) { if (a == NULL || length <= 0 || left < 0 || right >= length) { return -1; /* error */ } int index = random_in_range(left, right); swap(&a[index], &a[right]); int small = left - 1; for (index = left; index < right; index++) { if (a[index] < a[right]) { small++; if (small != index) swap(&a[small], &a[index]); } } small++; swap(&a[small], &a[right]); return small; }
void move_randomly(struct pacghost * ghost) { int availableWay = ghost_map[ghost->xLocation * width + ghost->yLocation] - '0'; int count = 0; if (availableWay >= 3 || !(canMove(ghost))) { int random = random_in_range(0,availableWay); if (isValidMoveCell(ghost->xLocation-1,ghost->yLocation)){ if (count != random){ count++; } else { ghost->direction = 0; count++; } } if (isValidMoveCell(ghost->xLocation,ghost->yLocation+1)){ if (count != random){ count++; } else { ghost->direction = 1; count++; } } if (isValidMoveCell(ghost->xLocation+1,ghost->yLocation)){ if (count != random){ count++; } else { ghost->direction = 2; count++; } } if (isValidMoveCell(ghost->xLocation,ghost->yLocation-1)){ if (count != random) count++; else ghost->direction = 3; } } }
vector<double> PageRankEsparso::metodoPotencia() { int n = A.size(); // creo el x aleatorio vector<double> x(n, 0); for (int i = 0; i < n; i++) { x[i] = random_in_range(1,50); } // creo el v aleatorio vector<double> v(n, double(1)/double(n)); vector<double> y = x; double delta = INFINITY; double last_delta; do { x = y; y = scaleVector(multiplyEsparso(A, x), teletransportacion); double w = norma1(x) - norma1(y); y = sumVector(y, scaleVector(v, w)); last_delta = delta; delta = phi(y) / phi(x); } while (fabs(delta - last_delta) > precision); return scaleVector(y, 1/norma1(y)); }