File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Test the pseudo random generator" Watch "Test the pseudo random generator" New topic
Author

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.
Regards,
Ellen
[ September 12, 2003: Message edited by: Ellen Zhao ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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: http://aspose.com/file-tools
 
subject: Test the pseudo random generator