Skip to main content

Drop in replacment for random that always returns 4 or 4/6

Project description

xkcd-random

Drop in replacement for random module that forces the core random number function to return 4, 0.4, 4/6. The specification is a bit ambiguous.

Uses the xkcd-221 algorithm.

Function Returns 4

tests pre-commit.ci status Downloads Python Version Release

(The name xkcd-random is too close to some pre-existing package, so it is now xkcd-random-four)

Installation

pip install xkcd-random

Usage

import xkcd_random as random

print(random.randint(0, 10))

Caveats

Some functions return something other than 4/6 or 4 because the random.random() function's results are manipulated before being returned to the user.

APIs might return 4 or 4/6, I don't know I haven't really tested it that well. You guys sure are picky about what random numbers you want. You know, here use this

import random

random.randint = lambda x: int(input("role a die"))

Implementation

SimpleOverrideXKCDRandom overrides 4 methods of Random() like the docstring suggests.

from random import Random


class SimpleOverrideXKCDRandom(Random):
    def random(self) -> float: return float(4 / 6)

    def getrandbits(self, k: int) -> int: return 4

    def seed(self, *_): ...

    def setstate(self, _): return ()

    def getstate(self): ...

xkcd_random is a fork of python 3.13. You can actually use this sensibly, with "system" (real randomness) or "xkcd" (it is 4.)

import xkcd_random

random = xkcd_random.Random(core=xkcd_random.SystemCore())

or

import os

os.environ["RANDOM_BACKEND"] = "system"
import xkcd_random as random

License

This code is copied directly from the python 3.11 so the license is the same as CPython's. Anything not covered by the cpython license is covered by MIT.

Prior Art and Similar Libraries

This list has an emphasis on "drop in replacements"

Project Health & Info

Metric Health Metric Info
Tests Tests License License
Coverage Codecov PyPI PyPI
Lint / Pre-commit pre-commit.ci status Python Versions Python Version
Quality Gate Quality Gate Status Docs Docs
CI Build Build Downloads Downloads
Maintainability Maintainability Rating Last Commit Last Commit
Category Health
Open Issues GitHub issues
Stars GitHub Repo stars

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

xkcd_random_four-0.1.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

xkcd_random_four-0.1.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file xkcd_random_four-0.1.0.tar.gz.

File metadata

  • Download URL: xkcd_random_four-0.1.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for xkcd_random_four-0.1.0.tar.gz
Algorithm Hash digest
SHA256 575ab755d914b332f4e310816620787146af22a92ed0295db5b2556fbd7be79c
MD5 c9d3a320b95e0cdc635611b7d2eba3ff
BLAKE2b-256 496bab62e9af5696c4edcd24c8fcdb1443c3063641b04c37e4785f50548601cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for xkcd_random_four-0.1.0.tar.gz:

Publisher: publish_to_pypi.yml on matthewdeanmartin/xkcd_random

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

File details

Details for the file xkcd_random_four-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for xkcd_random_four-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5eea4f345675889fa7d3e478c36cefe6832a1143cc71f5be5382a406c3a43295
MD5 35889101615dd877b51eaeb2c55dd0e4
BLAKE2b-256 77a2c7cd395a289d02b864e680ccad6941d0499f824a356ba07f22678c63e637

See more details on using hashes here.

Provenance

The following attestation bundles were made for xkcd_random_four-0.1.0-py3-none-any.whl:

Publisher: publish_to_pypi.yml on matthewdeanmartin/xkcd_random

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