On Mon, Dec 07, 2015 at 09:33:47AM +0100, Theo Buehler wrote:
> I think some of these are ok, but I'm unsure about some of the others.
> Here are some of my concerns:
> 
> - since arc4random_uniform can potentially loop indefinitely, it
>   might interfere with predictable timing of some routines.  I can't
>   tell if this is harmless in all cases below.  This applies in
>   particular to the proposed changes in the kernel.

I hadn't considered timing problems. I'll look at it again tonight, but
someone more familiar with the code should certainly look at it before
committing.

> - changing random() to arc4random() in games might have undesired
>   side-effects like interfering with the reproducibility of tests or
>   games.  I think this might apply to awk for the same reason as in the
>   shells.  

The patch for awk was wrong. Updated patch below. I'll look into hack
tonight when I have more time.


> > Index: lib/libc/stdlib/rand.c
> > ===================================================================
> > RCS file: /cvs/src/lib/libc/stdlib/rand.c,v
> > retrieving revision 1.15
> > diff -u -p -r1.15 rand.c
> > --- lib/libc/stdlib/rand.c  13 Sep 2015 08:31:47 -0000      1.15
> > +++ lib/libc/stdlib/rand.c  7 Dec 2015 06:42:17 -0000
> > @@ -50,7 +50,7 @@ int
> >  rand(void)
> >  {
> >     if (rand_deterministic == 0)
> > -           return (arc4random() % ((u_int)RAND_MAX + 1));
> > +           return (arc4random_uniform((u_int)RAND_MAX + 1));
> >     return (rand_r(&next));
> >  }
> >  
> 
> this is modulo 2^n, so I think this one is fine as it is.

It's safe but takes a bit of thinking. I first had it as
                return (arc4random() & RAND_MAX);
which to me is more obviously correct, but since it's safe as is. I have
no strong opinion on this.


> > Index: usr.bin/awk/run.c
> > ===================================================================
> 
> Unsure about this one.  I think deterministic sequences might be desired
> in some circumstances (this one is deterministic when a seed was given).

theo@ also pointed out that awk can be deterministic. Since RAND_MAX is
1 below a power of 2, & is safe. How about

Index: run.c
===================================================================
RCS file: /cvs/src/usr.bin/awk/run.c,v
retrieving revision 1.39
diff -u -p -r1.39 run.c
--- run.c       5 Sep 2015 22:07:10 -0000       1.39
+++ run.c       7 Dec 2015 19:28:31 -0000
@@ -1581,7 +1581,7 @@ Cell *bltin(Node **a, int n)      /* builtin 
                u = (Awkfloat) system(getsval(x)) / 256;   /* 256 is unix-dep */
                break;
        case FRAND:
-               u = (Awkfloat) (random() % RAND_MAX) / RAND_MAX;
+               u = (Awkfloat) (random() & RAND_MAX) / ((u_int)RAND_MAX + 1);
                break;
        case FSRAND:
                if (isrec(x)) {         /* no argument provided */

Reply via email to