Bug in ZigguratGaussianSampler?

20 views
Skip to first unread message

Nils Dreyer

unread,
Feb 4, 2016, 7:48:10 AM2/4/16
to SharpNEAT

Hi all,

 

When using the ZigguratGaussian Distribution function for simulating noise on a communication channel I noticed a correlation between the random numbers.

First, I generated 10,000 points in the following way (x- and y-coordinates are generated in the same for loop):


ZigguratGaussianSampler ZGS = new ZigguratGaussianSampler(5);

double[] data_x = new double[10000];
double[] data_y = new double[10000];
 
for (int ix = 1; ix < 10000; ix++ ) {
       data_x
[ix] = ZGS.NextSample(50.0, 10.0);
       data_y
[ix] = ZGS.NextSample(50.0, 10.0);
}
// Matlab >> plot(data_x, data_y, "linestyle","none", "marker", "x");



This results in the following distribution:



There is a kind of pattern inside the "random numbers".

Next I generated the same values in separate for-loops:


ZigguratGaussianSampler ZGS = new ZigguratGaussianSampler(5);
 
double[] data_x = new double[10000];
double[] data_y = new double[10000];

for (int ix = 1; ix < 10000; ix++ ) {
       data_x
[ix] = ZGS.NextSample(50.0, 10.0);
}

for (int ix = 1; ix < 10000; ix++) {
       data_y
[ix] = ZGS.NextSample(50.0, 10.0);
}
// Matlab >> plot(data_x, data_y, "linestyle","none", "marker", "x");

The values now “seem” to be Gaussian distributed:


For me it looks like a bug in the ZigguratGaussianSampler. The created numbers are somehow correlated and not really random. Is this a bug or a feature?


Regards,

Nils


Auto Generated Inline Image 1
Auto Generated Inline Image 2

Colin Green

unread,
Feb 4, 2016, 4:03:22 PM2/4/16
to shar...@googlegroups.com
Hi Nils,

Interesting. I was able to recreate the issue here and it looks to be a bug in FastRandom.NextByte(). 

Change this line:

    return (byte)(_byteBuffer >>=1);

To this

    return (byte)(_byteBuffer >>=8);


It's a performance optimisation where 4 random bytes are generated at a time and buffered for future used, but instead of shifting the buffer to the next byte (8 bits), it actually only shifts the buffer by one bit.

Thanks for bug report.

Colin







 

Nils Dreyer

unread,
Feb 5, 2016, 3:39:27 AM2/5/16
to SharpNEAT
Hi Colin,

This works for me!

Thanks for your help.
Nils
Reply all
Reply to author
Forward
0 new messages