The Lab Book Pages

An online collection of electronics information

Dr. Andrew Greensted

This site uses Google Analytics to track visits. Privacy Statement

Hardware RNG

Creating truly random numbers is no easy task. Doing it in digital hardware is even harder. Generally the best you can do is generate pseudo random numbers. Luckily there are a few ways to accomplish this. This page doesn't explain any theory, or claim to be the best RNG, but it's a starting point for anyone who needs a quick hardware RNG solution.

The Pseudo Random Number Generator

The pseudo random number generator used here is based upon a Cellular Automata. Take a look at the Wikipedia Entry for details. The CA rules and structure in this case were developed at the HP labs. Below are the details of the paper describing describing the CA.

FPGA Implementation of Neighborhood-of-Four Cellular Automata Random Number Generators
, 10th International Symposium on Field Programmable Gate Arrays, 2002, Monterey, California, USA

The CA is an 8x8 2D grid of cells, each connected to 4 neighbours. The state of each cell is updated every clock cycle with a value that is a function of the states of these neighbours.

The VHDL

Below is the VHDL that describes the RNG. To initialise the RNG the reset input has to be pulled high. Then the output, `dOut`, contains the random numbers.

File: `RandomNumberGenerator.vhdl`
```library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity RandomNumberGenerator is
port (   clk            : in     std_logic;
enable         : in     std_logic;
reset          : in     std_logic;
dOut           : out    std_logic_vector(63 downto 0));
end RandomNumberGenerator;

architecture General of RandomNumberGenerator is

-- Declare types
subtype CELL_ROW is std_logic_vector(0 to 7);
type CELL_SQUARE is array (0 to 7) of CELL_ROW;

-- Declare cell array
signal cells : CELL_SQUARE := ((others => '0'),(others => '0'),
(others => '0'),(others => '0'),
(others => '0'),(others => '0'),
(others => '0'),(others => '0'));

-- Declare function to calculate cell next state
function getCellOutput( cell3 : std_logic; cell2 : std_logic;
cell1 : std_logic; cell0 : std_logic) return std_logic is

variable output : std_logic := '0';
variable input  : std_logic_vector(3 downto 0) := (others => '0');

begin

input := cell3 & cell2 & cell1 & cell0;

-- CA 27225
-- h6A59
-- b0110 1010 0101 1001

case input is
when b"0000" => output := '1';
when b"0001" => output := '0';
when b"0010" => output := '0';
when b"0011" => output := '1';

when b"0100" => output := '1';
when b"0101" => output := '0';
when b"0110" => output := '1';
when b"0111" => output := '0';

when b"1000" => output := '0';
when b"1001" => output := '1';
when b"1010" => output := '0';
when b"1011" => output := '1';

when b"1100" => output := '0';
when b"1101" => output := '1';
when b"1110" => output := '1';
when b"1111" => output := '0';

when others  => output := '0';

end case;

return output;

end function;

begin

-- Convert the cell array into a linear output
OutputConnect_row : for row in 0 to 7 generate
begin

OutputConnect_col : for col in 0 to 7 generate
constant cellNum : natural := (row * 8) + col;
begin

dOut(cellNum) <= cells(row)(col);

end generate;

end generate;

-- Connect the cell array
Connect_row : for row in 0 to 7 generate
constant cell0row : natural := (row-2) mod 8;   -- 2n
constant cell1row : natural := row;             -- c
constant cell2row : natural := (row-1) mod 8;   -- n
constant cell3row : natural := (row+2) mod 8;   -- 2s

begin

Connect_col : for col in 0 to 7 generate
constant cell0col : natural := (col-2) mod 8;   -- 2w
constant cell1col : natural := col;             -- c
constant cell2col : natural := (col+2) mod 8;   -- 2e
constant cell3col : natural := (col+1) mod 8;   -- e
begin

CellUpdate : process(clk)

variable cell0 : std_logic;
variable cell1 : std_logic;
variable cell2 : std_logic;
variable cell3 : std_logic;

begin

if (clk'event and clk='1') then

if (reset='1') then

if (row=7 and col=7) then
cells(row)(col) <= '1'; -- Reset cell 7,7 to 1

else
cells(row)(col) <= '0'; -- Reset all other cells to '0'

end if;

elsif (enable='1') then
cell3 := cells(cell3row)(cell3col);
cell2 := cells(cell2row)(cell2col);
cell1 := cells(cell1row)(cell1col);
cell0 := cells(cell0row)(cell0col);

cells(row)(col) <= getCellOutput(cell3, cell2, cell1, cell0);

end if;

end if;

end process;

end generate;

end generate;

end General;
```

Testing the RNG

ModelSim can be used to test the RNG. Below is a testbench file for the RNG. This instantiates a single `RandomNumberGenerator`, performs a reset, and lets the RNG run.

File: `RandomNumberGenerator_TB.vhdl`
```library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity RandomNumberGenerator_TB is
end RandomNumberGenerator_TB;

architecture General of RandomNumberGenerator_TB is

signal clk           : std_logic;
signal enable        : std_logic := '0';
signal reset         : std_logic := '0';
signal dOut          : std_logic_vector(63 downto 0);

constant INT_DELAY   : time := 1 ns;

component RandomNumberGenerator is
port (   clk            : in     std_logic;
enable         : in     std_logic;
reset          : in     std_logic;
dOut           : out    std_logic_vector(63 downto 0));
end component;

begin

RNG : RandomNumberGenerator
port map (  clk         => clk,
enable      => enable,
reset       => reset,
dOut        => dOut);

TestClk : process
variable clkVar : std_logic := '0';
begin
clk      <= clkVar;
clkVar   := not clkVar;
wait for INT_DELAY;
end process;

TB : process
begin
reset    <= '1';
enable   <= '0';
wait until clk='1';

reset    <= '0';
enable   <= '1';
wait;

end process;
```

The do file below will perform the ModelSim test and also generate a file containing the RNG output. The file `rng.lst` can become very large if you run the simulation for a long time. If you're going to run a very long simulation, it is worth commenting out the `view` and `add` wave commands

File: `RandomNumberGenerator_TB.do`
```vlib work
vcom -93 RandomNumberGenerator.vhdl
vcom -93 RandomNumberGenerator_TB.vhdl
vsim -t 100ps -lib work RandomNumberGenerator_TB

view wave -undock
add wave -height 25                 -color red        clk
add wave -height 25                 -color red        reset
add wave -height 25                 -color pink       enable
add wave -height 25 -hex            -color pink       RNG/cells
add wave -height 25                 -color pink       dOut

configure list -delta none

run 5 us

write list rng.lst
```

