File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programming Diversions and the fly likes Test the pseudo random generator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Test the pseudo random generator" Watch "Test the pseudo random generator" New topic

Test the pseudo random generator

Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
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.
[ September 12, 2003: Message edited by: Ellen Zhao ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

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!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Test the pseudo random generator
It's not a secret anymore!