On 16-07-14 12:00:21, Philippe Meunier wrote:
> Hello,
> 
> According to jot(1)'s man page:
> 
> "$ jot -w %d -r 100000 1 4 | sort -n | uniq -c
> 33306 1
> 33473 2
> 33221 3
> 
> Note that with random sequences, all numbers generated will be smaller
> than the upper bound.  The largest value generated will be a tiny bit
> smaller than the upper bound.  For floating point formats, the value
> is rounded as described before being printed.  For integer formats,
> the highest value printed will be one less than the requested upper
> bound, because the generated value will be truncated."
> 
> The "smaller than the upper bound" part used to be correct but not
> anymore:
> 
> $ uname -a
> OpenBSD something.somewhere 5.9 GENERIC#1561 i386
> $ jot -w %d -r 100000 1 4 | sort -n | uniq -c
> 24729 1
> 25035 2
> 25106 3
> 25130 4
> 
> Looking at the cvs log for jot.c, this seems to be a known change:
> 
> "revision 1.27 [...] Internally, jot -r now uses arc4random_uniform()
> whenever this is clearly possible.  In particular `jot -r 1 10 20'
> yields an unbiased random number between 10 and 20 (both ends
> inclusive) from the shell."
> 
> I only discovered this change today after noticing that one of my
> shell scripts that used to work fine had started to fail with a low
> probability: the script uses jot(1) to generate a sequence of random
> array indexes, and with this change an index can now be out of bounds
> with a probability of about 1/1700.  Fortunately it isn't an important
> script but given the low probability of failure it wasn't exactly fun
> to debug.  Anyway, I don't know which one of jot or jot's man page is
> going to be fixed but I'd advocate for reverting to the previous
> behavior to preserve the semantics of scripts that rely on it.
> 
> Cheers,
> 
> Philippe
> 
Try this patch.  I don't use jot, but in my simple testing I think this is
what you are looking for.
-- 
Edgar Pettijohn
Index: jot.c
===================================================================
RCS file: /cvs/src/usr.bin/jot/jot.c,v
retrieving revision 1.27
diff -u -p -u -r1.27 jot.c
--- jot.c       10 Jan 2016 01:15:52 -0000      1.27
+++ jot.c       14 Jul 2016 22:53:41 -0000
@@ -296,7 +296,6 @@ main(int argc, char *argv[])
                        uintx = pow10 * (ender - begin);
                        if (uintx >= UINT32_MAX)
                                errx(1, "requested range too large");
-                       uintx++;
                }
 
                for (i = 1; i <= reps || infinity; i++) {

Reply via email to