Re: arc4random for src/games

2013-08-29 Thread Todd C. Miller
Looks good but I think this idiom used in hangman:

pos = (double) random() / (RAND_MAX + 1.0) * (double) Dict_size;

Can be replaced with:

pos = arc4random_uniform(Dict_size);

so long as Dict_size = UINT32_MAX, which should always be the case.

A similar idiom is used by monop (compare to mille/roll.c).

 - todd



Re: arc4random for src/games

2013-08-29 Thread Christian Weisgerber
Todd C. Miller:

 Looks good but I think this idiom used in hangman:
 
 pos = (double) random() / (RAND_MAX + 1.0) * (double) Dict_size;
 
 Can be replaced with:
 
 pos = arc4random_uniform(Dict_size);
 
 so long as Dict_size = UINT32_MAX, which should always be the case.

Hmm.  Dict_size and ksymsize are off_t, although they are indeed
unlikely to grow to UINT32_MAX in practice.

 A similar idiom is used by monop (compare to mille/roll.c).

Right.

Updated diff:

Index: arithmetic/arithmetic.c
===
RCS file: /cvs/src/games/arithmetic/arithmetic.c,v
retrieving revision 1.17
diff -u -p -r1.17 arithmetic.c
--- arithmetic/arithmetic.c 27 Oct 2009 23:59:23 -  1.17
+++ arithmetic/arithmetic.c 28 Aug 2013 14:30:16 -
@@ -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 operan
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
Index: backgammon/backgammon/main.c
===
RCS file: /cvs/src/games/backgammon/backgammon/main.c,v
retrieving revision 1.16
diff -u -p -r1.16 main.c
--- backgammon/backgammon/main.c27 Oct 2009 23:59:23 -  1.16
+++ backgammon/backgammon/main.c28 Aug 2013 14:34:54 -
@@ -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';
Index: backgammon/common_source/back.h
===
RCS file: /cvs/src/games/backgammon/common_source/back.h,v
retrieving revision 1.11
diff -u -p -r1.11 back.h
--- backgammon/common_source/back.h 14 Dec 2006 10:14:05 -  1.11
+++ backgammon/common_source/back.h 28 Aug 2013 14:35:11 -
@@ -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;}
Index: battlestar/extern.h
===
RCS file: /cvs/src/games/battlestar/extern.h,v
retrieving revision 1.14
diff -u -p -r1.14 extern.h
--- battlestar/extern.h 5 Apr 2013 01:28:27 -   1.14
+++ battlestar/extern.h 28 Aug 2013 14:36:46 -
@@ -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)))
Index: battlestar/init.c
===
RCS file: /cvs/src/games/battlestar/init.c,v
retrieving revision 1.13
diff -u -p -r1.13 init.c
--- battlestar/init.c   27 Oct 2009 23:59:24 -  1.13
+++ battlestar/init.c   28 Aug 2013 14:37:03 -
@@ -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.);
Index: bs/bs.c
===
RCS file: /cvs/src/games/bs/bs.c,v
retrieving revision 1.23
diff -u -p -r1.23 bs.c
--- bs/bs.c 14 Nov 2009 02:20:43 -  1.23
+++ bs/bs.c 28 Aug 2013 14:39:39 -
@@ -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)
 {
 

arc4random for src/games

2013-08-28 Thread Christian Weisgerber
This replaces srandomdev()+random() with calls to arc4random*() in
src/games.  There isn't much practical benefit to this.  Consider
it a style fix.

I have NOT touched the games that call srandom() with a particular seed
for deterministic gameplay.

Index: arithmetic/arithmetic.c
===
RCS file: /cvs/src/games/arithmetic/arithmetic.c,v
retrieving revision 1.17
diff -u -p -r1.17 arithmetic.c
--- arithmetic/arithmetic.c 27 Oct 2009 23:59:23 -  1.17
+++ arithmetic/arithmetic.c 28 Aug 2013 14:30:16 -
@@ -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 operan
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
Index: backgammon/backgammon/main.c
===
RCS file: /cvs/src/games/backgammon/backgammon/main.c,v
retrieving revision 1.16
diff -u -p -r1.16 main.c
--- backgammon/backgammon/main.c27 Oct 2009 23:59:23 -  1.16
+++ backgammon/backgammon/main.c28 Aug 2013 14:34:54 -
@@ -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';
Index: backgammon/common_source/back.h
===
RCS file: /cvs/src/games/backgammon/common_source/back.h,v
retrieving revision 1.11
diff -u -p -r1.11 back.h
--- backgammon/common_source/back.h 14 Dec 2006 10:14:05 -  1.11
+++ backgammon/common_source/back.h 28 Aug 2013 14:35:11 -
@@ -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;}
Index: battlestar/extern.h
===
RCS file: /cvs/src/games/battlestar/extern.h,v
retrieving revision 1.14
diff -u -p -r1.14 extern.h
--- battlestar/extern.h 5 Apr 2013 01:28:27 -   1.14
+++ battlestar/extern.h 28 Aug 2013 14:36:46 -
@@ -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)))
Index: battlestar/init.c
===
RCS file: /cvs/src/games/battlestar/init.c,v
retrieving revision 1.13
diff -u -p -r1.13 init.c
--- battlestar/init.c   27 Oct 2009 23:59:24 -  1.13
+++ battlestar/init.c   28 Aug 2013 14:37:03 -
@@ -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.);
Index: bs/bs.c
===
RCS file: /cvs/src/games/bs/bs.c,v
retrieving revision 1.23
diff -u -p -r1.23 bs.c
--- bs/bs.c 14 Nov 2009 02:20:43 -  1.23
+++ bs/bs.c 28 Aug 2013 14:39:39 -
@@ -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