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.
(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 |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
575ab755d914b332f4e310816620787146af22a92ed0295db5b2556fbd7be79c
|
|
| MD5 |
c9d3a320b95e0cdc635611b7d2eba3ff
|
|
| BLAKE2b-256 |
496bab62e9af5696c4edcd24c8fcdb1443c3063641b04c37e4785f50548601cc
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xkcd_random_four-0.1.0.tar.gz -
Subject digest:
575ab755d914b332f4e310816620787146af22a92ed0295db5b2556fbd7be79c - Sigstore transparency entry: 584497454
- Sigstore integration time:
-
Permalink:
matthewdeanmartin/xkcd_random@c568a509e327f1781033a810d94974b453773508 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/matthewdeanmartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@c568a509e327f1781033a810d94974b453773508 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file xkcd_random_four-0.1.0-py3-none-any.whl.
File metadata
- Download URL: xkcd_random_four-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5eea4f345675889fa7d3e478c36cefe6832a1143cc71f5be5382a406c3a43295
|
|
| MD5 |
35889101615dd877b51eaeb2c55dd0e4
|
|
| BLAKE2b-256 |
77a2c7cd395a289d02b864e680ccad6941d0499f824a356ba07f22678c63e637
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xkcd_random_four-0.1.0-py3-none-any.whl -
Subject digest:
5eea4f345675889fa7d3e478c36cefe6832a1143cc71f5be5382a406c3a43295 - Sigstore transparency entry: 584497455
- Sigstore integration time:
-
Permalink:
matthewdeanmartin/xkcd_random@c568a509e327f1781033a810d94974b453773508 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/matthewdeanmartin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@c568a509e327f1781033a810d94974b453773508 -
Trigger Event:
workflow_dispatch
-
Statement type: