Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shreyasmehta05/prng
A Python-based pseudo-random number generator and Monte Carlo simulation for estimating ฯ with visualization.
https://github.com/shreyasmehta05/prng
Last synced: about 1 month ago
JSON representation
A Python-based pseudo-random number generator and Monte Carlo simulation for estimating ฯ with visualization.
- Host: GitHub
- URL: https://github.com/shreyasmehta05/prng
- Owner: shreyasMehta05
- Created: 2024-12-08T09:03:03.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-12-08T09:20:20.000Z (about 2 months ago)
- Last Synced: 2024-12-08T10:20:46.317Z (about 2 months ago)
- Language: Python
- Homepage:
- Size: 623 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# **PRNG** - A Pseudo-Random Number Generator in Python ๐งฎโจ
![Waketime](https://img.shields.io/badge/Waketime-5%20hrs%2002%20minutes-blueviolet?style=plastic)This project implements a **Pseudo-Random Number Generator (PRNG)** in Python3. It uses a middle-square method to generate pseudo-random numbers and includes a Monte Carlo simulation for estimating ฯ. The results are visualized using `matplotlib`.
- This Project was part of the course `Introduction to Software Systems` at **IIIT Hyderabad**.
---## **๐ Project Overview**
### **โจ Main Features**
- **๐ ๏ธ Custom Pseudo-Random Number Generator:**
A deterministic approach to generating pseudo-random numbers with user-defined seed and iterations.
- **๐ Monte Carlo Simulation for ฯ Estimation:**
Uses generated pseudo-random numbers to estimate the value of ฯ.
- **๐ Visualization:**
Frequency distribution of random numbers and error analysis in ฯ estimation.---
## **๐ Directory Structure**
```
PRNG/
โโโ estimation_of_pie.png # ๐ Graph: Error in ฯ estimation
โโโ estimation_of_pie.py # ๐งฎ Monte Carlo ฯ estimation script
โโโ estimation_of_pi_improved.png # ๐ Improved graph: Error in ฯ estimation
โโโ Final_results.png # ๐ Final Estimation Result
โโโ pseudo_random_generator.py # ๐ฒ PRNG implementation
โโโ README.md # ๐ Documentation file
โโโ __pycache__/ # โ๏ธ Compiled Python files
```---
## **๐ How to Run**
### **1๏ธโฃ Clone the Repository**
```bash
git clone https://github.com/shreyasMehta05/PRNG.git
cd PRNG
```### **2๏ธโฃ Install Required Libraries**
Ensure you have Python 3 installed. Install the dependencies using:
```bash
pip install matplotlib
```### **3๏ธโฃ Run the Pseudo-Random Generator**
Generate and visualize a frequency distribution of pseudo-random numbers:
```bash
python3 pseudo_random_generator.py
```### **4๏ธโฃ Estimate ฯ Using Monte Carlo Simulation**
Estimate the value of ฯ using the generated pseudo-random numbers:
```bash
python3 estimation_of_pie.py
```---
## **๐ฒ Pseudo-Random Generator (`pseudo_random_generator.py`)**
This script generates `k` pseudo-random numbers using the middle-square method.
### **โ๏ธ Usage**
- Input the **number of digits** for the seed.
- Input the **number of random numbers** to generate.### **๐ Key Functionality**
- **`pseudoRandNumGen(seed, k)`**:
Generates a list of `k` pseudo-random numbers based on the given seed.
- **๐ Visualization:**
Outputs a histogram of the random number distribution.---
## **๐ Monte Carlo Simulation for ฯ Estimation (`estimation_of_pie.py`)**
This script estimates the value of ฯ using the Monte Carlo method. It compares two approaches:
1. **Custom Pseudo-Random Number Generator** (`PRNG`).
2. **Pythonโs Built-in Random Library** (`random`).### **โจ Key Features**
- **๐ Monte Carlo Algorithm:**
Uses random points within a square to calculate ฯ.
- **๐ Visualization:**
- Error in ฯ estimation using custom PRNG and Python's `random`.
- Relative error comparison.---
## **๐ท Visualization Examples**
### **1๏ธโฃ Frequency Distribution of Pseudo-Random Numbers**
![Frequency Distribution](estimation_of_pie.png)
_Histogram showing the frequency distribution of generated random numbers._### **2๏ธโฃ Error in Estimation of ฯ**
![Error in ฯ Estimation](estimation_of_pi_improved.png)
_Comparison of errors using custom PRNG and Pythonโs `random`._---
## **๐ Functions Overview**
### **`pseudo_random_generator.py`**
- **`numberOfDigits(n)`**:
Returns the number of digits in a given number.
- **`pseudoRandNumGen(seed, k)`**:
Core function to generate pseudo-random numbers using the middle-square method.### **`estimation_of_pie.py`**
- **`generate(k)`**:
Generates two sets of pseudo-random numbers (`x` and `y`) for Monte Carlo simulation.
- **`inSquare(l)`**:
Maps generated points to a unit square.
- **`estimate_pi(l)`**:
Estimates ฯ using points within a circle.
- **`estimate_pi_using_random(l)`**:
Estimates ฯ using Pythonโs `random`.
- **`estimate_pi_error(l)`**:
Calculates the absolute error in ฯ estimation.---
## **๐ Results**
- **โ Closest Estimate to ฯ (Custom PRNG):**
Closest calculated value of ฯ: **`3.141`**
- **๐ Visualization of Errors:**
Demonstrates the effectiveness of the custom PRNG compared to Python's built-in `random` library.
- **๐ Final Results:**
![Final Results](Final_results.png)
---## **๐ฆ Dependencies**
- **๐ Python 3**
- **๐ `matplotlib`** (for visualizations)---
## **๐จโ๐ป Author**
- **Shreyas Mehta**---