Checking for Randomness

The DIEHARD test suite can be used to test the generator's output for randomness. These aren't the easiest tools to use, but they seem to be the most recommended.

Download and decompress the C Source Code tar. I've had more success with `source.tar.gz`, rather than `die-c.tar.gz`. You'll need to compile the `asc2bin.c` and the `diehard.c` files. You'll need to have the Fortran to c (f2c) libraries installed.

```> gcc -o asc2bin asc2bin.c -lf2c -lm
> gcc -o diehard diehard.c -lf2c -lm
```

Before you can put DIEHARD to use, you need to convert the output from ModelSim into a different format. The one-liner below will do this. First, `awk` is used to pull out the 2nd column of data (the random numbers) and ignore the first 10 rows (whilst the RNG settles). Next, `tr` removes the `EOL` characters. Finally, fold cuts everything into 80 character long lines. Everything is output to `rng.data`

```> awk '{if (NR>10) {print \$2}}' rng.lst | tr -d '\n' | fold -w 80 > rng.data
```

Use the freshly compiled `asc2bin` program to convert the text file, `rng.data` into a binary format understood by the Diehard program.

```> ./asc2bin
```

Finally, run the Diehard program.

```> ./diehard
```

Randomness Results

The results below are for a 25ms simulation. DIEHARD was run using all the 15 tests available. Interpreting the result is even harder than designing a RNG! But, as far as I can tell if the p-values are evenly distributed, and neither 0 or 1, then the RNG is doing a good job. If anyone can provide further details on the reading/understanding of DIEHARD results, I'll be very happy to hear from them.

