Fast Gaussian Distributed Pseudorandom Number Generation in Java via the Ziggurat Algorithm

Vincent A. Cicirello
2024-05-30
Abstract:We report on experiments with the ziggurat algorithm for generating Gaussian distributed random numbers. The study utilizes our open source Java implementation that was introduced originally for Java 11 at a time when the Java API only provided the much slower polar method. Our Java implementation of the ziggurat algorithm is a port of the GNU Scientific Library's C implementation. Java 17 introduced a significant overhaul of pseudorandom number generation, including several modern pseudorandom number generators (PRNGs) as well as additional functionality, among which includes switching from the polar method to a modified ziggurat algorithm. In the experiments of this paper, we explore whether there is still a need for our implementation for Java 17+ applications. Our results show that Java 17's modified ziggurat is faster than our implementation for the PRNGs that support it. However, Java 17+ continues to use the polar method for the legacy PRNGs Random, SecureRandom, and ThreadLocalRandom. The linear congruential method of Java's Random class lacks the statistical properties required by Java's modified ziggurat implementation; and SecureRandom and ThreadLocalRandom unfortunately use the polar method as a side-effect of extending Random. Our implementation of the original ziggurat algorithm does not require the same statistical properties of the underlying PRNG as Java 17's optimized version, and can be used with any of these PRNGs, and is especially relevant where pre-Java 17 support is required.
Data Structures and Algorithms,Discrete Mathematics,Probability
What problem does this paper attempt to address?
This paper mainly discusses the problem of fast generation of pseudo-random numbers following the Gaussian distribution using the Ziggurat algorithm in Java. The author ported the Ziggurat algorithm from C implementation in the GNU Scientific Library to Java 11 because the Java API at that time only provided a slower polar coordinate method. With significant improvements in pseudo-random number generation (PRNG) in Java 17, including the introduction of multiple modern PRNGs and a modified version of the Ziggurat algorithm, the author investigated whether their implementations are still applicable to Java 17 and higher versions. In the paper, the author compared their Ziggurat implementation with the built-in Gaussian random number generation method in Java 17 (polar coordinate method for certain PRNG classes, modified Ziggurat method for other PRNG classes). The experimental results showed that the modified Ziggurat algorithm in Java 17 is faster than the original Ziggurat in the PRNG classes that support it. However, for PRNGs that do not support the modified Ziggurat, such as Random, SecureRandom, and ThreadLocalRandom, the original Ziggurat algorithm provided significant performance advantages, especially for applications that need to support versions of Java prior to Java 17. The conclusion of the paper pointed out that for Random and ThreadLocalRandom classes, the author's Ziggurat implementation is 83.12% and 87.72% faster than the polar coordinate method in Java 17, respectively. For SplittableRandom that uses the modified Ziggurat and PRNG introduced in Java 17, Java 17's implementation is faster. The author also proposed a technique to enable ThreadLocalRandom to use the modified Ziggurat, thus improving efficiency. Overall, the paper aims to evaluate and compare the efficiency of generating Gaussian distributed random numbers using the Ziggurat algorithm under different Java versions and PRNG classes, and provides a basis for developers to choose the appropriate method.