void generatePlayer(){ if(!strcmp(Player->name,"Lu Bu\n")){ Player->atk = 999; Player->def = 999; Player->spd = 1; Player->hp = 999; Player->maxHp = Player->hp; Player->xp = 0; Player->level = 10; Player->xpOffset = 999; Player->weaponNum = 0; Player->slot1 = *(Weapon *)calloc(1,sizeof(Weapon)); Player->slot2 = *(Weapon *)calloc(1,sizeof(Weapon)); } else{ Player->atk = rand_lim(12) + 6; Player->def = rand_lim(12) + 6; Player->spd = rand_lim(12) + 6; Player->hp = rand_lim(12) + 6; Player->maxHp = Player->hp; Player->xp = 0; Player->level = 1; Player->xpOffset = 10; Player->weaponNum = 0; Player->slot1 = *(Weapon *)calloc(1,sizeof(Weapon)); Player->slot2 = *(Weapon *)calloc(1,sizeof(Weapon)); } }
// Randomly mutates one of the bases in a gene void gene_point_mutate(base **gene) { int index = rand_lim(EQN_GENE_LENGTH - 1); char *bases = "SIR1!0gb+-*"; if(index < EQN_GENE_HEAD_LENGTH) { int new_index = rand_lim(NUM_BASES - 1); // Head bases can be mutated to any other base. There is no restriction. gene[index]->value = bases[new_index]; gene[index]->terminal = true; if(bases[new_index] == '+' || bases[new_index] == '-' || bases[new_index] == '*') { gene[index]->terminal = false; } } else { int new_index = rand_lim(NUM_TERMINALS - 1); // Tail bases can only be mutated from a terminator to another terminator. gene[index]->value = bases[new_index]; } }
/** * @brief Only use internally. */ void add_one(int *values) { call_srand(); int cntr = 0; int i; for (i = 0; i < 16; ++i) { if (values[i] == 0) { cntr++; break; } } if (cntr == 0) return; int p_1 = rand_lim(15); while (values[p_1] > 0) { p_1 = rand_lim(15); } int p_1_v = rand_lim(3); if (p_1_v < 3) p_1_v = 2; else p_1_v = 4; values[p_1] = p_1_v; }
void RAND(char* b1, char* b1_2, char* b2, char* b2_2, char* tm, char* tm2, data_t* data){ clock_t start, fin, t_total = 0; double transfTime, emptyTime; double mult = 100; start = clock(); for( long j =0; j < SHRT_MAX; j++) { for( int i = 0; i < NUM_IT; i++ ){} } fin = clock() - start; emptyTime = fin / CLOCKS_PER_SEC; for( long j = 0; j < SHRT_MAX; j++) { for( int i = 0; i < NUM_IT; i++ ) { start = clock(); memcpy(tm, b1, data->blockSize); memcpy(b1, b2, data->blockSize); memcpy(b2,tm, data->blockSize); fin = clock() - start ; t_total += fin; b1 = b1_2; b2 = b2_2; tm = tm2; b1 += data->blockSize * rand_lim(mult - 1); b2 += data->blockSize * rand_lim(mult - 1); tm += data->blockSize * rand_lim(mult - 1); } } long totalBytes = (long)data->blockSize * (long)SHRT_MAX * mult * 6; transfTime = (t_total/ CLOCKS_PER_SEC) - emptyTime ; std::cout << "\nRandom access throughput(MB/sec): " << totalBytes / (transfTime * 1024 * 1024) << std::endl; start = clock(); for( long j = 0; j < SHRT_MAX; j++) { for ( int i = 0; i < NUM_IT; i++) { memset(b1, '-', data->blockSize); b1 = b1_2; b1 += rand_lim(mult - 1) * data->blockSize; } } fin = clock() - start; double divis = CLOCKS_PER_SEC * NUM_IT * SHRT_MAX; double latency = fin / divis; std::cout << "\nRandom access latency(ms): " << latency << std::endl; }
//Randomly swaps a row of boxes. void sudoku_flip_rowbox(Sudoku sudoku) { int i = 0, div_size = sqrt(sudoku->size); for (i=0; i < div_size; i++) { while (80 > rand_lim(100)) { int brow1 = rand_lim(div_size-1); int brow2 = rand_lim(div_size-1); sudoku_swap_brow(sudoku, brow1, brow2); } } }
//Randomly swaps a column of boxes //ZZZThis could easily be combined into one function with a toggle parameter. void sudoku_flip_colbox(Sudoku sudoku) { int i = 0, div_size = sqrt(sudoku->size); for (i=0; i < div_size; i++) { while (80 > rand_lim(100)) { int bcol1 = rand_lim(div_size-1); int bcol2 = rand_lim(div_size-1); sudoku_swap_bcol(sudoku, bcol1, bcol2); } } }
void generateEnemy(){ int room = DRoom -> room; DRoom -> Enemy.atk = rand_lim(10 + room) + Player->level; DRoom -> Enemy.def = rand_lim(10 + room) + Player->level; DRoom -> Enemy.spd = rand_lim(10 + room) + Player->level; DRoom -> Enemy.hp = rand_lim(10 + room) + Player->level; int temp = rand_lim(nameSize); strcpy(DRoom -> Enemy.name,enemyNames[temp]); Enemy = &(DRoom -> Enemy); //int temp = rand_lime(2); //strcpy(EnemyList[temp],Enemy->name); }
void Mesh::random_complexify() { int random_group_index = rand_lim(groups.size() - 1); int random_face_index = rand_lim(groups[random_group_index]->getFaces().size() - 1); face_selected.group_pos = random_group_index; face_selected.face_pos = random_face_index; face_selected.face = groups[random_group_index]->getFaceAt(random_face_index); selection = SELECTION_FACE; complexify(); }
void Goblin_update(Entity *self) { if (self->ticks_idle > 30 / 4) { if (self->x == self->destination.x && self->y == self->destination.y) { self->destination.x = rand_lim(77) + 1; self->destination.y = rand_lim(21) + 1; } self->moveToDestination(self); self->ticks_idle = 0; } else { self->ticks_idle++; } }
int main(void) { FILE *outFile; srand(time(NULL)); int i, k, nums; for (i = 0; i < 3; i++) { if (i == 0) { outFile = fopen("list0.txt", "w"); nums = 1000; } else if (i == 1) { outFile = fopen("list1.txt", "w"); nums = 1000; } else { outFile = fopen("list2.txt", "w"); nums = 1000; } for (k = 0; k < nums; k++) { fprintf(outFile, "%d\n", rand_lim(1,1000)); } fclose(outFile); } return 0; }
//Flips a compeleted sudoku a random amounts of times around different axes //http://dryicons.com/blog/2009/08/14/a-simple-algorithm-for-generating-sudoku-puzzles/ void sudoku_transform(Sudoku sudoku) { //srand(time(NULL)); int i = 0; while (i < 5000) { //Magic number, I know... i++; int val = rand_lim(150); if (0 <= val && val < 25) { sudoku_flip_vert(sudoku); } else if (25 <= val && val < 50) { sudoku_flip_hori(sudoku); } else if (50 <= val && val < 75) { sudoku_flip_dia1(sudoku); } else if (75 <= val && val < 100) { sudoku_flip_dia2(sudoku); } else if (100 <= 125) { sudoku_flip_box_rows(sudoku); } else if (125 < 150) { sudoku_flip_box_rows(sudoku); } else if (150 < 175) { sudoku_flip_rowbox(sudoku); } else if (175 < 200) { sudoku_flip_colbox(sudoku); } } }
void accelerometer(void *arg) { RTIME now; int acceptable; /* * Arguments: &task (NULL=self), start time, period */ rt_task_set_periodic(NULL, TM_NOW, 33333333); while (isRunning) { rt_task_wait_period(NULL); acceptable = 0; //acceptable = rand() % (800 - 0 + 1) + 0; // acceleration between 0-50 acceptable = rand_lim(20); rt_mutex_acquire(&mutex_acc,TM_INFINITE); if(acceptable > acc_sample) acc_sample = acc_sample + 1; else if(acceptable < acc_sample) acc_sample = acc_sample - 1; rt_mutex_release(&mutex_acc); } }
void wavelet_new_randshift (struct wavelet_plan_s* plan) { int i; int maxShift = 1 << (plan->numLevels_tr); for(i = 0; i < plan->numdims_tr; i++) { // Generate random shift value between 0 and maxShift plan->randShift_tr[i] = rand_lim(&plan->state, maxShift); } }
//Swaps columns randomly within a box void sudoku_flip_box_cols(Sudoku sudoku) { int i, col, size = sudoku->size, div_size=sqrt(size); int old_div=0, new_div=div_size; for (i=0; i < size; i++) { col = i % size; while (80 > rand_lim(100)) { //Keep swapping with 70% chance int col1 = old_div + rand_lim(div_size-1); int col2 = old_div + rand_lim(div_size-1); sudoku_swap_col(sudoku, col1, col2); } if ((col % (div_size) == 0) && (col != 0)) { //Entered a new box. Re-set boundaries. old_div = new_div; new_div += div_size; } } }
//Flips rows randomly in each small box void sudoku_flip_box_rows(Sudoku sudoku) { int i, row, size = sudoku->size, div_size=sqrt(size); int old_div=0, new_div=div_size; for (i=0; i < (size*size); i+=size) { row = i/size; while (80 > rand_lim(100)) { //Keep swapping with 80% chance int row1 = old_div + rand_lim(div_size-1); int row2 = old_div + rand_lim(div_size-1); sudoku_swap_row(sudoku, row1, row2); } if ((row % (div_size) == 0) && (row != 0)) { //Entered a new box. Re-set boundaries. old_div = new_div; new_div += div_size; } } }
void generateRoom(){ int room = DRoom -> room; DRoom -> room = DRoom -> room + 1; generateEnemy(); printf("There is a %s in this room\n", Enemy->name); DRoom -> roomClear= 0; DRoom -> roomXp = rand_lim(Player->level) + Player->level + room; generateWep(Player->level); }
void genes_exchange(base **gene1, base **gene2, base **target) { int start_index = rand_lim(EQN_GENE_LENGTH - 1); int end_index = start_index + rand_lim(EQN_GENE_LENGTH - start_index - 1); for(int i = 0; i < EQN_GENE_LENGTH; i++) { if(i < start_index || i > end_index) { target[i]->value = gene1[i]->value; target[i]->terminal = gene1[i]->terminal; } else { target[i]->value = gene2[i]->value; target[i]->terminal = gene2[i]->terminal; } } }
char* initializeBlock(bool data, int size) { int tot = size * 1024; char* buf = new char[tot]; const char* const num = "02390293402384092841" ; if(data) { for(int i = 0; i < tot; i++) { *(buf+i) = num[rand_lim(20)]; } } return buf; }
void gene_randomize(base **gene) { char *bases = "SIR01!gb+-*"; for(int i = 0; i < EQN_GENE_LENGTH; i++) { if(i < EQN_GENE_HEAD_LENGTH) { int choose_terminal = rand_lim(HEAD_TERMINAL_PROBABILITY); int index; if(choose_terminal == 0) { // Choose a terminal index = rand_lim(NUM_TERMINALS - 1); } else { // Choose an operator index = NUM_TERMINALS + rand_lim(NUM_OPERATORS - 1); } gene[i]->value = bases[index]; gene[i]->terminal = true; if(bases[index] == '+' || bases[index] == '-' || bases[index] == '*') { gene[i]->terminal = false; } } else { int index = rand_lim(NUM_TERMINALS - 1); gene[i]->value = bases[index]; gene[i]->terminal = true; } } }
int main(int argc, const char * argv[]) { srand(time(NULL)); int k = atoi(argv[1]); int n = atoi(argv[2]); int i; for(i = 0; i < n; i++) { printf("%d\n", rand_lim(k)); } return 0; }
void sleep_rand(int max_ms) { struct timespec rand_time; unsigned int time_to_sleep = rand_lim(max_ms); rand_time.tv_sec = 0; rand_time.tv_nsec = time_to_sleep * 100000 + 1; if (rand_time.tv_nsec > 999999999) { rand_time.tv_nsec = 999999999; } //fprintf(stderr, "nsec val: %ld\n", rand_time.tv_nsec); if (nanosleep(&rand_time, NULL) != 0) { perror("nanosleep"); exit(1); } }
/** * @brief A pitch will be written into pitch using the given values. * @param pitch: (WIDTH*4-3)*9+9 minimum length, must contain only '\0' chars * @param values: 16 ints, negative values become empty cells, WIDTH-2 maximum chars to write each value */ void create_pitch(int *values) { call_srand(); int p_1 = 0; int p_2 = 0; while(p_1 == p_2) { p_1 = rand_lim(15); p_2 = rand_lim(15); } int p_1_v = rand_lim(3); if (p_1_v < 3) p_1_v = 2; else p_1_v = 4; int p_2_v = rand_lim(3); if (p_2_v < 3) p_2_v = 2; else p_2_v = 4; values[p_1] = p_1_v; values[p_2] = p_2_v; }
Weapon* generateWep(int currlevel){ Weapon *blank = (Weapon *) calloc(1,sizeof(Weapon)); blank -> type = rand_lim(MAX_WTYPES); blank -> part1 = rand_lim(MAX_PART1); blank -> part2 = rand_lim(MAX_PART2); blank -> part3 = rand_lim(MAX_PART3); blank -> lvl = rand_lim(currlevel) - rand_lim(currlevel/4) + currlevel/4; blank -> attk = (int) (wTypeBase[blank->type] * Part1x[blank->part1]*Part2x[blank->part2]*Part3x[blank->part3]) * blank->lvl/( Part1x[blank->part1]+Part2x[blank->part2]+Part3x[blank->part3]); return blank; }
void Mesh::render_new_face(float* xyz) { Vertex new_vertex = Vertex(xyz); new_vertex.set_deletable(true); unsigned int vertex_pos_first = 0; unsigned int vertex_pos_second; float min_dist = distance_bet(new_vertex, verts[vertex_pos_first]); for (unsigned int i = 1; i < verts.size(); i++) { float dist = distance_bet(new_vertex, verts[i]); if (dist < min_dist) { min_dist = dist; vertex_pos_first = i; } } do vertex_pos_second = rand_lim(verts.size()); while (vertex_pos_second == vertex_pos_first); min_dist = distance_bet(new_vertex, verts[vertex_pos_second]); for (unsigned int i = 0; i < verts.size(); i++) { float dist = distance_bet(new_vertex, verts[i]); if (dist < min_dist && i != vertex_pos_first) { min_dist = dist; vertex_pos_second = i; } } addVerts(new_vertex); Face* new_face = new Face(); new_face->addVert(verts.size() - 1); new_face->addVert(vertex_pos_first); new_face->addVert(vertex_pos_second); groups[groups.size() - 1]->addFace(new_face); }
void gyroscope(void *arg) { RTIME now; int randG, i, prev; gyroIndex = 0; /* * Arguments: &task (NULL=self), start time, period */ rt_task_set_periodic(NULL, TM_NOW, 3333333); while (isRunning) { for(i=0; i<BUFFERSIZE; i++){ rt_task_wait_period(NULL); //randG = rand() % (361 - 0 + 1) + 0; // angle between 0-361 randG = rand_lim(360); rt_mutex_acquire(&mutex_gyro,TM_INFINITE); //printf("Random: %d\n", randG); prev = gyroIndex; gyroIndex++; if(gyro_sample[prev] >= 360 && randG > gyro_sample[prev]) gyro_sample[gyroIndex] = 1; else if(gyro_sample[prev] <= 1 && randG < gyro_sample[prev]) gyro_sample[gyroIndex] = 360; else if(randG > gyro_sample[prev]) gyro_sample[gyroIndex] = gyro_sample[prev] + 5; else if(randG < gyro_sample[prev]) gyro_sample[gyroIndex] = gyro_sample[prev] - 5; else gyro_sample[gyroIndex] = gyro_sample[prev]; rt_mutex_release(&mutex_gyro); } rt_mutex_acquire(&mutex_gyro,TM_INFINITE); gyroIndex = 0; rt_mutex_release(&mutex_gyro); } }
/* * Randomly shuffle the entire play queue * * Moves the entrie queue into an array of play_queue pointers. * Shuffles the array, and relink the pointers from the array * as the play_queue. */ void queue_shuffle() { struct play_queue *shuffle_array[queue_entry->size-1]; int cnt = 0; struct play_queue *current = queue_entry->head; if(current == NULL || current->next == NULL) { //printf("Not enough tracks in queue to shuffle\n"); return; } while(cnt < queue_entry->size) { shuffle_array[cnt] = current; current = current->next; cnt++; } int i = queue_entry->size-1; struct play_queue *tmp; for(i = queue_entry->size-1; i > 0; i--) { int rand = rand_lim(i); tmp = shuffle_array[rand]; shuffle_array[rand] = shuffle_array[i]; shuffle_array[i] = tmp; } cnt = 0; queue_entry->head = shuffle_array[0]; current = queue_entry->head; cnt++; while(cnt < queue_entry->size-1) { current->next = shuffle_array[cnt]; current = current->next; cnt++; } current->next = shuffle_array[cnt]; current = current->next; queue_entry->tail = current; current->next = NULL; queue_info(); }
void genes_recombine(base **gene1, base **gene2, base **target) { int crossover_index = rand_lim(EQN_GENE_LENGTH - 1); for(int i = 0; i < EQN_GENE_LENGTH; i++) { if(i <= crossover_index) { target[i]->value = gene1[i]->value; target[i]->terminal = gene1[i]->terminal; } else { target[i]->value = gene2[i]->value; target[i]->terminal = gene2[i]->terminal; } } }
static void wavelet3_thresh_apply(const operator_data_t* _data, float mu, complex float* out, const complex float* in) { const struct wavelet3_thresh_s* data = CAST_DOWN(wavelet3_thresh_s, _data); long shift[data->N]; for (unsigned int i = 0; i < data->N; i++) shift[i] = 0; if (data->randshift) { int levels = wavelet_num_levels(data->N, data->flags, data->dims, data->minsize, 4); for (unsigned int i = 0; i < data->N; i++) if (MD_IS_SET(data->flags, i)) shift[i] = rand_lim((unsigned int*)&data->rand_state, 1 << levels); } wavelet3_thresh(data->N, data->lambda * mu, data->flags, shift, data->dims, out, in, data->minsize, 4, wavelet3_dau2); }
int *createRightSides(int N, int d) { int* right_sides = malloc(N * d * sizeof(int)); int i, j; for (i=0; i<N; ++i) { for (j=0; j<d; ++j) { right_sides[i*d+j] = i+N; } } // shuffle right_sides int temp,r; for (i=N*d-1; i>1; --i) { temp = right_sides[i]; r = rand_lim(i); right_sides[i] = right_sides[r]; right_sides[r] = temp; } return right_sides; }
void random_selector( long array_size, long num_of_points_retained, int *array) { printf("use array of %ld elements\n", array_size); int i, j, m; int *a; /* just a dynamic array */ a = (int *) malloc(array_size * sizeof(*a)); if (NULL == a) { printf("Memory fails"); exit(1); } /* init array with ints between 0..array_size */ for (i = 0; i < array_size; ++i) a[i] = i; /* shuffle it */ srand(time(NULL)); for(i=0; i < array_size-1; i++) { m = i + rand_lim(array_size-i-1); j = a[i]; a[i] = a[m]; a[m] = j; } for (i=0; i<num_of_points_retained; i++) { array[i]=a[i]; // printf("DEBUG1: % d th original :%d \n",i, array[i]); } free(a); }