File: `rng.out`
```       NOTE: Most of the tests in DIEHARD return a p-value, which
should be uniform on [0,1) if the input file contains truly
independent random bits.   Those p-values are obtained by
p=F(X), where F is the assumed distribution of the sample
random variable X---often normal. But that assumed F is just
an asymptotic approximation, for which the fit will be worst
in the tails. Thus you should not be surprised with
occasional p-values near 0 or 1, such as .0012 or .9983.
When a bit stream really FAILS BIG, you will get p's of 0 or
1 to six or more places.  By all means, do not, as a
Statistician might, think that a p < .025 or p> .975 means
that the RNG has "failed the test at the .05 level".  Such
p's happen among the hundreds that DIEHARD produces, even
with good RNG's.  So keep in mind that " p happens".
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::            This is the BIRTHDAY SPACINGS TEST                 ::
:: Choose m birthdays in a year of n days.  List the spacings    ::
:: between the birthdays.  If j is the number of values that     ::
:: occur more than once in that list, then j is asymptotically   ::
:: Poisson distributed with mean m^3/(4n).  Experience shows n   ::
:: must be quite large, say n>=2^18, for comparing the results   ::
:: to the Poisson distribution with that mean.  This test uses   ::
:: n=2^24 and m=2^9,  so that the underlying distribution for j  ::
:: is taken to be Poisson with lambda=2^27/(2^26)=2.  A sample   ::
:: of 500 j's is taken, and a chi-square goodness of fit test    ::
:: provides a p value.  The first test uses bits 1-24 (counting  ::
:: from the left) from integers in the specified file.           ::
::   Then the file is closed and reopened. Next, bits 2-25 are   ::
:: used to provide birthdays, then 3-26 and so on to bits 9-32.  ::
:: Each set of bits provides a p-value, and the nine p-values    ::
:: provide a sample for a KSTEST.                                ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
BIRTHDAY SPACINGS TEST, M= 512 N=2**24 LAMBDA=  2.0000
Results for rng.bin
For a sample of size 500:     mean
rng.bin          using bits  1 to 24   2.146
duplicate       number       number
spacings       observed     expected
0          57.       67.668
1         113.      135.335
2         147.      135.335
3         104.       90.224
4          50.       45.112
5          22.       18.045
6 to INF          7.        8.282
Chisquare with  6 d.o.f. =    10.07 p-value=  .878343
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  2 to 25   2.004
duplicate       number       number
spacings       observed     expected
0          68.       67.668
1         123.      135.335
2         149.      135.335
3          93.       90.224
4          45.       45.112
5          14.       18.045
6 to INF          8.        8.282
Chisquare with  6 d.o.f. =     3.51 p-value=  .257040
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  3 to 26   1.900
duplicate       number       number
spacings       observed     expected
0          73.       67.668
1         136.      135.335
2         142.      135.335
3          91.       90.224
4          39.       45.112
5          14.       18.045
6 to INF          5.        8.282
Chisquare with  6 d.o.f. =     3.79 p-value=  .295399
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  4 to 27   1.880
duplicate       number       number
spacings       observed     expected
0          68.       67.668
1         167.      135.335
2         119.      135.335
3          83.       90.224
4          37.       45.112
5          20.       18.045
6 to INF          6.        8.282
Chisquare with  6 d.o.f. =    12.26 p-value=  .943576
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  5 to 28   1.878
duplicate       number       number
spacings       observed     expected
0          77.       67.668
1         155.      135.335
2         119.      135.335
3          82.       90.224
4          44.       45.112
5          15.       18.045
6 to INF          8.        8.282
Chisquare with  6 d.o.f. =     7.42 p-value=  .715952
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  6 to 29   2.016
duplicate       number       number
spacings       observed     expected
0          63.       67.668
1         135.      135.335
2         136.      135.335
3          93.       90.224
4          50.       45.112
5          17.       18.045
6 to INF          6.        8.282
Chisquare with  6 d.o.f. =     1.63 p-value=  .049630
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  7 to 30   2.042
duplicate       number       number
spacings       observed     expected
0          60.       67.668
1         127.      135.335
2         159.      135.335
3          79.       90.224
4          47.       45.112
5          20.       18.045
6 to INF          8.        8.282
Chisquare with  6 d.o.f. =     7.22 p-value=  .698746
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  8 to 31   2.032
duplicate       number       number
spacings       observed     expected
0          66.       67.668
1         142.      135.335
2         126.      135.335
3          80.       90.224
4          56.       45.112
5          24.       18.045
6 to INF          6.        8.282
Chisquare with  6 d.o.f. =     7.39 p-value=  .714044
:::::::::::::::::::::::::::::::::::::::::
For a sample of size 500:     mean
rng.bin          using bits  9 to 32   1.982
duplicate       number       number
spacings       observed     expected
0          71.       67.668
1         133.      135.335
2         137.      135.335
3          85.       90.224
4          49.       45.112
5          17.       18.045
6 to INF          8.        8.282
Chisquare with  6 d.o.f. =      .93 p-value=  .011958
:::::::::::::::::::::::::::::::::::::::::
The 9 p-values were
.878343   .257040   .295399   .943576   .715952
.049630   .698746   .714044   .011958
A KSTEST for the 9 p-values yields  .383382

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::            THE OVERLAPPING 5-PERMUTATION TEST                 ::
:: This is the OPERM5 test.  It looks at a sequence of one mill- ::
:: ion 32-bit random integers.  Each set of five consecutive     ::
:: integers can be in one of 120 states, for the 5! possible or- ::
:: derings of five numbers.  Thus the 5th, 6th, 7th,...numbers   ::
:: each provide a state. As many thousands of state transitions  ::
:: are observed,  cumulative counts are made of the number of    ::
:: occurences of each state.  Then the quadratic form in the     ::
:: weak inverse of the 120x120 covariance matrix yields a test   ::
:: equivalent to the likelihood ratio test that the 120 cell     ::
:: counts came from the specified (asymptotically) normal dis-   ::
:: tribution with the specified 120x120 covariance matrix (with  ::
:: rank 99).  This version uses 1,000,000 integers, twice.       ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
OPERM5 test for file rng.bin
For a sample of 1,000,000 consecutive 5-tuples,
chisquare for 99 degrees of freedom= 97.547; p-value= .477514
OPERM5 test for file rng.bin
For a sample of 1,000,000 consecutive 5-tuples,
chisquare for 99 degrees of freedom= 99.109; p-value= .521976
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This is the BINARY RANK TEST for 31x31 matrices. The leftmost ::
:: 31 bits of 31 random integers from the test sequence are used ::
:: to form a 31x31 binary matrix over the field {0,1}. The rank  ::
:: is determined. That rank can be from 0 to 31, but ranks< 28   ::
:: are rare, and their counts are pooled with those for rank 28. ::
:: Ranks are found for 40,000 such random matrices and a chisqua-::
:: re test is performed on counts for ranks 31,30,29 and <=28.   ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Binary rank test for rng.bin
Rank test for 31x31 binary matrices:
rows from leftmost 31 bits of each 32-bit integer
rank   observed  expected (o-e)^2/e  sum
28       218     211.4   .204914     .205
29      5164    5134.0   .175182     .380
30     23070   23103.0   .047271     .427
31     11548   11551.5   .001075     .428
chisquare=  .428 for 3 d. of f.; p-value= .322071
--------------------------------------------------------------
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This is the BINARY RANK TEST for 32x32 matrices. A random 32x ::
:: 32 binary matrix is formed, each row a 32-bit random integer. ::
:: The rank is determined. That rank can be from 0 to 32, ranks  ::
:: less than 29 are rare, and their counts are pooled with those ::
:: for rank 29.  Ranks are found for 40,000 such random matrices ::
:: and a chisquare test is performed on counts for ranks  32,31, ::
:: 30 and <=29.                                                  ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Binary rank test for rng.bin
Rank test for 32x32 binary matrices:
rows from leftmost 32 bits of each 32-bit integer
rank   observed  expected (o-e)^2/e  sum
29       207     211.4   .092324     .092
30      5171    5134.0   .266505     .359
31     22969   23103.0   .777757    1.137
32     11653   11551.5   .891423    2.028
chisquare= 2.028 for 3 d. of f.; p-value= .510598
--------------------------------------------------------------

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This is the BINARY RANK TEST for 6x8 matrices.  From each of  ::
:: six random 32-bit integers from the generator under test, a   ::
:: specified byte is chosen, and the resulting six bytes form a  ::
:: 6x8 binary matrix whose rank is determined.  That rank can be ::
:: from 0 to 6, but ranks 0,1,2,3 are rare; their counts are     ::
:: pooled with those for rank 4. Ranks are found for 100,000     ::
:: random matrices, and a chi-square test is performed on        ::
:: counts for ranks 6,5 and <=4.                                 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Binary Rank Test for rng.bin
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  1 to  8
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          984       944.3       1.669       1.669
r =5        21889     21743.9        .968       2.637
r =6        77127     77311.8        .442       3.079
p=1-exp(-SUM/2)= .78551
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  2 to  9
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          984       944.3       1.669       1.669
r =5        21561     21743.9       1.538       3.207
r =6        77455     77311.8        .265       3.473
p=1-exp(-SUM/2)= .82383
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  3 to 10
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          921       944.3        .575        .575
r =5        21732     21743.9        .007        .581
r =6        77347     77311.8        .016        .598
p=1-exp(-SUM/2)= .25826
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  4 to 11
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          943       944.3        .002        .002
r =5        21725     21743.9        .016        .018
r =6        77332     77311.8        .005        .023
p=1-exp(-SUM/2)= .01168
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  5 to 12
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          956       944.3        .145        .145
r =5        21727     21743.9        .013        .158
r =6        77317     77311.8        .000        .158
p=1-exp(-SUM/2)= .07615
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  6 to 13
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          978       944.3       1.203       1.203
r =5        21646     21743.9        .441       1.643
r =6        77376     77311.8        .053       1.697
p=1-exp(-SUM/2)= .57188
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  7 to 14
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          947       944.3        .008        .008
r =5        21950     21743.9       1.954       1.961
r =6        77103     77311.8        .564       2.525
p=1-exp(-SUM/2)= .71708
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  8 to 15
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          990       944.3       2.212       2.212
r =5        21653     21743.9        .380       2.592
r =6        77357     77311.8        .026       2.618
p=1-exp(-SUM/2)= .72991
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits  9 to 16
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          981       944.3       1.426       1.426
r =5        21640     21743.9        .496       1.923
r =6        77379     77311.8        .058       1.981
p=1-exp(-SUM/2)= .62863
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 10 to 17
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          977       944.3       1.132       1.132
r =5        21880     21743.9        .852       1.984
r =6        77143     77311.8        .369       2.353
p=1-exp(-SUM/2)= .69160
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 11 to 18
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          961       944.3        .295        .295
r =5        22096     21743.9       5.702       5.997
r =6        76943     77311.8       1.759       7.756
p=1-exp(-SUM/2)= .97931
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 12 to 19
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4         1005       944.3       3.902       3.902
r =5        21595     21743.9       1.020       4.921
r =6        77400     77311.8        .101       5.022
p=1-exp(-SUM/2)= .91881
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 13 to 20
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          918       944.3        .733        .733
r =5        21694     21743.9        .115        .847
r =6        77388     77311.8        .075        .922
p=1-exp(-SUM/2)= .36940
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 14 to 21
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          965       944.3        .454        .454
r =5        21673     21743.9        .231        .685
r =6        77362     77311.8        .033        .717
p=1-exp(-SUM/2)= .30145
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 15 to 22
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          967       944.3        .546        .546
r =5        21641     21743.9        .487       1.033
r =6        77392     77311.8        .083       1.116
p=1-exp(-SUM/2)= .42758
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 16 to 23
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          918       944.3        .733        .733
r =5        21856     21743.9        .578       1.310
r =6        77226     77311.8        .095       1.406
p=1-exp(-SUM/2)= .50483
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 17 to 24
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          948       944.3        .014        .014
r =5        21851     21743.9        .528        .542
r =6        77201     77311.8        .159        .701
p=1-exp(-SUM/2)= .29560
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 18 to 25
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          928       944.3        .281        .281
r =5        21796     21743.9        .125        .406
r =6        77276     77311.8        .017        .423
p=1-exp(-SUM/2)= .19056
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 19 to 26
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          959       944.3        .229        .229
r =5        21826     21743.9        .310        .539
r =6        77215     77311.8        .121        .660
p=1-exp(-SUM/2)= .28108
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 20 to 27
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          947       944.3        .008        .008
r =5        21670     21743.9        .251        .259
r =6        77383     77311.8        .066        .324
p=1-exp(-SUM/2)= .14975
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 21 to 28
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          930       944.3        .217        .217
r =5        21767     21743.9        .025        .241
r =6        77303     77311.8        .001        .242
p=1-exp(-SUM/2)= .11402
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 22 to 29
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          909       944.3       1.320       1.320
r =5        21630     21743.9        .597       1.916
r =6        77461     77311.8        .288       2.204
p=1-exp(-SUM/2)= .66783
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 23 to 30
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          910       944.3       1.246       1.246
r =5        21684     21743.9        .165       1.411
r =6        77406     77311.8        .115       1.526
p=1-exp(-SUM/2)= .53368
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 24 to 31
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          938       944.3        .042        .042
r =5        21470     21743.9       3.450       3.492
r =6        77592     77311.8       1.015       4.508
p=1-exp(-SUM/2)= .89501
Rank of a 6x8 binary matrix,
rows formed from eight bits of the RNG rng.bin
b-rank test for bits 25 to 32
OBSERVED   EXPECTED     (O-E)^2/E      SUM
r<=4          949       944.3        .023        .023
r =5        21572     21743.9       1.359       1.382
r =6        77479     77311.8        .362       1.744
p=1-exp(-SUM/2)= .58187
TEST SUMMARY, 25 tests on 100,000 random 6x8 matrices
These should be 25 uniform [0,1] random variables:
.785508     .823833     .258259     .011680     .076153
.571875     .717078     .729908     .628630     .691601
.979310     .918810     .369401     .301445     .427582
.504831     .295599     .190558     .281075     .149745
.114025     .667834     .533678     .895009     .581875
brank test summary for rng.bin
The KS test for those 25 supposed UNI's yields
KS p-value= .000284

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::                   THE BITSTREAM TEST                          ::
:: The file under test is viewed as a stream of bits. Call them  ::
:: b1,b2,... .  Consider an alphabet with two "letters", 0 and 1 ::
:: and think of the stream of bits as a succession of 20-letter  ::
:: "words", overlapping.  Thus the first word is b1b2...b20, the ::
:: second is b2b3...b21, and so on.  The bitstream test counts   ::
:: the number of missing 20-letter (20-bit) words in a string of ::
:: 2^21 overlapping 20-letter words.  There are 2^20 possible 20 ::
:: letter words.  For a truly random string of 2^21+19 bits, the ::
:: number of missing words j should be (very close to) normally  ::
:: distributed with mean 141,909 and sigma 428.  Thus            ::
::  (j-141909)/428 should be a standard normal variate (z score) ::
:: that leads to a uniform [0,1) p value.  The test is repeated  ::
:: twenty times.                                                 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
THE OVERLAPPING 20-tuples BITSTREAM TEST,
20 BITS PER WORD, 2^21 words.
This test samples the bitstream 20 times.
No. missing words should average  141909. with sigma=428.
-----------------------------------        ---------------
tst no  1:  143298 missing words,    3.24 sigmas from mean, p-value= .99941
tst no  2:  143119 missing words,    2.83 sigmas from mean, p-value= .99765
tst no  3:  143320 missing words,    3.30 sigmas from mean, p-value= .99951
tst no  4:  143079 missing words,    2.73 sigmas from mean, p-value= .99686
tst no  5:  143379 missing words,    3.43 sigmas from mean, p-value= .99970
tst no  6:  144148 missing words,    5.23 sigmas from mean, p-value=1.00000
tst no  7:  143034 missing words,    2.63 sigmas from mean, p-value= .99570
tst no  8:  142911 missing words,    2.34 sigmas from mean, p-value= .99037
tst no  9:  142724 missing words,    1.90 sigmas from mean, p-value= .97151
tst no 10:  143635 missing words,    4.03 sigmas from mean, p-value= .99997
tst no 11:  143687 missing words,    4.15 sigmas from mean, p-value= .99998
tst no 12:  143914 missing words,    4.68 sigmas from mean, p-value=1.00000
tst no 13:  143386 missing words,    3.45 sigmas from mean, p-value= .99972
tst no 14:  143748 missing words,    4.30 sigmas from mean, p-value= .99999
tst no 15:  143845 missing words,    4.52 sigmas from mean, p-value=1.00000
tst no 16:  143915 missing words,    4.69 sigmas from mean, p-value=1.00000
tst no 17:  143531 missing words,    3.79 sigmas from mean, p-value= .99992
tst no 18:  144044 missing words,    4.99 sigmas from mean, p-value=1.00000
tst no 19:  144056 missing words,    5.02 sigmas from mean, p-value=1.00000
tst no 20:  143526 missing words,    3.78 sigmas from mean, p-value= .99992

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::             The tests OPSO, OQSO and DNA                      ::
::         OPSO means Overlapping-Pairs-Sparse-Occupancy         ::
:: The OPSO test considers 2-letter words from an alphabet of    ::
:: 1024 letters.  Each letter is determined by a specified ten   ::
:: bits from a 32-bit integer in the sequence to be tested. OPSO ::
:: generates  2^21 (overlapping) 2-letter words  (from 2^21+1    ::
:: "keystrokes")  and counts the number of missing words---that  ::
:: is 2-letter words which do not appear in the entire sequence. ::
:: That count should be very close to normally distributed with  ::
:: mean 141,909, sigma 290. Thus (missingwrds-141909)/290 should ::
:: be a standard normal variable. The OPSO test takes 32 bits at ::
:: a time from the test file and uses a designated set of ten    ::
:: consecutive bits. It then restarts the file for the next de-  ::
:: signated 10 bits, and so on.                                  ::
::                                                               ::
::   The test OQSO is similar, except that it considers 4-letter ::
:: words from an alphabet of 32 letters, each letter determined  ::
:: by a designated string of 5 consecutive bits from the test    ::
:: file, elements of which are assumed 32-bit random integers.   ::
:: The mean number of missing words in a sequence of 2^21 four-  ::
:: letter words,  (2^21+3 "keystrokes"), is again 141909, with   ::
:: sigma = 295.  The mean is based on theory; sigma comes from   ::
:: extensive simulation.                                         ::
::                                                               ::
::    The DNA test considers an alphabet of 4 letters::  C,G,A,T,::
:: determined by two designated bits in the sequence of random   ::
:: integers being tested.  It considers 10-letter words, so that ::
:: as in OPSO and OQSO, there are 2^20 possible words, and the   ::
:: mean number of missing words from a string of 2^21  (over-    ::
:: lapping)  10-letter  words (2^21+9 "keystrokes") is 141909.   ::
:: The standard deviation sigma=339 was determined as for OQSO   ::
:: by simulation.  (Sigma for OPSO, 290, is the true value (to   ::
:: three places), not determined by simulation.                  ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
OPSO test for generator rng.bin
Output: No. missing words (mw), equiv normal variate (z), p-value (p)
mw     z     p
OPSO for rng.bin         using bits 23 to 32        142830  3.175  .9993
OPSO for rng.bin         using bits 22 to 31        142271  1.247  .8938
OPSO for rng.bin         using bits 21 to 30        142220  1.071  .8580
OPSO for rng.bin         using bits 20 to 29        141856  -.184  .4270
OPSO for rng.bin         using bits 19 to 28        141673  -.815  .2076
OPSO for rng.bin         using bits 18 to 27        141381 -1.822  .0342
OPSO for rng.bin         using bits 17 to 26        142049   .482  .6850
OPSO for rng.bin         using bits 16 to 25        141967   .199  .5788
OPSO for rng.bin         using bits 15 to 24        141905  -.015  .4940
OPSO for rng.bin         using bits 14 to 23        141809  -.346  .3647
OPSO for rng.bin         using bits 13 to 22        141957   .164  .5653
OPSO for rng.bin         using bits 12 to 21        142148   .823  .7947
OPSO for rng.bin         using bits 11 to 20        141825  -.291  .3856
OPSO for rng.bin         using bits 10 to 19        141988   .271  .6069
OPSO for rng.bin         using bits  9 to 18        141498 -1.418  .0780
OPSO for rng.bin         using bits  8 to 17        141777  -.456  .3241
OPSO for rng.bin         using bits  7 to 16        141691  -.753  .2258
OPSO for rng.bin         using bits  6 to 15        141738  -.591  .2773
OPSO for rng.bin         using bits  5 to 14        142330  1.451  .9266
OPSO for rng.bin         using bits  4 to 13        141562 -1.198  .1155
OPSO for rng.bin         using bits  3 to 12        141901  -.029  .4885
OPSO for rng.bin         using bits  2 to 11        142004   .326  .6280
OPSO for rng.bin         using bits  1 to 10        141692  -.749  .2268
OQSO test for generator rng.bin
Output: No. missing words (mw), equiv normal variate (z), p-value (p)
mw     z     p
OQSO for rng.bin         using bits 28 to 32        142601  2.345  .9905
OQSO for rng.bin         using bits 27 to 31        142473  1.911  .9720
OQSO for rng.bin         using bits 26 to 30        143111  4.073 1.0000
OQSO for rng.bin         using bits 25 to 29        142602  2.348  .9906
OQSO for rng.bin         using bits 24 to 28        142275  1.240  .8924
OQSO for rng.bin         using bits 23 to 27        141864  -.154  .4389
OQSO for rng.bin         using bits 22 to 26        141916   .023  .5090
OQSO for rng.bin         using bits 21 to 25        142088   .606  .7276
OQSO for rng.bin         using bits 20 to 24        142328  1.419  .9221
OQSO for rng.bin         using bits 19 to 23        142199   .982  .8369
OQSO for rng.bin         using bits 18 to 22        143060  3.901 1.0000
OQSO for rng.bin         using bits 17 to 21        142957  3.551  .9998
OQSO for rng.bin         using bits 16 to 20        141592 -1.076  .1410
OQSO for rng.bin         using bits 15 to 19        142025   .392  .6525
OQSO for rng.bin         using bits 14 to 18        142443  1.809  .9648
OQSO for rng.bin         using bits 13 to 17        142157   .840  .7994
OQSO for rng.bin         using bits 12 to 16        142478  1.928  .9731
OQSO for rng.bin         using bits 11 to 15        142632  2.450  .9929
OQSO for rng.bin         using bits 10 to 14        142575  2.257  .9880
OQSO for rng.bin         using bits  9 to 13        142405  1.680  .9535
OQSO for rng.bin         using bits  8 to 12        141835  -.252  .4005
OQSO for rng.bin         using bits  7 to 11        141296 -2.079  .0188
OQSO for rng.bin         using bits  6 to 10        142536  2.124  .9832
OQSO for rng.bin         using bits  5 to  9        141565 -1.167  .1216
OQSO for rng.bin         using bits  4 to  8        142735  2.799  .9974
OQSO for rng.bin         using bits  3 to  7        142315  1.375  .9155
OQSO for rng.bin         using bits  2 to  6        142764  2.897  .9981
OQSO for rng.bin         using bits  1 to  5        143207  4.399 1.0000
DNA test for generator rng.bin
Output: No. missing words (mw), equiv normal variate (z), p-value (p)
mw     z     p
DNA for rng.bin         using bits 31 to 32        141215 -2.048  .0203
DNA for rng.bin         using bits 30 to 31        142079   .501  .6916
DNA for rng.bin         using bits 29 to 30        142072   .480  .6843
DNA for rng.bin         using bits 28 to 29        141875  -.101  .4597
DNA for rng.bin         using bits 27 to 28        142070   .474  .6822
DNA for rng.bin         using bits 26 to 27        141981   .211  .5837
DNA for rng.bin         using bits 25 to 26        142215   .902  .8164
DNA for rng.bin         using bits 24 to 25        141825  -.249  .4018
DNA for rng.bin         using bits 23 to 24        141439 -1.387  .0827
DNA for rng.bin         using bits 22 to 23        141777  -.390  .3481
DNA for rng.bin         using bits 21 to 22        141487 -1.246  .1064
DNA for rng.bin         using bits 20 to 21        141858  -.151  .4398
DNA for rng.bin         using bits 19 to 20        141741  -.497  .3098
DNA for rng.bin         using bits 18 to 19        142294  1.135  .8718
DNA for rng.bin         using bits 17 to 18        141473 -1.287  .0990
DNA for rng.bin         using bits 16 to 17        141416 -1.455  .0728
DNA for rng.bin         using bits 15 to 16        141942   .096  .5384
DNA for rng.bin         using bits 14 to 15        141987   .229  .5906
DNA for rng.bin         using bits 13 to 14        141888  -.063  .4749
DNA for rng.bin         using bits 12 to 13        141714  -.576  .2822
DNA for rng.bin         using bits 11 to 12        142271  1.067  .8570
DNA for rng.bin         using bits 10 to 11        141540 -1.089  .1380
DNA for rng.bin         using bits  9 to 10        141367 -1.600  .0548
DNA for rng.bin         using bits  8 to  9        141810  -.293  .3848
DNA for rng.bin         using bits  7 to  8        142004   .279  .6100
DNA for rng.bin         using bits  6 to  7        141379 -1.564  .0589
DNA for rng.bin         using bits  5 to  6        142357  1.321  .9067
DNA for rng.bin         using bits  4 to  5        141929   .058  .5231
DNA for rng.bin         using bits  3 to  4        142306  1.170  .8790
DNA for rng.bin         using bits  2 to  3        141718  -.564  .2862
DNA for rng.bin         using bits  1 to  2        142148   .704  .7593

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::     This is the COUNT-THE-1's TEST on a stream of bytes.      ::
:: Consider the file under test as a stream of bytes (four per   ::
:: 32 bit integer).  Each byte can contain from 0 to 8 1's,      ::
:: with probabilities 1,8,28,56,70,56,28,8,1 over 256.  Now let  ::
:: the stream of bytes provide a string of overlapping  5-letter ::
:: words, each "letter" taking values A,B,C,D,E. The letters are ::
:: determined by the number of 1's in a byte::  0,1,or 2 yield A,::
:: 3 yields B, 4 yields C, 5 yields D and 6,7 or 8 yield E. Thus ::
:: we have a monkey at a typewriter hitting five keys with vari- ::
:: ous probabilities (37,56,70,56,37 over 256).  There are 5^5   ::
:: possible 5-letter words, and from a string of 256,000 (over-  ::
:: lapping) 5-letter words, counts are made on the frequencies   ::
:: for each word.   The quadratic form in the weak inverse of    ::
:: the covariance matrix of the cell counts provides a chisquare ::
:: test::  Q5-Q4, the difference of the naive Pearson sums of    ::
:: (OBS-EXP)^2/EXP on counts for 5- and 4-letter cell counts.    ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Test results for rng.bin
Chi-square with 5^5-5^4=2500 d.of f. for sample size:2560000
chisquare  equiv normal  p-value
Results fo COUNT-THE-1's in successive bytes:
byte stream for rng.bin          2466.94      -.467      .320078
byte stream for rng.bin          2451.27      -.689      .245355

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::     This is the COUNT-THE-1's TEST for specific bytes.        ::
:: Consider the file under test as a stream of 32-bit integers.  ::
:: From each integer, a specific byte is chosen , say the left-  ::
:: most::  bits 1 to 8. Each byte can contain from 0 to 8 1's,   ::
:: with probabilitie 1,8,28,56,70,56,28,8,1 over 256.  Now let   ::
:: the specified bytes from successive integers provide a string ::
:: of (overlapping) 5-letter words, each "letter" taking values  ::
:: A,B,C,D,E. The letters are determined  by the number of 1's,  ::
:: in that byte::  0,1,or 2 ---> A, 3 ---> B, 4 ---> C, 5 ---> D,::
:: and  6,7 or 8 ---> E.  Thus we have a monkey at a typewriter  ::
:: hitting five keys with with various probabilities::  37,56,70,::
:: 56,37 over 256. There are 5^5 possible 5-letter words, and    ::
:: from a string of 256,000 (overlapping) 5-letter words, counts ::
:: are made on the frequencies for each word. The quadratic form ::
:: in the weak inverse of the covariance matrix of the cell      ::
:: counts provides a chisquare test::  Q5-Q4, the difference of  ::
:: the naive Pearson  sums of (OBS-EXP)^2/EXP on counts for 5-   ::
:: and 4-letter cell counts.                                     ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Chi-square with 5^5-5^4=2500 d.of f. for sample size: 256000
chisquare  equiv normal  p value
Results for COUNT-THE-1's in specified bytes:
bits  1 to  8  2727.18      3.213      .999343
bits  2 to  9  2440.25      -.845      .199063
bits  3 to 10  2431.63      -.967      .166802
bits  4 to 11  2614.24      1.616      .946916
bits  5 to 12  2494.23      -.082      .467507
bits  6 to 13  2476.92      -.326      .372037
bits  7 to 14  2491.96      -.114      .454719
bits  8 to 15  2438.49      -.870      .192196
bits  9 to 16  2544.50       .629      .735423
bits 10 to 17  2597.59      1.380      .916223
bits 11 to 18  2426.74     -1.036      .150082
bits 12 to 19  2341.97     -2.235      .012715
bits 13 to 20  2511.80       .167      .566266
bits 14 to 21  2539.23       .555      .710472
bits 15 to 22  2664.43      2.325      .989975
bits 16 to 23  2539.10       .553      .709873
bits 17 to 24  2619.61      1.691      .954628
bits 18 to 25  2401.88     -1.388      .082631
bits 19 to 26  2508.30       .117      .546708
bits 20 to 27  2651.73      2.146      .984054
bits 21 to 28  2573.41      1.038      .850392
bits 22 to 29  2434.89      -.921      .178573
bits 23 to 30  2443.39      -.801      .211692
bits 24 to 31  2521.79       .308      .621023
bits 25 to 32  2604.64      1.480      .930547

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::               THIS IS A PARKING LOT TEST                      ::
:: In a square of side 100, randomly "park" a car---a circle of  ::
:: radius 1.   Then try to park a 2nd, a 3rd, and so on, each    ::
:: time parking "by ear".  That is, if an attempt to park a car  ::
:: causes a crash with one already parked, try again at a new    ::
:: random location. (To avoid path problems, consider parking    ::
:: helicopters rather than cars.)   Each attempt leads to either ::
:: a crash or a success, the latter followed by an increment to  ::
:: the list of cars already parked. If we plot n:  the number of ::
:: attempts, versus k::  the number successfully parked, we get a::
:: curve that should be similar to those provided by a perfect   ::
:: random number generator.  Theory for the behavior of such a   ::
:: random curve seems beyond reach, and as graphics displays are ::
:: not available for this battery of tests, a simple characteriz ::
:: ation of the random experiment is used: k, the number of cars ::
:: successfully parked after n=12,000 attempts. Simulation shows ::
:: that k should average 3523 with sigma 21.9 and is very close  ::
:: to normally distributed.  Thus (k-3523)/21.9 should be a st-  ::
:: andard normal variable, which, converted to a uniform varia-  ::
:: ble, provides input to a KSTEST based on a sample of 10.      ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
CDPARK: result of ten tests on file rng.bin
Of 12,000 tries, the average no. of successes
should be 3523 with sigma=21.9
Successes: 3545    z-score:  1.005 p-value: .842447
Successes: 3545    z-score:  1.005 p-value: .842447
Successes: 3530    z-score:   .320 p-value: .625377
Successes: 3539    z-score:   .731 p-value: .767486
Successes: 3542    z-score:   .868 p-value: .807188
Successes: 3531    z-score:   .365 p-value: .642555
Successes: 3539    z-score:   .731 p-value: .767486
Successes: 3496    z-score: -1.233 p-value: .108811
Successes: 3518    z-score:  -.228 p-value: .409702
Successes: 3520    z-score:  -.137 p-value: .445521

square size   avg. no.  parked   sample sigma
100.            3530.500       14.702
KSTEST for the above 10: p=  .823155

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::               THE MINIMUM DISTANCE TEST                       ::
:: It does this 100 times::   choose n=8000 random points in a   ::
:: square of side 10000.  Find d, the minimum distance between   ::
:: the (n^2-n)/2 pairs of points.  If the points are truly inde- ::
:: pendent uniform, then d^2, the square of the minimum distance ::
:: should be (very close to) exponentially distributed with mean ::
:: .995 .  Thus 1-exp(-d^2/.995) should be uniform on [0,1) and  ::
:: a KSTEST on the resulting 100 values serves as a test of uni- ::
:: formity for random points in the square. Test numbers=0 mod 5 ::
:: are printed but the KSTEST is based on the full set of 100    ::
:: random choices of 8000 points in the 10000x10000 square.      ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
This is the MINIMUM DISTANCE test
for random integers in the file rng.bin
Sample no.    d^2     avg     equiv uni
5     .1194    .3292     .113094
10    1.0545    .5702     .653467
15    2.6312    .8902     .928955
20    2.6907    .9965     .933080
25     .2643   1.0946     .233292
30    2.4415   1.1991     .914035
35     .1677   1.4590     .155138
40     .3458   1.3471     .293607
45     .2835   1.2585     .247907
50     .0340   1.1628     .033610
55     .0860   1.1680     .082846
60    2.6036   1.2162     .926955
65    1.0217   1.2002     .641851
70     .8516   1.1808     .575097
75     .0720   1.1774     .069766
80    1.3485   1.1477     .742123
85     .2336   1.1157     .209233
90     .0144   1.0993     .014364
95     .8251   1.1296     .563615
100    3.4086   1.1380     .967474
MINIMUM DISTANCE TEST for rng.bin
Result of KS test on 20 transformed mindist^2's:
p-value= .427126

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::              THE 3DSPHERES TEST                               ::
:: Choose  4000 random points in a cube of edge 1000.  At each   ::
:: point, center a sphere large enough to reach the next closest ::
:: point. Then the volume of the smallest such sphere is (very   ::
:: close to) exponentially distributed with mean 120pi/3.  Thus  ::
:: the radius cubed is exponential with mean 30. (The mean is    ::
:: obtained by extensive simulation).  The 3DSPHERES test gener- ::
:: ates 4000 such spheres 20 times.  Each min radius cubed leads ::
:: to a uniform variable by means of 1-exp(-r^3/30.), then a     ::
::  KSTEST is done on the 20 p-values.                           ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
The 3DSPHERES test for file rng.bin
sample no:  1     r^3=   5.077     p-value= .15569
sample no:  2     r^3=  11.540     p-value= .31932
sample no:  3     r^3=  85.572     p-value= .94229
sample no:  4     r^3=  29.439     p-value= .62518
sample no:  5     r^3=  34.526     p-value= .68364
sample no:  6     r^3=  65.367     p-value= .88683
sample no:  7     r^3=  10.023     p-value= .28401
sample no:  8     r^3=  24.019     p-value= .55095
sample no:  9     r^3=  58.162     p-value= .85612
sample no: 10     r^3=    .810     p-value= .02665
sample no: 11     r^3=  13.453     p-value= .36137
sample no: 12     r^3=  13.447     p-value= .36124
sample no: 13     r^3=  52.152     p-value= .82420
sample no: 14     r^3=  34.210     p-value= .68028
sample no: 15     r^3=  16.248     p-value= .41818
sample no: 16     r^3=  67.559     p-value= .89481
sample no: 17     r^3=  21.954     p-value= .51896
sample no: 18     r^3=  14.856     p-value= .39055
sample no: 19     r^3=  34.521     p-value= .68358
sample no: 20     r^3=  19.331     p-value= .47500
A KS test is applied to those 20 p-values.
---------------------------------------------------------
3DSPHERES test for file rng.bin              p-value= .366727
\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::      This is the SQEEZE test                                  ::
::  Random integers are floated to get uniforms on [0,1). Start- ::
::  ing with k=2^31=2147483647, the test finds j, the number of  ::
::  iterations necessary to reduce k to 1, using the reduction   ::
::  k=ceiling(k*U), with U provided by floating integers from    ::
::  the file being tested.  Such j's are found 100,000 times,    ::
::  then counts for the number of times j was <=6,7,...,47,>=48  ::
::  are used to provide a chi-square test for cell frequencies.  ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
RESULTS OF SQUEEZE TEST FOR rng.bin
Table of standardized frequency counts
( (obs-exp)/sqrt(exp) )^2
for j taking values <=6,7,8,...,47,>=48:
-1.5     -.3    -1.6      .2      .4     1.2
-.8     1.9      .3     1.0    -1.5      .2
-.6      .0    -2.2     -.3      .3     -.9
1.5     1.7    -1.7     -.8      .8      .6
.5      .3     1.8     -.5     1.1    -1.0
-.3      .4    -2.3      .9     -.2     1.1
1.4      .8      .5     -.1      .9     1.0
-1.1
Chi-square with 42 degrees of freedom: 48.814
z-score=   .743  p-value= .782086
______________________________________________________________

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::             The  OVERLAPPING SUMS test                        ::
:: Integers are floated to get a sequence U(1),U(2),... of uni-  ::
:: form [0,1) variables.  Then overlapping sums,                 ::
::   S(1)=U(1)+...+U(100), S2=U(2)+...+U(101),... are formed.    ::
:: The S's are virtually normal with a certain covariance mat-   ::
:: rix.  A linear transformation of the S's converts them to a   ::
:: sequence of independent standard normals, which are converted ::
:: to uniform variables for a KSTEST. The  p-values from ten     ::
:: KSTESTs are given still another KSTEST.                       ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Test no.  1      p-value  .887849
Test no.  2      p-value  .136395
Test no.  3      p-value  .103668
Test no.  4      p-value  .165459
Test no.  5      p-value  .594303
Test no.  6      p-value  .537640
Test no.  7      p-value  .914682
Test no.  8      p-value  .514679
Test no.  9      p-value  .498268
Test no. 10      p-value  .981655
Results of the OSUM test for rng.bin
KSTEST on the above 10 p-values:  .265369

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::     This is the RUNS test.  It counts runs up, and runs down, ::
:: in a sequence of uniform [0,1) variables, obtained by float-  ::
:: ing the 32-bit integers in the specified file. This example   ::
:: shows how runs are counted:  .123,.357,.789,.425,.224,.416,.95::
:: contains an up-run of length 3, a down-run of length 2 and an ::
:: up-run of (at least) 2, depending on the next values.  The    ::
:: covariance matrices for the runs-up and runs-down are well    ::
:: known, leading to chisquare tests for quadratic forms in the  ::
:: weak inverses of the covariance matrices.  Runs are counted   ::
:: for sequences of length 10,000.  This is done ten times. Then ::
:: repeated.                                                     ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
The RUNS test for file rng.bin
Up and down runs in a sample of 10000
_________________________________________________
Run test for rng.bin        :
runs up; ks test for 10 p's: .421407
runs down; ks test for 10 p's: .236630
Run test for rng.bin        :
runs up; ks test for 10 p's: .555940
runs down; ks test for 10 p's: .984019

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This is the CRAPS TEST. It plays 200,000 games of craps, finds::
:: the number of wins and the number of throws necessary to end  ::
:: each game.  The number of wins should be (very close to) a    ::
:: normal with mean 200000p and variance 200000p(1-p), with      ::
:: p=244/495.  Throws necessary to complete the game can vary    ::
:: from 1 to infinity, but counts for all>21 are lumped with 21. ::
:: A chi-square test is made on the no.-of-throws cell counts.   ::
:: Each 32-bit integer from the test file provides the value for ::
:: the throw of a die, by floating to [0,1), multiplying by 6    ::
:: and taking 1 plus the integer part of the result.             ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Results of craps test for rng.bin
No. of wins:  Observed Expected
98780    98585.86
98780= No. of wins, z-score=  .868 pvalue= .80739
Analysis of Throws-per-Game:
Chisq=  28.28 for 20 degrees of freedom, p=  .89702
Throws Observed Expected  Chisq     Sum
1    66835    66666.7    .425     .425
2    37309    37654.3   3.167    3.592
3    26684    26954.7   2.719    6.311
4    19499    19313.5   1.782    8.094
5    13889    13851.4    .102    8.196
6    10057     9943.5   1.295    9.490
7     7188     7145.0    .258    9.749
8     5229     5139.1   1.574   11.322
9     3717     3699.9    .079   11.402
10     2583     2666.3   2.602   14.004
11     1953     1923.3    .458   14.462
12     1392     1388.7    .008   14.469
13      972     1003.7   1.002   15.471
14      749      726.1    .720   16.191
15      546      525.8    .773   16.964
16      351      381.2   2.385   19.349
17      293      276.5    .980   20.329
18      185      200.8   1.248   21.577
19      171      146.0   4.287   25.863
20      121      106.2   2.058   27.921
21      277      287.1    .356   28.278
SUMMARY  FOR rng.bin
p-value for no. of wins: .807388
p-value for throws/game: .897016

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

Results of DIEHARD battery of tests sent to file rng.out
```

All in all, probably not a bad pseudo random number generator at all...

Xilinx ISE Implementation

It seems that the ISE synthesis tools have an issue with the mod operator used to calculate the cell neighbour connections. There is a simple fix to this problem. Rather than allowing any possible negative numbers to be formed, adjust the row and column calculations. The extract below shows the required changes.

Xilinx ISE Adjusted File: `RandomNumberGenerator.vhdl`
```-- Connect the cell array
Connect_row : for row in 0 to 7 generate
constant cell0row : integer := (row+6) mod 8;   -- 2n
constant cell1row : integer := row;             -- c
constant cell2row : integer := (row+7) mod 8;   -- n
constant cell3row : integer := (row+2) mod 8;   -- 2s

begin

Connect_col : for col in 0 to 7 generate
constant cell0col : integer := (col+6) mod 8;   -- 2w
constant cell1col : integer := col;             -- c
constant cell2col : integer := (col+2) mod 8;   -- 2e
constant cell3col : integer := (col+1) mod 8;   -- e
begin

```