diff options
36 files changed, 104 insertions, 152 deletions
diff --git a/games/arithmetic/arithmetic.c b/games/arithmetic/arithmetic.c index 17cffd4f590..0c14c265d25 100644 --- a/games/arithmetic/arithmetic.c +++ b/games/arithmetic/arithmetic.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arithmetic.c,v 1.17 2009/10/27 23:59:23 deraadt Exp $ */ +/* $OpenBSD: arithmetic.c,v 1.18 2013/08/29 20:22:09 naddy Exp $ */ /* * Copyright (c) 1989, 1993 @@ -124,9 +124,6 @@ main(int argc, char *argv[]) if (argc -= optind) usage(); - /* Seed the random-number generator. */ - srandomdev(); - (void)signal(SIGINT, intr); /* Now ask the questions. */ @@ -177,7 +174,7 @@ problem(void) int left, op, right, result; char line[80]; - op = keys[random() % nkeys]; + op = keys[arc4random_uniform(nkeys)]; if (op != '/') right = getrandom(rangemax + 1, op, 1); retry: @@ -198,7 +195,7 @@ retry: case '/': right = getrandom(rangemax, op, 1) + 1; result = getrandom(rangemax + 1, op, 0); - left = right * result + random() % right; + left = right * result + arc4random_uniform(right); break; } @@ -308,7 +305,7 @@ getrandom(int maxval, int op, int operand) struct penalty **pp, *p; op = opnum(op); - value = random() % (maxval + penalty[op][operand]); + value = arc4random_uniform(maxval + penalty[op][operand]); /* * 0 to maxval - 1 is a number to be used directly; bigger values diff --git a/games/backgammon/backgammon/main.c b/games/backgammon/backgammon/main.c index 8b08c1b16f7..05efc815c4b 100644 --- a/games/backgammon/backgammon/main.c +++ b/games/backgammon/backgammon/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.16 2009/10/27 23:59:23 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.17 2013/08/29 20:22:09 naddy Exp $ */ /* * Copyright (c) 1980, 1993 @@ -100,7 +100,6 @@ main (argc,argv) /* use whole screen for text */ begscr = 0; - srandomdev(); /* seed random number generator */ getarg(argc, argv); args[acnt] = '\0'; diff --git a/games/backgammon/common_source/back.h b/games/backgammon/common_source/back.h index 351342f46a4..cd5bc0610c1 100644 --- a/games/backgammon/common_source/back.h +++ b/games/backgammon/common_source/back.h @@ -1,4 +1,4 @@ -/* $OpenBSD: back.h,v 1.11 2006/12/14 10:14:05 martin Exp $ */ +/* $OpenBSD: back.h,v 1.12 2013/08/29 20:22:10 naddy Exp $ */ /* * Copyright (c) 1980, 1993 @@ -43,7 +43,7 @@ #include <term.h> #include <unistd.h> -#define rnum(r) (random()%r) +#define rnum(r) arc4random_uniform(r) #define D0 dice[0] #define D1 dice[1] #define swap {D0 ^= D1; D1 ^= D0; D0 ^= D1; d0 = 1-d0;} diff --git a/games/battlestar/extern.h b/games/battlestar/extern.h index beff427cb55..edeed086215 100644 --- a/games/battlestar/extern.h +++ b/games/battlestar/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.14 2013/04/05 01:28:27 tedu Exp $ */ +/* $OpenBSD: extern.h,v 1.15 2013/08/29 20:22:10 naddy Exp $ */ /* $NetBSD: extern.h,v 1.5 1995/04/24 12:22:18 cgd Exp $ */ /* @@ -49,7 +49,7 @@ #define BITS (8 * sizeof (int)) #define OUTSIDE (position > 68 && position < 246 && position != 218) -#define rnd(x) (random() % (x)) +#define rnd(x) arc4random_uniform(x) #define max(a,b) ((a) < (b) ? (b) : (a)) /* avoid name collision with sys/param.h */ #define TestBit(array, index) (array[index/BITS] & (1 << (index % BITS))) diff --git a/games/battlestar/init.c b/games/battlestar/init.c index 25e781566a3..e982e3d1aee 100644 --- a/games/battlestar/init.c +++ b/games/battlestar/init.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init.c,v 1.13 2009/10/27 23:59:24 deraadt Exp $ */ +/* $OpenBSD: init.c,v 1.14 2013/08/29 20:22:10 naddy Exp $ */ /* $NetBSD: init.c,v 1.4 1995/03/21 15:07:35 cgd Exp $ */ /* @@ -46,7 +46,6 @@ initialize(const char *filename) puts("First Adventure game written by His Lordship, the honorable"); puts("Admiral D.W. Riggle\n"); location = dayfile; - srandomdev(); username = getutmp(); if (username == NULL) errx(1, "Don't know who you are."); diff --git a/games/bs/bs.c b/games/bs/bs.c index dc7a83b50b0..6a6a605c5b5 100644 --- a/games/bs/bs.c +++ b/games/bs/bs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bs.c,v 1.23 2009/11/14 02:20:43 guenther Exp $ */ +/* $OpenBSD: bs.c,v 1.24 2013/08/29 20:22:11 naddy Exp $ */ /* * Copyright (c) 1986, Bruce Holloway * All rights reserved. @@ -39,8 +39,6 @@ * v2.2 with bugfixes and strategical improvements, March 1998. */ -/* #define _POSIX_SOURCE */ /* ( random() ) */ - #include <sys/param.h> #include <sys/types.h> #include <curses.h> @@ -235,8 +233,6 @@ static void intro(void) { char *tmpname; - srandomdev(); /* Kick the random number generator */ - (void) signal(SIGINT,uninitgame); (void) signal(SIGINT,uninitgame); if(signal(SIGQUIT,SIG_IGN) != SIG_IGN) @@ -344,7 +340,7 @@ static void placeship(int b, ship_t *ss, int vis) static int rnd(int n) { - return(((random() & 0x7FFF) % n)); + return(arc4random_uniform(n)); } static void randomplace(int b, ship_t *ss) diff --git a/games/canfield/canfield/canfield.c b/games/canfield/canfield/canfield.c index 8b340cd461e..3b139dcd99c 100644 --- a/games/canfield/canfield/canfield.c +++ b/games/canfield/canfield/canfield.c @@ -1,4 +1,4 @@ -/* $OpenBSD: canfield.c,v 1.12 2009/10/27 23:59:24 deraadt Exp $ */ +/* $OpenBSD: canfield.c,v 1.13 2013/08/29 20:22:11 naddy Exp $ */ /* $NetBSD: canfield.c,v 1.7 1995/05/13 07:28:35 jtc Exp $ */ /* @@ -534,7 +534,7 @@ shuffle(struct cardtype *deck[]) deck[i]->paid = FALSE; } for (i = decksize-1; i>=0; i--) { - j = random() % decksize; + j = arc4random_uniform(decksize); if (i != j) { temp = deck[i]; deck[i] = deck[j]; @@ -1630,7 +1630,6 @@ initall(void) { int i; - srandomdev(); time(&acctstart); initdeck(deck); uid = getuid(); diff --git a/games/fish/fish.c b/games/fish/fish.c index 5fae3ff340e..5559fa9432f 100644 --- a/games/fish/fish.c +++ b/games/fish/fish.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fish.c,v 1.15 2009/10/27 23:59:24 deraadt Exp $ */ +/* $OpenBSD: fish.c,v 1.16 2013/08/29 20:22:12 naddy Exp $ */ /* $NetBSD: fish.c,v 1.3 1995/03/23 08:28:18 cgd Exp $ */ /*- @@ -98,7 +98,6 @@ main(int argc, char *argv[]) usage(); } - srandomdev(); instructions(); init(); @@ -412,7 +411,7 @@ init(void) int nrandom(int n) { - return((int)random() % n); + return(arc4random_uniform(n)); } int diff --git a/games/fortune/strfile/strfile.c b/games/fortune/strfile/strfile.c index 8c2fd7b8c87..b1cd9a5e59d 100644 --- a/games/fortune/strfile/strfile.c +++ b/games/fortune/strfile/strfile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: strfile.c,v 1.18 2013/08/22 04:43:41 guenther Exp $ */ +/* $OpenBSD: strfile.c,v 1.19 2013/08/29 20:22:12 naddy Exp $ */ /* $NetBSD: strfile.c,v 1.4 1995/04/24 12:23:09 cgd Exp $ */ /*- @@ -427,8 +427,6 @@ randomize(void) int32_t tmp; int32_t *sp; - srandomdev(); - Tbl.str_flags |= STR_RANDOM; cnt = Tbl.str_numstr; @@ -437,7 +435,7 @@ randomize(void) */ for (sp = Seekpts; cnt > 0; cnt--, sp++) { - i = random() % cnt; + i = arc4random_uniform(cnt); tmp = sp[0]; sp[0] = sp[i]; sp[i] = tmp; diff --git a/games/gomoku/main.c b/games/gomoku/main.c index c516fc58a48..bb646880e37 100644 --- a/games/gomoku/main.c +++ b/games/gomoku/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.23 2012/03/04 04:05:15 fgsch Exp $ */ +/* $OpenBSD: main.c,v 1.24 2013/08/29 20:22:13 naddy Exp $ */ /* * Copyright (c) 1994 * The Regents of the University of California. All rights reserved. @@ -128,8 +128,6 @@ main(argc, argv) if (!debug) #ifdef SVR4 srand(time(0)); -#else - srandomdev(); #endif if (interactive) cursinit(); /* initialize curses */ diff --git a/games/gomoku/pickmove.c b/games/gomoku/pickmove.c index 70063bbac8f..1e23af9f294 100644 --- a/games/gomoku/pickmove.c +++ b/games/gomoku/pickmove.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pickmove.c,v 1.11 2009/10/27 23:59:24 deraadt Exp $ */ +/* $OpenBSD: pickmove.c,v 1.12 2013/08/29 20:22:13 naddy Exp $ */ /* * Copyright (c) 1994 * The Regents of the University of California. All rights reserved. @@ -206,7 +206,7 @@ better(sp, sp1, us) #ifdef SVR4 return (rand() & 1); #else - return ((int)random() & 1); + return (arc4random() & 1); #endif } diff --git a/games/hangman/getword.c b/games/hangman/getword.c index d50d6b4abae..01fee069a57 100644 --- a/games/hangman/getword.c +++ b/games/hangman/getword.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getword.c,v 1.7 2009/10/27 23:59:25 deraadt Exp $ */ +/* $OpenBSD: getword.c,v 1.8 2013/08/29 20:22:14 naddy Exp $ */ /* $NetBSD: getword.c,v 1.4 1995/03/23 08:32:45 cgd Exp $ */ /* @@ -54,7 +54,7 @@ getword(void) while (badwords < MAXBADWORDS) { if (countwords) badwords++; - pos = (double) random() / (RAND_MAX + 1.0) * (double) Dict_size; + pos = arc4random_uniform(Dict_size); fseek(inf, pos, SEEK_SET); if (fgets(Word, BUFSIZ, inf) == NULL) continue; diff --git a/games/hangman/ksyms.c b/games/hangman/ksyms.c index 1d75e4ab0bd..fb91d638d65 100644 --- a/games/hangman/ksyms.c +++ b/games/hangman/ksyms.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ksyms.c,v 1.1 2008/04/01 21:05:50 miod Exp $ */ +/* $OpenBSD: ksyms.c,v 1.2 2013/08/29 20:22:14 naddy Exp $ */ /* * Copyright (c) 2008 Miodrag Vallat. @@ -42,7 +42,7 @@ kgetword() size_t symlen; for (tries = 0; tries < MAXBADWORDS; tries++) { - pos = (double) random() / (RAND_MAX + 1.0) * (double)ksymsize; + pos = arc4random_uniform(ksymsize); if (lseek(ksyms, pos + ksymoffs, SEEK_SET) == -1) continue; buflen = read(ksyms, symbuf, BUFSIZ); diff --git a/games/hangman/setup.c b/games/hangman/setup.c index 453fd4b2bc7..8a76cf282be 100644 --- a/games/hangman/setup.c +++ b/games/hangman/setup.c @@ -1,4 +1,4 @@ -/* $OpenBSD: setup.c,v 1.10 2009/10/27 23:59:25 deraadt Exp $ */ +/* $OpenBSD: setup.c,v 1.11 2013/08/29 20:22:14 naddy Exp $ */ /* $NetBSD: setup.c,v 1.3 1995/03/23 08:32:59 cgd Exp $ */ /*- @@ -58,7 +58,6 @@ setup(void) addstr(*sp); } - srandomdev(); if (ksyms) { if (ksetup() != 0) { endwin(); diff --git a/games/hunt/hunt/otto.c b/games/hunt/hunt/otto.c index f14dc295024..cd6ad5967f5 100644 --- a/games/hunt/hunt/otto.c +++ b/games/hunt/hunt/otto.c @@ -1,4 +1,4 @@ -/* $OpenBSD: otto.c,v 1.9 2006/03/27 00:10:15 tedu Exp $ */ +/* $OpenBSD: otto.c,v 1.10 2013/08/29 20:22:14 naddy Exp $ */ /* $NetBSD: otto.c,v 1.2 1997/10/10 16:32:39 lukem Exp $ */ /* * Copyright (c) 1983-2003, Regents of the University of California. @@ -544,15 +544,15 @@ wander() break; } if (j == FRONT - && num_turns > 4 + (random() % - ((flbr[FRONT].flags & BEEN) ? 7 : HEIGHT))) + && 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(random() % NUMDIRECTIONS); + duck(arc4random_uniform(NUMDIRECTIONS)); num_turns = 0; return; } else { diff --git a/games/hunt/huntd/driver.c b/games/hunt/huntd/driver.c index 056f1ddbf04..078779b2192 100644 --- a/games/hunt/huntd/driver.c +++ b/games/hunt/huntd/driver.c @@ -1,4 +1,4 @@ -/* $OpenBSD: driver.c,v 1.19 2009/12/10 23:53:06 beck Exp $ */ +/* $OpenBSD: driver.c,v 1.20 2013/08/29 20:22:15 naddy Exp $ */ /* $NetBSD: driver.c,v 1.5 1997/10/20 00:37:16 lukem Exp $ */ /* * Copyright (c) 1983-2003, Regents of the University of California. @@ -466,9 +466,6 @@ init(int background) if (Server_socket + 1 > Num_fds) Num_fds = Server_socket + 1; - /* Initialise the random seed: */ - srandomdev(); - /* Dig the maze: */ makemaze(); @@ -916,9 +913,7 @@ int rand_num(range) int range; { - if (range == 0) - return 0; - return (random() % range); + return (arc4random_uniform(range)); } /* diff --git a/games/mille/mille.c b/games/mille/mille.c index 0335c38a80a..3d22c25f9fc 100644 --- a/games/mille/mille.c +++ b/games/mille/mille.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mille.c,v 1.18 2009/10/27 23:59:25 deraadt Exp $ */ +/* $OpenBSD: mille.c,v 1.19 2013/08/29 20:22:15 naddy Exp $ */ /* $NetBSD: mille.c,v 1.4 1995/03/24 05:01:48 cgd Exp $ */ /* @@ -78,7 +78,6 @@ main(ac, av) leaveok(Score, TRUE); leaveok(Miles, TRUE); clearok(curscr, TRUE); - srandomdev(); cbreak(); noecho(); signal(SIGINT, rub); diff --git a/games/mille/mille.h b/games/mille/mille.h index cef0024c3a0..4006f1f8174 100644 --- a/games/mille/mille.h +++ b/games/mille/mille.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mille.h,v 1.9 2003/06/03 03:01:40 millert Exp $ */ +/* $OpenBSD: mille.h,v 1.10 2013/08/29 20:22:16 naddy Exp $ */ /* $NetBSD: mille.h,v 1.5 1995/03/24 05:01:51 cgd Exp $ */ /* @@ -154,11 +154,6 @@ # define EXTENSIONPROMPT 7 # define OVERWRITEFILEPROMPT 8 -# ifdef SYSV -# define srandom(x) srand(x) -# define random() rand() -# endif /* SYSV */ - typedef struct { bool coups[NUM_SAFE]; bool can_go; diff --git a/games/mille/roll.c b/games/mille/roll.c index 44864ddd96d..9b662085ef9 100644 --- a/games/mille/roll.c +++ b/games/mille/roll.c @@ -1,4 +1,4 @@ -/* $OpenBSD: roll.c,v 1.5 2009/10/27 23:59:25 deraadt Exp $ */ +/* $OpenBSD: roll.c,v 1.6 2013/08/29 20:22:16 naddy Exp $ */ /* $NetBSD: roll.c,v 1.4 1995/03/24 05:02:07 cgd Exp $ */ /* @@ -47,6 +47,6 @@ roll(ndie, nsides) tot = 0; while (ndie--) - tot += random() % nsides + 1; + tot += arc4random_uniform(nsides) + 1; return tot; } diff --git a/games/monop/monop.c b/games/monop/monop.c index 918557d13d5..db433b67cf7 100644 --- a/games/monop/monop.c +++ b/games/monop/monop.c @@ -1,4 +1,4 @@ -/* $OpenBSD: monop.c,v 1.10 2009/10/27 23:59:26 deraadt Exp $ */ +/* $OpenBSD: monop.c,v 1.11 2013/08/29 20:22:16 naddy Exp $ */ /* $NetBSD: monop.c,v 1.3 1995/03/23 08:34:52 cgd Exp $ */ /* @@ -47,7 +47,6 @@ main(ac, av) int ac; char *av[]; { - srandomdev(); num_luck = sizeof lucky_mes / sizeof (char *); init_decks(); init_monops(); diff --git a/games/monop/roll.c b/games/monop/roll.c index 9d7f45967e3..501e177a204 100644 --- a/games/monop/roll.c +++ b/games/monop/roll.c @@ -1,4 +1,4 @@ -/* $OpenBSD: roll.c,v 1.5 2009/10/27 23:59:26 deraadt Exp $ */ +/* $OpenBSD: roll.c,v 1.6 2013/08/29 20:22:16 naddy Exp $ */ /* $NetBSD: roll.c,v 1.5 1995/03/23 08:35:13 cgd Exp $ */ /* @@ -39,12 +39,10 @@ int roll(ndie, nsides) int ndie, nsides; { - int tot, r; - double num_sides; + int tot; - num_sides = nsides; tot = 0; while (ndie--) - tot += (r = random()) * (num_sides / RAND_MAX) + 1; + tot += arc4random_uniform(nsides) + 1; return tot; } diff --git a/games/phantasia/include.h b/games/phantasia/include.h index b8b874093ef..f489818182f 100644 --- a/games/phantasia/include.h +++ b/games/phantasia/include.h @@ -1,4 +1,4 @@ -/* $OpenBSD: include.h,v 1.3 2001/02/04 02:51:23 pjanzen Exp $ */ +/* $OpenBSD: include.h,v 1.4 2013/08/29 20:22:17 naddy Exp $ */ /* $NetBSD: include.h,v 1.3 1995/04/24 12:24:31 cgd Exp $ */ /* @@ -14,6 +14,7 @@ #include <math.h> #include <setjmp.h> #include <signal.h> +#include <stdint.h> #include <stdlib.h> #include <string.h> #include <time.h> diff --git a/games/phantasia/main.c b/games/phantasia/main.c index 0b5348474b1..14f02408aed 100644 --- a/games/phantasia/main.c +++ b/games/phantasia/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.14 2010/12/15 06:40:39 tedu Exp $ */ +/* $OpenBSD: main.c,v 1.15 2013/08/29 20:22:17 naddy Exp $ */ /* $NetBSD: main.c,v 1.3 1995/04/24 12:24:37 cgd Exp $ */ /* @@ -312,8 +312,7 @@ main(argc, argv) / / RETURN VALUE: none / -/ MODULES CALLED: time(), fopen(), srandom(), error(), getuid(), getlogin(), -/ getpwuid() +/ MODULES CALLED: fopen(), error(), getuid(), getlogin(), getpwuid() / / GLOBAL INPUTS: / @@ -384,8 +383,6 @@ initialstate() if ((Energyvoidfp = fopen(_PATH_VOID, "r+")) == NULL) error(_PATH_VOID); /* NOTREACHED */ - - srandomdev(); /* prime random numbers */ } /**/ /************************************************************************ diff --git a/games/phantasia/misc.c b/games/phantasia/misc.c index 7aaf2092daf..c9c043baf0a 100644 --- a/games/phantasia/misc.c +++ b/games/phantasia/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.14 2010/12/15 06:40:39 tedu Exp $ */ +/* $OpenBSD: misc.c,v 1.15 2013/08/29 20:22:17 naddy Exp $ */ /* $NetBSD: misc.c,v 1.2 1995/03/24 03:59:03 cgd Exp $ */ /* @@ -1574,7 +1574,7 @@ descrstatus(playerp) / / RETURN VALUE: none / -/ MODULES CALLED: random() +/ MODULES CALLED: arc4random() / / GLOBAL INPUTS: none / @@ -1583,19 +1583,13 @@ descrstatus(playerp) / DESCRIPTION: / Convert random integer from library routine into a floating / point number, and divide by the largest possible random number. -/ We mask large integers with 32767 to handle sites that return -/ 31 bit random integers. / *************************************************************************/ double drandom() { - if (sizeof(int) != 2) - /* use only low bits */ - return ((double) (random() & 0x7fff) / 32768.0); - else - return ((double) random() / 32768.0); + return ((double) arc4random() / (UINT32_MAX + 1.0)); } /**/ /************************************************************************ diff --git a/games/phantasia/setup.c b/games/phantasia/setup.c index f188dea8d3e..9ab9691eb68 100644 --- a/games/phantasia/setup.c +++ b/games/phantasia/setup.c @@ -1,4 +1,4 @@ -/* $OpenBSD: setup.c,v 1.11 2011/06/23 03:14:32 deraadt Exp $ */ +/* $OpenBSD: setup.c,v 1.12 2013/08/29 20:22:17 naddy Exp $ */ /* $NetBSD: setup.c,v 1.4 1995/04/24 12:24:41 cgd Exp $ */ /* @@ -25,8 +25,8 @@ void Error(char *, char *); / / RETURN VALUE: none / -/ MODULES CALLED: time(), exit(), stat(), Error(), open(), close(), fopen(), -/ fgets(), floor(), srandomdev(), umask(), strlcpy(), +/ MODULES CALLED: exit(), stat(), Error(), open(), close(), fopen(), +/ fgets(), floor(), umask(), strlcpy(), / unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf() / / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid @@ -82,8 +82,6 @@ main(argc, argv) argc -= optind; argv += optind; - srandomdev(); /* prime random numbers */ - umask(0117); /* only owner can read/write created files */ prefix = getenv("DESTDIR"); @@ -227,7 +225,7 @@ Error(str, file) / / RETURN VALUE: none / -/ MODULES CALLED: random() +/ MODULES CALLED: arc4random() / / GLOBAL INPUTS: none / @@ -240,8 +238,5 @@ Error(str, file) double drandom() { - if (sizeof(int) != 2) - return((double) (random() & 0x7fff) / 32768.0); - else - return((double) random() / 32768.0); + return((double) arc4random() / (UINT32_MAX + 1.0)); } diff --git a/games/quiz/quiz.c b/games/quiz/quiz.c index f25b3aec0c6..86b230bbf4d 100644 --- a/games/quiz/quiz.c +++ b/games/quiz/quiz.c @@ -1,4 +1,4 @@ -/* $OpenBSD: quiz.c,v 1.20 2009/10/27 23:59:26 deraadt Exp $ */ +/* $OpenBSD: quiz.c,v 1.21 2013/08/29 20:22:18 naddy Exp $ */ /* $NetBSD: quiz.c,v 1.9 1995/04/22 10:16:58 cgd Exp $ */ /*- @@ -213,12 +213,11 @@ quiz(void) char *answer, *t, question[LINE_SZ]; const char *s; - srandomdev(); guesses = rights = wrongs = 0; for (;;) { if (qsize == 0) break; - next = random() % qsize; + next = arc4random_uniform(qsize); qp = qlist.q_next; for (i = 0; i < next; i++) qp = qp->q_next; @@ -228,7 +227,7 @@ quiz(void) qsize = next; continue; } - if (tflag && random() % 100 > 20) { + if (tflag && arc4random_uniform(100) > 20) { /* repeat questions in tutorial mode */ while (qp && (!qp->q_asked || qp->q_answered)) qp = qp->q_next; diff --git a/games/rain/rain.c b/games/rain/rain.c index 348a908c22a..1e8960a5446 100644 --- a/games/rain/rain.c +++ b/games/rain/rain.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rain.c,v 1.16 2012/05/27 10:09:33 sthen Exp $ */ +/* $OpenBSD: rain.c,v 1.17 2013/08/29 20:22:18 naddy Exp $ */ /* * Copyright (c) 1980, 1993 @@ -84,7 +84,6 @@ main(int argc, char *argv[]) sleeptime.tv_nsec = delay * 500000; timespecadd(&sleeptime, &sleeptime, &sleeptime); - srandomdev(); initscr(); tcols = COLS - 4; tlines = LINES - 4; @@ -98,16 +97,16 @@ main(int argc, char *argv[]) curs_set(0); for (j = 4; j >= 0; --j) { - xpos[j] = random() % tcols + 2; - ypos[j] = random() % tlines + 2; + xpos[j] = arc4random_uniform(tcols) + 2; + ypos[j] = arc4random_uniform(tlines) + 2; } for (j = 0;;) { if (sig_caught) { endwin(); exit(0); } - x = random() % tcols + 2; - y = random() % tlines + 2; + x = arc4random_uniform(tcols) + 2; + y = arc4random_uniform(tlines) + 2; mvaddch(y, x, '.'); mvaddch(ypos[j], xpos[j], 'o'); if (!j--) diff --git a/games/robots/main.c b/games/robots/main.c index 0c23a0ac935..fc2c2cede84 100644 --- a/games/robots/main.c +++ b/games/robots/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.17 2009/10/27 23:59:26 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.18 2013/08/29 20:22:19 naddy Exp $ */ /* $NetBSD: main.c,v 1.5 1995/04/22 10:08:54 cgd Exp $ */ /* @@ -136,7 +136,6 @@ main(int ac, char *av[]) stdscr = newwin(Y_SIZE, X_SIZE, 0, 0); } - srandomdev(); do { init_field(); for (Level = Start_level; !Dead; Level++) { diff --git a/games/robots/rnd_pos.c b/games/robots/rnd_pos.c index b123430d9c7..9ca16206fc4 100644 --- a/games/robots/rnd_pos.c +++ b/games/robots/rnd_pos.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rnd_pos.c,v 1.5 2009/10/27 23:59:26 deraadt Exp $ */ +/* $OpenBSD: rnd_pos.c,v 1.6 2013/08/29 20:22:19 naddy Exp $ */ /* * Copyright (c) 1980, 1993 @@ -55,5 +55,5 @@ rnd_pos(void) int rnd(int range) { - return (int)random() % range; + return arc4random_uniform(range); } diff --git a/games/sail/extern.h b/games/sail/extern.h index d0446bf4898..106bb71ee4f 100644 --- a/games/sail/extern.h +++ b/games/sail/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.8 2003/06/03 03:01:41 millert Exp $ */ +/* $OpenBSD: extern.h,v 1.9 2013/08/29 20:22:19 naddy Exp $ */ /* $NetBSD: extern.h,v 1.8 1998/09/13 15:27:30 hubertf Exp $ */ /* @@ -61,7 +61,7 @@ extern char nobells; /* -b, don't ring bell before Signal */ extern gid_t gid; extern gid_t egid; -#define die() ((random() >> 3) % 6 + 1) +#define die() (arc4random_uniform(6) + 1) #define sqr(a) ((a) * (a)) #define min(a,b) ((a) < (b) ? (a) : (b)) diff --git a/games/sail/main.c b/games/sail/main.c index 706a60e56a5..30a3f2cb2b3 100644 --- a/games/sail/main.c +++ b/games/sail/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.6 2009/10/27 23:59:27 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.7 2013/08/29 20:22:20 naddy Exp $ */ /* $NetBSD: main.c,v 1.3 1995/04/22 10:37:01 cgd Exp $ */ /* @@ -56,7 +56,6 @@ main(argc, argv) exit(1); close(fd); - srandomdev(); if ((p = strrchr(*argv, '/'))) p++; else diff --git a/games/snake/snake.c b/games/snake/snake.c index 8d13ec9f623..d6ce2d4482d 100644 --- a/games/snake/snake.c +++ b/games/snake/snake.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snake.c,v 1.14 2009/11/13 21:50:12 deraadt Exp $ */ +/* $OpenBSD: snake.c,v 1.15 2013/08/29 20:22:20 naddy Exp $ */ /* $NetBSD: snake.c,v 1.8 1995/04/29 00:06:41 mycroft Exp $ */ /* @@ -170,7 +170,6 @@ main(int argc, char *argv[]) exit(1); } - srandomdev(); penalty = loot = 0; initscr(); #ifdef KEY_LEFT @@ -470,8 +469,8 @@ snrand(struct point *sp) int i; for (;;) { - p.col = random() % ccnt; - p.line = random() % lcnt; + 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) @@ -580,7 +579,7 @@ chase(struct point *np, struct point *sp) } for (w = i = 0; i < 8; i++) w += wt[i]; - vp = ((random() >> 6) & 01777) % w; + vp = arc4random_uniform(w); for (i = 0; i < 8; i++) if (vp < wt[i]) break; @@ -830,7 +829,7 @@ pushsnake(void) if ((same(&snake[i], &you)) || (same(&tmp, &you))) { surround(&you); i = (cashvalue) % 10; - bonus = ((random() >> 8) & 0377) % 10; + bonus = arc4random_uniform(10); mvprintw(lcnt + 1, 0, "%d\n", bonus); refresh(); delay(30); diff --git a/games/tetris/tetris.c b/games/tetris/tetris.c index bf586a071b9..7e7bc9f281a 100644 --- a/games/tetris/tetris.c +++ b/games/tetris/tetris.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tetris.c,v 1.23 2009/10/28 00:25:38 deraadt Exp $ */ +/* $OpenBSD: tetris.c,v 1.24 2013/08/29 20:22:20 naddy Exp $ */ /* $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $ */ /*- @@ -139,8 +139,8 @@ randshape(void) const struct shape *tmp; int i, j; - tmp = &shapes[random() % 7]; - j = random() % 4; + tmp = &shapes[arc4random_uniform(7)]; + j = arc4random_uniform(4); for (i = 0; i < j; i++) tmp = &shapes[classic? tmp->rotc : tmp->rot]; return (tmp); @@ -225,7 +225,6 @@ main(int argc, char *argv[]) scr_init(); setup_board(); - srandomdev(); scr_set(); pos = A_FIRST*B_COLS + (B_COLS/2)-1; diff --git a/games/worm/worm.c b/games/worm/worm.c index 68baffa3271..cbb6b79281c 100644 --- a/games/worm/worm.c +++ b/games/worm/worm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: worm.c,v 1.23 2009/10/27 23:59:27 deraadt Exp $ */ +/* $OpenBSD: worm.c,v 1.24 2013/08/29 20:22:21 naddy Exp $ */ /* * Copyright (c) 1980, 1993 @@ -92,7 +92,6 @@ main(int argc, char **argv) FD_ZERO(&rset); setbuf(stdout, outbuf); - srandomdev(); signal(SIGINT, leave); signal(SIGQUIT, leave); signal(SIGTSTP, suspend); /* process control signal */ @@ -209,7 +208,7 @@ leave(int dummy) int rnd(int range) { - return random() % range; + return arc4random_uniform(range); } void diff --git a/games/worms/worms.c b/games/worms/worms.c index 30b3bd10758..ee78203b0f0 100644 --- a/games/worms/worms.c +++ b/games/worms/worms.c @@ -1,4 +1,4 @@ -/* $OpenBSD: worms.c,v 1.20 2011/04/25 13:30:07 millert Exp $ */ +/* $OpenBSD: worms.c,v 1.21 2013/08/29 20:22:21 naddy Exp $ */ /* * Copyright (c) 1980, 1993 @@ -226,7 +226,6 @@ main(int argc, char *argv[]) sleeptime.tv_nsec = delay * 500000; timespecadd(&sleeptime, &sleeptime, &sleeptime); - srandomdev(); if (!(worm = calloc((size_t)number, sizeof(struct worm)))) nomem(); initscr(); @@ -319,7 +318,7 @@ main(int argc, char *argv[]) break; default: w->orientation = - op->opts[(int)random() % op->nopts]; + op->opts[arc4random_uniform(op->nopts)]; } mvaddch(y += yinc[w->orientation], x += xinc[w->orientation], diff --git a/games/wump/wump.c b/games/wump/wump.c index 5a3967a3066..de9e3f93ddb 100644 --- a/games/wump/wump.c +++ b/games/wump/wump.c @@ -1,4 +1,4 @@ -/* $OpenBSD: wump.c,v 1.25 2009/10/27 23:59:28 deraadt Exp $ */ +/* $OpenBSD: wump.c,v 1.26 2013/08/29 20:22:22 naddy Exp $ */ /* * Copyright (c) 1989, 1993 @@ -201,12 +201,11 @@ main(int argc, char *argv[]) errx(1, "too many tunnels! The cave collapsed!\n(Fortunately, the wumpus escaped!)"); - srandomdev(); if (level == HARD) { if (room_num / 2 - bat_num) - bat_num += (random() % (room_num / 2 - bat_num)); + bat_num += arc4random_uniform(room_num / 2 - bat_num); if (room_num / 2 - pit_num) - pit_num += (random() % (room_num / 2 - pit_num)); + pit_num += arc4random_uniform(room_num / 2 - pit_num); } /* Leave at least two rooms free--one for the player to start in, and @@ -314,7 +313,7 @@ take_action(void) case '\n': return(0); } - if (random() % 15 == 1) + if (arc4random_uniform(15) == 1) (void)printf("Que pasa?\n"); else (void)printf("I don't understand!\n"); @@ -367,7 +366,7 @@ move_to(const char *room_number) if (!tunnel_available) { (void)printf("*Oof!* (You hit the wall)\n"); - if (random() % 6 == 1) { + if (arc4random_uniform(6) == 1) { (void)printf("Your colorful comments awaken the wumpus!\n"); move_wump(); if (wumpus_loc == player_loc) { @@ -380,7 +379,7 @@ move_to(const char *room_number) /* now let's move into that room and check it out for dangers */ /* if (next_room == room_num + 1) - * jump(next_room = (random() % room_num) + 1); + * jump(next_room = arc4random_uniform(room_num) + 1); */ player_loc = next_room; for (;;) { @@ -392,7 +391,7 @@ move_to(const char *room_number) return(1); } if (cave[next_room].has_a_pit) { - if (random() % 12 < 2) { + if (arc4random_uniform(12) < 2) { pit_survive(); return(0); } else { @@ -408,7 +407,8 @@ move_to(const char *room_number) (void)printf( "*flap* *flap* *flap* (humongous bats pick you up and move you%s!)\n", just_moved_by_bats ? " again": ""); - next_room = player_loc = (random() % room_num) + 1; + next_room = player_loc = + arc4random_uniform(room_num) + 1; just_moved_by_bats = 1; } @@ -461,7 +461,7 @@ shoot(char *room_list) if (next == 0) break; /* Old wumpus used room 0 as the terminator */ - chance = random() % 10; + chance = arc4random_uniform(10); if (roomcnt == 4 && chance < 2) { (void)printf( "Your finger slips on the bowstring! *twaaaaaang*\n\ @@ -482,11 +482,12 @@ The arrow is weakly shot and can go no further than room %d!\n",arrow_location); /* if (next > room_num) { * (void)printf( * "A faint gleam tells you the arrow has gone through a magic tunnel!\n"); - * arrow_location = (random() % room_num) + 1; + * arrow_location = + * arc4random_uniform(room_num) + 1; * } else */ arrow_location = next; } else { - link = (random() % link_num); + link = (arc4random_uniform(link_num)); if (cave[arrow_location].tunnel[link] == player_loc) (void)printf( "*thunk* The arrow can't find a way from %d to %d and flies back into\n\ @@ -529,11 +530,13 @@ into room %d!\n", arrow_location, next, cave[arrow_location].tunnel[link]); /* each time you shoot, it's more likely the wumpus moves */ static int lastchance = 2; - if (random() % level == EASY ? 12 : 9 < (lastchance += 2)) { + if (arc4random_uniform(level) == EASY ? + 12 : 9 < (lastchance += 2)) { move_wump(); if (wumpus_loc == player_loc) { wump_walk_kill(); - lastchance = random() % 3; /* Reset for next game */ + /* Reset for next game */ + lastchance = arc4random_uniform(3); return(1); } @@ -587,7 +590,7 @@ cave_init(void) * divisor of (delta + 1) and room_num to be 1 */ do { - delta = (random() % (room_num - 1)) + 1; + delta = arc4random_uniform(room_num - 1) + 1; } while (gcd(room_num, delta + 1) != 1); for (i = 1; i <= room_num; ++i) { @@ -602,7 +605,7 @@ cave_init(void) for (j = 2; j < link_num ; j++) { if (cave[i].tunnel[j] != -1) continue; -try_again: link = (random() % room_num) + 1; +try_again: link = arc4random_uniform(room_num) + 1; /* skip duplicates */ for (k = 0; k < j; k++) if (cave[i].tunnel[k] == link) @@ -611,7 +614,7 @@ try_again: link = (random() % room_num) + 1; if (link == i) goto try_again; cave[i].tunnel[j] = link; - if (random() % 2 == 1) + if (arc4random() % 2 == 1) continue; for (k = 0; k < link_num; ++k) { /* if duplicate, skip it */ @@ -678,7 +681,7 @@ dodecahedral_cave_init(void) for (i = 0; i < 20; i++) loc[i] = i; for (i = 0; i < 20; i++) { - j = random() % (20 - i); + j = arc4random_uniform(20 - i); if (j) { temp = loc[i]; loc[i] = loc[i + j]; @@ -731,7 +734,7 @@ initialize_things_in_cave(void) /* place some bats, pits, the wumpus, and the player. */ for (i = 0; i < bat_num; ++i) { do { - loc = (random() % room_num) + 1; + loc = arc4random_uniform(room_num) + 1; } while (cave[loc].has_a_bat); cave[loc].has_a_bat = 1; #ifdef DEBUG @@ -742,7 +745,7 @@ initialize_things_in_cave(void) for (i = 0; i < pit_num; ++i) { do { - loc = (random() % room_num) + 1; + loc = arc4random_uniform(room_num) + 1; } while (cave[loc].has_a_pit || cave[loc].has_a_bat); /* Above used to be &&; || makes sense but so does just * checking cave[loc].has_a_pit */ @@ -753,14 +756,14 @@ initialize_things_in_cave(void) #endif } - wumpus_loc = (random() % room_num) + 1; + wumpus_loc = arc4random_uniform(room_num) + 1; #ifdef DEBUG if (debug) (void)printf("<wumpus in room %d>\n", wumpus_loc); #endif do { - player_loc = (random() % room_num) + 1; + player_loc = arc4random_uniform(room_num) + 1; } while (player_loc == wumpus_loc || cave[player_loc].has_a_pit || cave[player_loc].has_a_bat); /* Replaced (level == HARD ? @@ -839,7 +842,7 @@ wump_nearby(void) void move_wump(void) { - wumpus_loc = cave[wumpus_loc].tunnel[random() % link_num]; + wumpus_loc = cave[wumpus_loc].tunnel[arc4random_uniform(link_num)]; #ifdef DEBUG if (debug) (void)printf("Wumpus moved to room %d\n",wumpus_loc); |