Skip to main content

A simple coin flip random number generator

Project description

mitchallen.coin

PyPI version Python versions License Downloads

A simple, lightweight Python package that provides a random number generator perfect for simulations, games, and probabilistic applications.

Installation

pip install mitchallen-coin

Quick Start

from mitchallen.coin import flip, heads, tails

# Get a random number between 0.0 and 1.0
result = flip()
print(result)  # e.g., 0.7234567890123456

# Get a boolean coin flip result
is_heads = heads()
print(is_heads)  # True or False

# Get the opposite boolean coin flip result
is_tails = tails()
print(is_tails)  # True or False

Usage Examples

Simulate a Coin Flip

from mitchallen.coin import heads, tails

# Using heads()
if heads():
    print("Heads")
else:
    print("Tails")

# Or using tails()
if tails():
    print("Tails")
else:
    print("Heads")

Random Probability Events

from mitchallen.coin import flip

# 30% chance of rain
if flip() < 0.3:
    print("Bring an umbrella!")
else:
    print("No rain expected")

Monte Carlo Simulation

from mitchallen.coin import flip

# Estimate pi using Monte Carlo method
inside_circle = 0
total_points = 100000

for _ in range(total_points):
    x = flip()
    y = flip()
    if x*x + y*y <= 1:
        inside_circle += 1

pi_estimate = 4 * inside_circle / total_points
print(f"Pi estimate: {pi_estimate}")

Random Selection with Weighted Probabilities

from mitchallen.coin import flip

def choose_outcome(probabilities):
    """Choose an outcome based on probability distribution."""
    value = flip()
    cumulative = 0
    for outcome, prob in probabilities.items():
        cumulative += prob
        if value < cumulative:
            return outcome
    return list(probabilities.keys())[-1]

# Game loot drop system
loot = {
    "common": 0.5,
    "rare": 0.3,
    "epic": 0.15,
    "legendary": 0.05
}

result = choose_outcome(loot)
print(f"You got: {result}")

API Reference

flip()

Returns a random floating-point number in the range [0.0, 1.0).

Returns:

  • float: A random decimal number from 0.0 (inclusive) to 1.0 (exclusive)

Example:

from mitchallen.coin import flip

value = flip()
assert 0.0 <= value < 1.0

heads()

Returns True if flip() > 0.5, False otherwise. Useful for simple boolean coin flip simulations.

Returns:

  • bool: True if the coin flip result is greater than 0.5, False otherwise

Example:

from mitchallen.coin import heads

result = heads()
if result:
    print("Heads!")
else:
    print("Tails!")

tails()

Returns the opposite boolean value of heads(). Returns True if heads() would return False, and False if heads() would return True.

Returns:

  • bool: The opposite of what heads() would return

Example:

from mitchallen.coin import tails

result = tails()
if result:
    print("Tails!")
else:
    print("Heads!")

Why mitchallen.coin?

  • Simple: Clean API with intuitive functions
  • Lightweight: No dependencies
  • Type-safe: Full type annotations with mypy type checking
  • Quality: Enforced code quality with Ruff linting and formatting
  • Tested: Comprehensive test suite ensuring quality and reliability
  • Namespace package: Works alongside other mitchallen packages

Contributing

Contributions are welcome! See CONTRIBUTING.md for development setup and guidelines.

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mitchallen_coin-0.1.7.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mitchallen_coin-0.1.7-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file mitchallen_coin-0.1.7.tar.gz.

File metadata

  • Download URL: mitchallen_coin-0.1.7.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mitchallen_coin-0.1.7.tar.gz
Algorithm Hash digest
SHA256 df436630126221997358c2629b36d924730c7e225bd3a9ff1a61f1684c99f48f
MD5 522dc577d0975f6cf385b12e8863616f
BLAKE2b-256 c83b9308a72bde1824bf8b7a5d65eaffe34275b54911d88a4a1d87e854ecf5a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for mitchallen_coin-0.1.7.tar.gz:

Publisher: publish.yml on mitchallen/python-coin-flip

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mitchallen_coin-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for mitchallen_coin-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4fb39bf7d7b971fb268ea5d1b3eece90342d4f0224f3c622c014b4da52dca5a4
MD5 3b07ffdf0f9072a4f0e61a6d19da7114
BLAKE2b-256 94fcde7f5a4f2eee7fd3bddff9705204df662e21f193c061d889410cee462147

See more details on using hashes here.

Provenance

The following attestation bundles were made for mitchallen_coin-0.1.7-py3-none-any.whl:

Publisher: publish.yml on mitchallen/python-coin-flip

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page