Skip to main content

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

Mastodon Rel

Mastodon

Mastodon{rel=me}

Mastodon{:rel="me"}

Metadata

Release files for xkcd-random-four 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xkcd-random-four 0.1.1
File Size Uploaded
xkcd_random_four-0.1.1.tar.gz 18.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xkcd-random-four 0.1.1
File Interpreter ABI Platform
xkcd_random_four-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.3 kB

Release files / xkcd_random_four-0.1.1.tar.gz

Download URL xkcd_random_four-0.1.1.tar.gz
Size 18.3 kB
Tags Source
SHA-256 checksum
How to use checksums
573e4e15b1de4b1e0b7a8ef2003c64a5a72ccc12d9b89143502a52c2441b094e
BLAKE2b-256 checksum
How to use checksums
873589927620a0e91c92a3c111c0724e44d0daee847f631d7f8e968693a376d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 13, 2025.

Transparency log

Release files / xkcd_random_four-0.1.1-py3-none-any.whl

Download URL xkcd_random_four-0.1.1-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83f53d0aed96e033f69f5ee373271d0155a7f8843ecbb1ae9abcc4a445686b7c
BLAKE2b-256 checksum
How to use checksums
ee949b398a2b979ffdf43fabdc9e62009cc98039c770d6c03a6a62dba2c9b971
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 13, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page