This is a simple C++ library for Pseudo Random Number Generation. It provides a functionality similar to that of the python standard library random.
- Ease of Use: Straightforward API for quick integration into your C++ projects.
- Versatility: Supports different PRNG algorithms, allowing you to choose based on your requirements.
- Convenience: Provides various standard distributions used in scientific computation and also has rudimentary support for custom probability distributions.
- To be decided
Pseudorandom Number Generators
- Mersenne Twister (MT)
- Linear Feedback Shift Register (LFSR)
- XOR-Shift (XOR)
- Blum Blum Shub (BBS)
- Naor-Reingold (NR)
Distrbutions
- Cauchy
- Exponential
- Gaussian
- Maxwell
- Weibull
- Bernoulli
- Binomial
- Gibbs
- Hypergeometric
- Negative-Hypergeometric
- Poisson
Time taken for generating 100000000 random numbers by DiceForge's PRNGs:
Generator | Time Taken |
---|---|
MT32 | 1780.44 ms |
MT64 | 1779.17 ms |
XORShift32 | 445.931 ms |
XORShift64 | 476.061 ms |
LFSR32 | 10777.7 ms |
LFSR64 | 19091.7 ms |
BBS32 | 50130.6 ms |
BBS64 | 97033.5 ms |
NaorReingold | 210082 ms |
For comparison, benchmarking other existing standard libraries for the same test.
Generator | Time Taken |
---|---|
C++'s Mersenne Twister | 4032.7 ms |
C rand() function | 1388.03 ms |
python's random | 175175.89 ms (~ 3min) |
numpy's randint | 165600.48 ms (~ 3min) |
The various PRNGs in DiceForge are robust and pass various Dieharder tests:
Generator | Tests Passed | Tests Failed |
---|---|---|
MT32 | 29 (4 weak) | 1 |
MT64 | 29 (1 weak) | 1 |
XORShift32 | 29 | 1 |
XORShift64 | 29 (1 weak) | 1 |
BBS32 | 29 (2 weak) | 1 |
BBS64 | 29 (2 weak) | 1 |
LFSR32 | 29 (2 weak) | 1 |
LFSR64 | 29 | 1 |
Naor Reingold | 19 | 11 |
For more information about the Dieharder tests, check out DiceForge's Documentation.
Check out the Documentation for detailed information on library usage, supported algorithms, and more!
- C++ compiler (minimum C++17 or later, note that C++20 is required for using 2D probability functions)
- CMake (>=3.28) [If building library]
- Download the compiled binaries from out folder along with the include files include
- If you want compact compilation commands later on, add the path to include/ to the environment variable PATH
- Start generating pseudo-random numbers!
- Clone the repository:
git clone https://github.com/yourusername/diceforge-library-clone.git
- Create the build folder by using the CMakeLists.txt
- Build library using the given CMake configurations (
cmake --build <build-folder>
) - Currently DiceForge supports g++, clang, mingw and their variants. It does not support msvc.
- Install the library to usr/local/ (
cmake --install <build-folder>
) - Use it without installation by following the steps mentioned in Using a prebuilt library
Here's a quick example to get you started:
example.cpp
#include "diceforge.h"
int main() {
// Create a PRNG object
int seed = 123;
DiceForge::XORShift32 prng = DiceForge::XORShift32(seed);
// Generate and print a random number using the prng
std::cout << "Random number: " << prng.next() << std::endl;
// Generate and print a random number using DiceForge's default prng
std::cout << "One more random number: " << DiceForge::Random.next() << std::endl;
return 0;
}
Now to compile it with a compiler of your choice (g++ has been used in the following examples), enter one of the following commands in your terminal:
- If DiceForge has been installed (It is assumed that g++ has been configured to have access and to search usr/local for the installed libraries):
g++ example.cpp -ldiceforge -o example.out
- If DiceForge has not been installed but the library has been included in PATH :
g++ example.cpp libdiceforge.a -o example.out
where libdiceforge.a is the built static library
- If DiceForge has not been installed and the library has not been included in PATH :
g++ example.cpp libdiceforge.a -I "<include>" -o example.out
where <include> is the path to the folder corresponding to include and libdiceforge.a is the built static library
Feel free to explore the library and experiment with different algorithms!
Currently, this is an IIT-M (Indian Institute of Technology Madras) Math Club exclusive project and we are not accepting outside contributions.
-
View the Tasks folder to view the tasks to be done.
-
Reference materials are provided in the Contributors_Only folder.