This project is a Python-based probability calculator developed as part of the freeCodeCamp Scientific Computing with Python curriculum, specifically for the "Probability Calculator" project. It implements a Hat class to simulate drawing colored balls from a hat and an experiment function to estimate the probability of drawing specific combinations of balls. The project demonstrates key concepts such as object-oriented programming, random sampling, and Monte Carlo simulations, aligning with freeCodeCamp's focus on practical coding skills.
- Hat Class:
- Initialize a hat with a specified number of colored balls (e.g.,
blue=5, red=4). - Draw a specified number of balls without replacement, removing them from the hat.
- Handle cases where the draw request exceeds available balls.
- Initialize a hat with a specified number of colored balls (e.g.,
- Experiment Function:
- Perform Monte Carlo simulations to estimate the probability of drawing a specified combination of balls.
- Use deep copying to ensure each experiment starts with a fresh hat.
- Calculate success based on meeting or exceeding expected ball counts.
- Random Sampling: Utilizes Python's
random.samplefor unbiased draws without replacement.
- Ensure Python 3.x is installed on your system.
- Clone this repository:
git clone https://github.com/Xrowly/probability-calculator.git - Navigate to the project directory:
cd probability-calculator - The project requires the
randomandcopymodules, which are part of Python's standard library. No external dependencies are needed.
- Run the main script to see example usage:
python main.py - The
main.pyscript demonstrates creating aHat, performing draws, and calculating probabilities for specific outcomes.
Running main.py might produce output like:
Hat contents: ['blue', 'blue', 'blue', 'blue', 'blue', 'red', 'red', 'red', 'red', 'green', 'green'] Example draw (4 balls): ['red', 'blue', 'green', 'blue'] Probability of drawing at least 1 red and 2 green (2000 experiments): 0.1235 Note: The probability value will vary due to the random nature of the simulation.
Modify main.py to create custom hats or experiments. Example:
from probability_calculator import Hat, experiment hat = Hat(blue=3, red=2, yellow=5) probability = experiment( hat=hat, expected_balls={"blue": 1, "yellow": 2}, num_balls_drawn=4, num_experiments=1000 ) print(f"Probability: {probability}")- probability_calculator.py: Defines the
Hatclass andexperimentfunction, implementing the core logic for the probability calculations. - main.py: Demonstrates usage with sample hats, draws, and probability experiments, showcasing the functionality required by the freeCodeCamp project.
- The application is console-based and does not support persistent storage or graphical visualization.
- Probability estimates depend on the number of experiments; fewer experiments may lead to less accurate results.
- Draws are performed without replacement, as per the project requirements.
Contributions are welcome! Please fork the repository and submit pull requests that align with the freeCodeCamp Scientific Computing with Python curriculum goals. Ensure changes maintain the project's educational focus.
This project is licensed under the MIT License. See the LICENSE file for details.
This project was completed as part of the freeCodeCamp Scientific Computing with Python curriculum, which emphasizes practical programming skills through hands-on projects. Visit freeCodeCamp.org for more details on the curriculum and other learning resources.