void snrand(struct point *sp) { struct point p; int i; for (;;) { p.col = arc4random_uniform(ccnt); p.line = arc4random_uniform(lcnt); /* make sure it's not on top of something else */ if (p.line == 0 && p.col < 5) continue; if (same(&p, &you)) continue; if (same(&p, &money)) continue; if (same(&p, &finish)) continue; for (i = 0; i < 6; i++) if (same(&p, &snake[i])) break; if (i < 6) continue; break; } *sp = p; }
void GatyaTenDrawScene::initCard() { _cards.clear(); for (int i=0;i<CARD_NUM;i++) { Cards card; if (isSRCard()) { int j = arc4random_uniform((int)SRGirlList.size()); card.filePath = SRGirlList.at(j); } else { int j = arc4random_uniform((int)RGirlList.size()); card.filePath = RGirlList.at(j); } _cards.push_back(card); } int tag = 1; for (int x = 0; x < 4; x++) { for (int y = 0; y < 3; y++) { if(y == 2 && x > 1) { break; } createCard(CardSprite::PositionIndex((ONE_CARD_WIDTH*CARD_SCALE)*x+(ONE_CARD_WIDTH*CARD_SCALE)/2+(ONE_CARD_WIDTH*CARD_SCALE)*(x+1)/5, WINSIZE.height*4/5 - ONE_CARD_HEIGHT*CARD_SCALE*y - 25*y), tag); tag++; } } }
Uint32 randomPrimaryColor() { Uint8 r = arc4random_uniform(2); Uint8 g = arc4random_uniform(2); Uint8 b = arc4random_uniform(2); return convertRGBAToColor(r * 255, g * 255, b * 255, 255); }
void initialise_rand_genes(int size, int *expression, double *basal) { int i; for (i = 0; i < size; i++) { basal[i] = arc4random_uniform(200); basal[i] -= 100; basal[i] /= 100; expression[i] = arc4random_uniform(2); } }
Uint32 randomYellowMagentaCyan() { Uint8 components[3] = {0}; u_int32_t comp1 = arc4random_uniform(3); u_int32_t comp2 = comp1; while (comp2 == comp1) { comp2 = arc4random_uniform(3); } components[comp1] = 255; components[comp2] = 255; return convertRGBAToColor(components[0], components[1], components[2], 255); }
int main(int argc, const char * argv[]) { int i = 0,socre = 10,usum = 0; while( i < 10) { int ram1 = arc4random_uniform(90)+10; int ram2 = arc4random_uniform(90)+10; printf("%d + %d = ",ram1,ram2); scanf("%d",&usum); if( usum == ram1 + ram2) socre += 1; else socre -= 1; i++; } printf("您得了%d分 \n",socre); }
void beginFace() //uint8_t red, uint8_t green, uint8_t blue) { // pick r, g and b randomly for now; TODO: handle // colours properly, once I've figure out the file formats uint8_t red = (uint8_t)arc4random_uniform(256); uint8_t green = (uint8_t)arc4random_uniform(256); uint8_t blue = (uint8_t)arc4random_uniform(256); // push the colour to our latch, prepare for new incoming indices colour[0] = red; colour[1] = green; colour[2] = blue; faceIndices.clear(); }
/* * cut: * Cut the deck and set turnover. Actually, we only ASK the * player what card to turn. We do a random one, anyway. */ int cut(bool mycrib, int pos) { int i; bool win; win = FALSE; if (mycrib) { if (!rflag) { /* random cut */ char *foo; /* This is silly, but we should parse user input, * even if we're not actually going to use it. */ do { msg(quiet ? "Cut the deck? " : "How many cards down do you wish to cut the deck? "); foo = get_line(); if (*foo != '\0' && ((i = atoi(foo)) < 4 || i > 36)) msg("Invalid cut"); else *foo = '\0'; } while (*foo != '\0'); } i = arc4random_uniform(CARDS - pos); turnover = deck[i + pos]; addmsg(quiet ? "You cut " : "You cut the "); msgcard(turnover, FALSE); endmsg(); prcrib(mycrib, FALSE); if (turnover.rank == JACK) { msg("I get two for his heels"); win = chkscr(&cscore, 2); } } else { i = arc4random_uniform(CARDS - pos) + pos; turnover = deck[i]; addmsg(quiet ? "I cut " : "I cut the "); msgcard(turnover, FALSE); endmsg(); prcrib(mycrib, FALSE); if (turnover.rank == JACK) { msg("You get two for his heels"); win = chkscr(&pscore, 2); } } makeknown(&turnover, 1); return (win); }
void gentoken(char *buf, int len) { char *p; if (len == 0) return; for (p = buf; len > 1; p++, len--) { if (p == buf) *p = token0cnv[arc4random_uniform(sizeof(token0cnv)-1)]; else *p = tokencnv[arc4random_uniform(sizeof(tokencnv)-1)]; } *p = '\0'; }
/* * randomize: * Randomize the order of the string table. We must be careful * not to randomize across delimiter boundaries. All * randomization is done within each block. */ void randomize(void) { uint32_t cnt, i; off_t tmp; off_t *sp; #if __FreeBSD_version < 800041 srandomdev(); #endif Tbl.str_flags |= STR_RANDOM; cnt = Tbl.str_numstr; /* * move things around randomly */ for (sp = Seekpts; cnt > 0; cnt--, sp++) { #if __FreeBSD_version < 800041 i = random() % cnt; #else i = arc4random_uniform(cnt); #endif tmp = sp[0]; sp[0] = sp[i]; sp[i] = tmp; } }
int pushsnake(void) { int i, bonus; int issame = 0; struct point tmp; /* * My manual says times doesn't return a value. Furthermore, the * snake should get his turn every time no matter if the user is * on a fast terminal with typematic keys or not. * So I have taken the call to times out. */ for (i = 4; i >= 0; i--) if (same(&snake[i], &snake[5])) issame++; if (!issame) pchar(&snake[5], ' '); /* Need the following to catch you if you step on the snake's tail */ tmp.col = snake[5].col; tmp.line = snake[5].line; for (i = 4; i >= 0; i--) snake[i + 1] = snake[i]; chase(&snake[0], &snake[1]); pchar(&snake[1], SNAKETAIL); pchar(&snake[0], SNAKEHEAD); for (i = 0; i < 6; i++) { if ((same(&snake[i], &you)) || (same(&tmp, &you))) { surround(&you); i = (cashvalue) % 10; bonus = arc4random_uniform(10); mvprintw(lcnt + 1, 0, "%d\n", bonus); refresh(); delay(30); if (bonus == i) { spacewarp(1); #ifdef LOGGING logit("bonus"); #endif flushinp(); return(1); } flushinp(); endwin(); if (loot >= penalty) { printf("\nYou and your $%d have been eaten\n", cashvalue); } else { printf("\nThe snake ate you. You owe $%d.\n", -cashvalue); } #ifdef LOGGING logit("eaten"); #endif length(moves); snscore(TOPN); close(rawscores); exit(0); } } return(0); }
static void pkg_add_file_random_suffix(char *buf, int buflen, int suflen) { int nchars = strlen(buf); char *pos; int r; if (nchars + suflen > buflen - 1) { suflen = buflen - nchars - 1; if (suflen <= 0) return; } buf[nchars++] = '.'; pos = buf + nchars; while(suflen --) { #ifndef HAVE_ARC4RANDOM r = rand() % (sizeof(litchar) - 1); #else r = arc4random_uniform(sizeof(litchar) - 1); #endif *pos++ = litchar[r]; } *pos = '\0'; }
u_int16_t pick_proxy_port(void) { /* Random should be good enough for avoiding port collisions. */ return (IPPORT_HIFIRSTAUTO + arc4random_uniform(IPPORT_HILASTAUTO - IPPORT_HIFIRSTAUTO)); }
static size_t db_random(size_t mod) { if (cold) return (random() % mod); return (arc4random_uniform(mod)); }
TEST(argdata_create_timestamp, bad) { // Nanoseconds out of bounds. { struct timespec ts = {.tv_nsec = -1}; ASSERT_EQ(NULL, argdata_create_timestamp(&ts)); ASSERT_EQ(EINVAL, errno); } { struct timespec ts = {.tv_nsec = 1000000000}; ASSERT_EQ(NULL, argdata_create_timestamp(&ts)); ASSERT_EQ(EINVAL, errno); } } TEST(argdata_create_timestamp, random) { for (size_t i = 0; i < 1000; ++i) { // Create timestamp object. struct timespec ts1; arc4random_buf(&ts1.tv_sec, sizeof(ts1.tv_sec)); ts1.tv_nsec = arc4random_uniform(1000000000); argdata_t *ad = argdata_create_timestamp(&ts1); ASSERT_NE(NULL, ad); // Validate that it holds the same timestamp value. struct timespec ts2; ASSERT_EQ(0, argdata_get_timestamp(ad, &ts2)); ASSERT_EQ(ts1.tv_sec, ts2.tv_sec); ASSERT_EQ(ts1.tv_nsec, ts2.tv_nsec); argdata_free(ad); } }
void sym_getword(void) { uint tries; off_t pos; int buflen; char symbuf[1 + BUFSIZ], *sym, *end; size_t symlen; for (tries = 0; tries < MAXBADWORDS; tries++) { pos = arc4random_uniform(symsize); if (lseek(symfd, pos + symoffs, SEEK_SET) == -1) continue; buflen = read(symfd, symbuf, BUFSIZ); if (buflen < 0) continue; /* * The buffer is hopefully large enough to hold at least * a complete symbol, i.e. two occurences of NUL, or * one occurence of NUL and the buffer containing the end * of the string table. We make sure the buffer will be * NUL terminated in all cases. */ if (buflen + pos >= symsize) buflen = symsize - pos; *(end = symbuf + buflen) = '\0'; for (sym = symbuf; *sym != '\0'; sym++) ; if (sym == end) continue; symlen = strlen(++sym); if (symlen < MINLEN || symlen > MAXLEN) continue; /* ignore symbols containing dots or dollar signs */ if (strchr(sym, '.') != NULL || strchr(sym, '$') != NULL) continue; break; } if (tries >= MAXBADWORDS) { mvcur(0, COLS - 1, LINES -1, 0); endwin(); errx(1, "can't seem a suitable symbol in %s", Dict_name); } strlcpy(Word, sym, sizeof Word); strlcpy(Known, sym, sizeof Known); for (sym = Known; *sym != '\0'; sym++) { if (*sym == '-') *sym = '_'; /* try not to confuse player */ if (isalnum((unsigned char)*sym)) *sym = '-'; } }
int main(int argc, const char * argv[]) { /* 0:剪刀 1:石头 2:布 计算机出拳:随机生成0 1 2 人出拳:只能输入0 1 2 比较计算机和人的出拳 */ int computer = -1; int player = -1; computer = arc4random_uniform(3); printf("%d\n",computer); printf("请出拳,只能输入0,1,2\n0剪刀,1石头,2布\n"); scanf("%d",&player); if (player < 0 ||player > 2){ printf("请按套路出拳\n"); } else{ if ((player == 0 && computer == 2)|| (player == 1 && computer == 0)|| (player == 2 && computer == 1)) { printf("玩家赢\n"); } else if ((computer == 0 && player == 2)|| (computer == 1 && player == 0)|| (computer == 2 && player == 1)){ printf("电脑赢\n");} else printf("平局\n"); } return 0; }
int take_action(void) { /* * Do the action specified by the player, either 'm'ove, 's'hoot * or something exceptionally bizarre and strange! Returns 1 * iff the player died during this turn, otherwise returns 0. */ switch (*answer) { case 'M': case 'm': /* move */ return(move_to(answer + 1)); case 'S': case 's': /* shoot */ return(shoot(answer + 1)); case 'Q': case 'q': case 'x': exit(0); case '\n': return(0); } if (arc4random_uniform(15) == 1) (void)printf("Que pasa?\n"); else (void)printf("I don't understand!\n"); return(0); }
int process_input(Map *game) { printf("\n> "); char ch = getchar(); getchar(); // eat ENTER // srand and rand are obsoleted by arc4random() // arc4random_uniform() will return a uniformly distributed random number less than upper_bound. int damage = arc4random_uniform(4); switch(ch) { case -1: printf("Giving up? You suck.\n"); return 0; break; case 'n': game->_(move)(game, NORTH); break; case 's': game->_(move)(game, SOUTH); break; case 'e': game->_(move)(game, EAST); break; case 'w': game->_(move)(game, WEST); break; case 'a': game->_(attack)(game, damage); break; case 'l': printf("You can go:\n"); if (game->location->north) { printf("NORTH\n"); } if (game->location->south) { printf("SOUTH\n"); } if (game->location->east) { printf("EAST\n"); } if (game->location->west) { printf("WEST\n"); } break; default: printf("What?: %d\n", ch); } return 1; }
static int select_dispatch(struct event_base *base, void *arg, struct timeval *tv) { int res, i, j; struct selectop *sop = arg; check_selectop(sop); memcpy(sop->event_readset_out, sop->event_readset_in, sop->event_fdsz); memcpy(sop->event_writeset_out, sop->event_writeset_in, sop->event_fdsz); res = select(sop->event_fds + 1, sop->event_readset_out, sop->event_writeset_out, NULL, tv); check_selectop(sop); if (res == -1) { if (errno != EINTR) { event_warn("select"); return (-1); } evsignal_process(base); return (0); } else if (base->sig.evsignal_caught) { evsignal_process(base); } event_debug(("%s: select reports %d", __func__, res)); check_selectop(sop); i = arc4random_uniform(sop->event_fds + 1); for (j = 0; j <= sop->event_fds; ++j) { struct event *r_ev = NULL, *w_ev = NULL; if (++i >= sop->event_fds+1) i = 0; res = 0; if (FD_ISSET(i, sop->event_readset_out)) { r_ev = sop->event_r_by_fd[i]; res |= EV_READ; } if (FD_ISSET(i, sop->event_writeset_out)) { w_ev = sop->event_w_by_fd[i]; res |= EV_WRITE; } if (r_ev && (res & r_ev->ev_events)) { event_active(r_ev, res & r_ev->ev_events, 1); } if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) { event_active(w_ev, res & w_ev->ev_events, 1); } } check_selectop(sop); return (0); }
// RANDOM NUMBER GENERATOR (USE POINTERS FOR MORE EFFICIENCY) double cm_random(double *min, double *max) { #ifdef MAC_VERSION return *min + ((*max - *min) * (((double)arc4random_uniform(RANDMAX)) / (double)RANDMAX)); #endif #ifdef WIN_VERSION return *min + ((*max - *min) * ((double)(rand() % RANDMAX) / (double)RANDMAX)); #endif }
void client(const struct child *c) { char buf[1024]; struct sockaddr_un sun; socklen_t sunlen; ssize_t n; int s, val; arc4random_buf(buf, sizeof(buf)); redo: if ((s = socket(PF_UNIX, SOCK_DGRAM, 0)) == -1) err(1, "%s socket", c->c_name); val = 256*1024; if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) == -1) err(1, "%s setsockopt SO_SNDBUF", c->c_name); sun.sun_len = sizeof(sun); sun.sun_family = AF_UNIX; strlcpy(sun.sun_path, c->c_sock, sizeof(sun.sun_path)); sunlen = sizeof(sun); if (connect(s, (struct sockaddr *)&sun, sunlen) == -1) err(1, "%s connect", c->c_name); while (1) { switch (arc4random_uniform(1000)) { case 0: reconnect: if (close(s) == -1) err(1, "%s close", c->c_name); goto redo; case 1: _exit(0); default: if ((n = send(s, buf, arc4random_uniform( sizeof(buf)), 0)) == -1) { if (errno == ECONNRESET || errno == ECONNREFUSED) goto reconnect; err(1, "%s send", c->c_name); sleep(1); } printf("%s send %zd\n", c->c_name, n); sleep(0); } } }
void two_point_crossover(int *first_child, int *second_child, int *father, int *mother, int population_size, int team_size) { //get a value in [1..teamcount - 1] int firstCrossOverPoint = arc4random_uniform(team_size - 2) + 1; int secondCrossOverPoint = arc4random_uniform(team_size - (firstCrossOverPoint + 1)) + (firstCrossOverPoint + 1); for (int i = 0; i < population_size; i++) { if (i < (firstCrossOverPoint * (team_size - 1)) || (i >= secondCrossOverPoint * (team_size - 1))) { first_child[i] = mother[i]; second_child[i] = father[i]; } else { first_child[i] = father[i]; second_child[i] = mother[i]; } } }
/* * exported semaphores */ int sem_init(sem_t *semp, int pshared, unsigned int value) { sem_t sem; if (value > SEM_VALUE_MAX) { errno = EINVAL; return (-1); } if (pshared) { errno = EPERM; return (-1); #ifdef notyet char name[SEM_RANDOM_NAME_LEN]; sem_t *sempshared; int i; for (;;) { for (i = 0; i < SEM_RANDOM_NAME_LEN - 1; i++) name[i] = arc4random_uniform(255) + 1; name[SEM_RANDOM_NAME_LEN - 1] = '\0'; sempshared = sem_open(name, O_CREAT | O_EXCL, 0, value); if (sempshared != SEM_FAILED) break; if (errno == EEXIST) continue; if (errno != EPERM) errno = ENOSPC; return (-1); } /* unnamed semaphore should not be opened twice */ if (sem_unlink(name) == -1) { sem_close(sempshared); errno = ENOSPC; return (-1); } *semp = *sempshared; free(sempshared); return (0); #endif } sem = calloc(1, sizeof(*sem)); if (!sem) { errno = ENOSPC; return (-1); } sem->lock = _SPINLOCK_UNLOCKED_ASSIGN; sem->value = value; *semp = sem; return (0); }
uint32_t queue_generate_msgid(void) { uint32_t msgid; while ((msgid = arc4random_uniform(0xffffffff)) == 0) ; return msgid; }
static char generate_printable() { char c = 0; while (!isprint(c)) c = arc4random_uniform(128); return (c); }
bool GatyaTenDrawScene::isSRCard() { int i = arc4random_uniform(1000); if (i < 15) { return true; } return false; }
static void wander(void) { int i, j, rel_dir, dir_mask, dir_count; for (i = 0; i < NUMDIRECTIONS; i++) if (!(flbr[i].flags & BEEN) || flbr[i].distance <= 1) break; if (i == NUMDIRECTIONS) memset(been_there, 0, sizeof been_there); dir_mask = dir_count = 0; for (i = 0; i < NUMDIRECTIONS; i++) { j = (RIGHT + i) % NUMDIRECTIONS; if (flbr[j].distance <= 1 || flbr[j].flags & DEADEND) continue; if (!(flbr[j].flags & BEEN_SAME)) { dir_mask = 1 << j; dir_count = 1; break; } if (j == FRONT && num_turns > 4 + (arc4random_uniform( ((flbr[FRONT].flags & BEEN) ? 7 : HEIGHT)))) continue; dir_mask |= 1 << j; dir_count = 1; break; } if (dir_count == 0) { duck(arc4random_uniform(NUMDIRECTIONS)); num_turns = 0; return; } else { rel_dir = ffs(dir_mask) - 1; } if (rel_dir == FRONT) num_turns++; else num_turns = 0; face_and_move_direction(rel_dir, 1); }
/** * Return a random time. * */ time_t ods_rand(time_t mod) { #ifdef HAVE_ARC4RANDOM_UNIFORM return (time_t) (arc4random_uniform((uint32_t) mod+1)); #elif HAVE_ARC4RANDOM return (time_t) (arc4random() % (unsigned) mod+1); #else return (time_t) (random() % (unsigned) mod+1); #endif }
void mutate(float chance, int *population, int population_size, int **playable_dates, int *playable_date_sizes, int team_size) { int treshold = chance * 1000; for (int i = 0; i < team_size; i++) { if (arc4random_uniform(1000) <= treshold) { choose_random((population + (i * (team_size - 1))), team_size - 1, playable_dates[i], playable_date_sizes[i]); } } }