help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] $RANDOM excludes value zero when called in a subscript


From: Roger Price
Subject: [Help-bash] $RANDOM excludes value zero when called in a subscript
Date: Tue, 6 Aug 2019 10:20:50 +0200 (CEST)
User-agent: Alpine 2.20 (LSU 67 2015-01-07)

I've come across a curious anomaly when trying to use $RANDOM to generate
pseudo random integers uniformly distributed in the range 0..32767 .

Direct use of $RANDOM works correctly, but $RANDOM in a sub-script does
not generate the value zero.

The test program I wrote may be found at http://rogerprice.org/randtest.sh .
This program automatically generates the required sub-script file in /tmp .

Here is its output running on openSUSE 42.3:

 rprice@titan /tmp ./randtest.sh
 BASH_VERSION=4.3.48(1)-release
 17:08:10 ./randtest.sh run begins ...
   Histogram showing i=0 zero value anomaly for calls of
                $RANDOM in a sub-script.
 ,----------------------------+------------------------------,
 |      $RANDOM used directly |    10    13  ...    19    13 |
 | $RANDOM used in sub-script |     0    14  ...    10    20 |
 |----------------------------+------------------------------|
 |                    Index i |     0     1  ... 32766 32767 |
 '----------------------------+------------------------------'
 Number of samples: 500000
 Expected value of histogram entry: 15.25
 Probability of zero value histogram entry: .0000002359
 17:26:02 ./randtest.sh ends.

The anomaly also appears on Debian stretch BASH_VERSION=4.4.12(1)-release.  Is
this a bug or am I doing something wrong?  - any hint would be very welcome.

Roger



reply via email to

[Prev in Thread] Current Thread [Next in Thread]