Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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**

---