RNG Algorithm Comparator

Compare different random number generator algorithms and analyze their statistical properties

Overview

The RNG Algorithm Comparator lets you compare different pseudo-random number generator algorithms including LCG, Mersenne Twister, XorShift, Mulberry32, and JavaScript’s Math.random(). Evaluate statistical quality through chi-square tests, runs tests, and serial correlation analysis, while visualizing distribution patterns and 2D plots that reveal structural weaknesses. Benchmark performance across algorithms and understand the trade-offs between speed, quality, and statistical properties to choose the right RNG for your application.

Tips and Tricks

Getting Started

  1. Select an algorithm from the dropdown menu
  2. Click “Run Tests” to generate random numbers and analyze them
  3. Review the results - check both visual patterns and statistical test outcomes
  4. Compare algorithms by running tests on different RNGs
  5. Check performance to see speed differences between algorithms

Understanding the Algorithms

Linear Congruential Generator (LCG) - Simplest and fastest PRNG - Known statistical weaknesses - visible patterns in 2D plots - Best for: Educational purposes, simple simulations - Avoid for: Scientific computing, serious applications

Mersenne Twister - Industry standard with excellent statistical properties - Very long period (2^19937 - 1) - Best for: Scientific computing, simulations, general use - Avoid for: Cryptography (not cryptographically secure)

XorShift - Very fast with good quality - Uses XOR and bit-shift operations - Best for: Game development, real-time applications - Note: Simpler variant than XorShift128+

Mulberry32 - Good balance of speed and quality - Simple 32-bit implementation - Best for: General-purpose applications - Note: Relatively new, less battle-tested

Math.random() - Browser/JavaScript engine dependent - Usually high quality (often XorShift or similar) - Best for: Web applications, when you can’t control implementation - Note: Can’t set seed for reproducibility

Interpreting Statistical Tests

Chi-Square Test (Tests uniformity) - Pass (p > 0.05): Values are uniformly distributed - Fail (p ≤ 0.05): Bias toward certain values detected - What to watch: Mersenne Twister and XorShift should pass; LCG may fail

Runs Test (Tests independence) - Pass (p > 0.05): Values appear independent - Fail (p ≤ 0.05): Patterns in sequence detected - What to watch: Too many or too few runs indicate correlation

Serial Correlation (Tests correlation between consecutive values) - Good (≈ 0): No linear relationship between consecutive values - Bad (far from 0): Consecutive values are correlated - What to watch: LCG often shows higher correlation

Visual Pattern Analysis

Distribution Histogram - Should be roughly flat across all bins - Peaks or valleys indicate bias - Good RNGs show uniform distribution

2D Pattern Plot - Plots pairs of consecutive random numbers - Should fill the square uniformly with no patterns - LCG warning: Will show visible diagonal lines (lattice structure)

Correlation Plot - Shows correlation at different lags - Should stay near zero for all lags - Spikes indicate problematic correlations

Practical Tips

Choosing an Algorithm: - Need speed? XorShift or Mulberry32 - Need quality? Mersenne Twister - Need simplicity? Math.random() - Learning/teaching? LCG (to see what can go wrong)

Red Flags: - Multiple statistical test failures - Visible patterns in 2D plot (except LCG, which always has them) - High serial correlation (|r| > 0.1) - Non-uniform histogram

Performance Considerations: - Speed differences matter for millions of random numbers - For typical web apps, quality matters more than speed - Mersenne Twister is fast enough for most purposes

Reproducibility: - Only seeded algorithms (LCG, Mersenne, XorShift, Mulberry32) are reproducible - Math.random() cannot be seeded in standard JavaScript - Use seeded RNG when you need to reproduce results

Common Questions

Q: Why does LCG fail so many tests? A: LCG is known to have poor statistical properties. It’s included for educational purposes to show what a poor RNG looks like.

Q: Which should I use for my project? A: Mersenne Twister for general use, XorShift for speed-critical applications. Avoid LCG for anything serious.

Q: Are any of these cryptographically secure? A: No! None of these are suitable for cryptography. Use crypto.getRandomValues() for security applications.

Q: Why do results vary each run? A: Each test uses a different seed. Run multiple times to ensure consistency in pass/fail patterns.

Advanced Usage

Benchmarking: - Compare generation speed across algorithms - Note that speed can vary by browser and device - Consider both speed and quality for your use case

Custom Testing: - Adjust sample size to see how it affects test results - Try different seeds to check consistency - Combine visual and statistical analysis for comprehensive evaluation