# Test the pseudo random generator

Ellen Zhao

Ranch Hand

Posts: 581

posted 12 years ago

It's dangerous to blindingly trust your system's pseudo random generator as far as encryption, simulation or statistics are concerned. This test is based on a theorem given by Ernesto Cesaro ( proof can be found in[KUNTH: the art of computer programming, 1998]).

I was testing it on Java SDK 1.4.2, always got an output of 3.0. But I caculated using the primCounter which given by this program with my pocket Caculator, the value is rather close to 3.1416....... Why didn't the program above print a longer double, but always simply 3.0? Thank you very much in advance.

Regards,

Ellen

[ September 12, 2003: Message edited by: Ellen Zhao ]

I was testing it on Java SDK 1.4.2, always got an output of 3.0. But I caculated using the primCounter which given by this program with my pocket Caculator, the value is rather close to 3.1416....... Why didn't the program above print a longer double, but always simply 3.0? Thank you very much in advance.

Regards,

Ellen

[ September 12, 2003: Message edited by: Ellen Zhao ]

posted 12 years ago

I think because you're truncating the value. In this expression:

int temp = 6 * 100000 / primCounter;

by using "int" for temp, you're effectively limiting the possible results to square roots of integers. Pi^2 is 9.7, rather far from an integer; it's getting truncated to 9, and so the printed result is 3.

To fix, use this:

double temp = 6.0 * 100000 / primCounter;

(note the 6 being changed to 6.0, to make the numerator of this fraction be a double.) You should then get values which are, indeed, close to Pi!

int temp = 6 * 100000 / primCounter;

by using "int" for temp, you're effectively limiting the possible results to square roots of integers. Pi^2 is 9.7, rather far from an integer; it's getting truncated to 9, and so the printed result is 3.

To fix, use this:

double temp = 6.0 * 100000 / primCounter;

(note the 6 being changed to 6.0, to make the numerator of this fraction be a double.) You should then get values which are, indeed, close to Pi!