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.
(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 | License | ||
| Coverage | PyPI | ||
| Lint / Pre-commit | Python Versions | ||
| Quality Gate | Docs | ||
| CI Build | Downloads | ||
| Maintainability | Last Commit |
| Category | Health |
|---|---|
| Open Issues | |
| Stars |
Mastodon Rel
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)
| File | Size | Uploaded | |
|---|---|---|---|
| xkcd_random_four-0.1.1.tar.gz | 18.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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 logRelease 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