Skip to content

MRHemmati/PRNG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Pseudo-Random Number Generator (PRNG)

This repository contains a C++ implementation of the ran2 pseudo-random number generator, based on Chapter 7 of Numerical Recipes in C. It includes uniform distribution tests, automated visualization using gnuplot, and a Python testing suite to verify correctness.

Overview

The ran2 algorithm is a long-period ($> 2 \times 10^{18}$) random number generator designed by L'Ecuyer. It incorporates a Bays-Durham shuffle and added safeguards to ensure high-quality pseudo-random numbers. It returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values).

Features

  • High-Quality PRNG: Implementation of the classic ran2 algorithm.
  • Helper Interface:
    • Randomize(): Dynamically seeds the generator using time(NULL).
    • Random(): Returns a uniform double in the range [0.0, 1.0).
    • Random(int N): Returns a uniform integer in the range [0, N).
  • Distribution Analysis: Automatically bins the generated sequence into 20 sub-intervals to test for uniform distribution.
  • Auto-plotting: Generates a GNUplot configuration (temp.txt) and exports a distribution plot (PRNG1.png).

Requirements

To compile and run the application, you need:

  1. A C++ compiler (e.g., g++ or clang++).
  2. gnuplot installed on your system (optional, required to generate the plot image).

Installing Gnuplot (Optional)

  • macOS (via Homebrew):
    brew install gnuplot
  • Ubuntu/Debian:
    sudo apt-get install gnuplot

Getting Started

1. Compile the Code

Compile the source code using the following shell command:

g++ -o PRNG.out PRNG.cpp -Ofast

2. Run the Program

Run the compiled binary:

./PRNG.out

3. Execution flow

  1. The program asks you to specify the number of random numbers to generate:
    chand adad random mikhayd? (yek adade integer vared konid)=
    
    (Enter an integer, e.g., 10000)
  2. It prints the generated random sequence.
  3. It prints the frequency distribution counts (p[i]) across 20 bins.
  4. It prints the normalized frequencies.
  5. It compiles a gnuplot script (temp.txt) and runs it to save the plot as PRNG1.png.

Automated Testing

An automated Python test script is provided in the repository to compile, run, and validate the C++ code's functionality and statistical behavior.

To run the tests:

python3 test_prng.py

What the test checks:

  • Successful compilation of PRNG.cpp.
  • Boundary check of all generated random numbers (must be in [0.0, 1.0)).
  • Integrity of the frequency binning (sum of bins must equal the total requested samples).
  • Verification of normalized frequencies.
  • Verification of output files (temp.txt and PRNG1.png).
  • Statistical sanity check (the mean of the generated uniform distribution must be close to 0.5).

License & Reference

This implementation is based on the ran2 routine from Numerical Recipes in C. Please refer to Numerical Recipes for their licensing terms.

About

random generator, PRNG

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors