Skip to main content

一个基于世界文化不吉利数字过滤的随机数生成器

Project description

🍀 luckyrandom

让你的随机数“吉利”起来!

luckyrandom 是基于 Python 标准库 random 改造的随机数生成器,能够自动避开世界各地文化、宗教中的不吉利数字(如 4、13、666 等)。API 完全兼容 random,你几乎不需要改变任何代码习惯。


📦 安装

pip install luckyrandom

🚀 快速开始

import luckyrandom as lucky

# 默认使用全球预设:过滤含4的数字、13、666
print(lucky.randint(1, 100))   # 永远不会有 4,13,14,24,40,666 ...

# 切换为中国预设(只过滤数值 4 和 13)
lucky.set_preset("china_strict")
print(lucky.randint(1, 50))

# 自定义不吉利数字集合
lucky.set_bad_numbers({4, 13, 17, 666})

# 创建独立的生成器实例,使用日本预设
jp_random = lucky.LuckyRandom.from_preset("japan")
print(jp_random.randint(1, 100))

🌍 所有可用预设

预设名称 说明
none 不过滤任何数字
default 过滤 {4, 13}
china 过滤所有包含 4 的数字,以及 13, 666
china_strict 仅过滤 {4, 13}
japan 过滤含 49 的数字,以及 13, 42, 49
korea 过滤所有包含 4 的数字
west 过滤 {13, 666}
italy 过滤 {13, 17, 666}
christianity 过滤 {13, 666}
afghanistan 过滤 {13, 39}
turkey 过滤 13
brazil 过滤 13
india 过滤 13
thailand 无特殊过滤
russia 过滤 {13, 666}
philippines 过滤 13
spain 过滤 13
greece 过滤 13
global 最严格:过滤含 4 数字 + 13, 666

💡 直接调用 lucky.set_preset("name") 即可在运行时切换文化背景。


🎛️ 高级用法

自定义任意规则

你可以传入一个 函数 来判断数字是否不吉利:

import luckyrandom as lucky

def my_rule(x: int) -> bool:
    return x % 10 == 4  # 结尾是4的数字不吉利

r = lucky.LuckyRandom(bad_rule=my_rule, seed=42)
print(r.randint(1, 100))

对序列操作过滤

序列函数(choice, choices, sample)支持 exclude_bad=True 参数,自动移除不吉利元素:

import luckyrandom as lucky

nums = [1, 4, 8, 13, 42, 100]
print(lucky.choice(nums, exclude_bad=True))   # 永远不会返回 4 或 13

动态修改规则

r = lucky.LuckyRandom()
r.set_bad_numbers({14, 24})           # 替换整组不吉利数字
r.add_bad_numbers(666)                # 追加(仅当规则为集合时有效)
r.clear_bad_numbers()                 # 清空所有过滤

📚 API 参考

luckyrandom 完整实现了 random.Random 的所有方法。所有生成整数的函数 都会自动应用不吉利数字过滤:

方法 说明
randint(a, b) 返回 [a, b] 区间内的合法随机整数
randrange(start, stop, step) 返回 range(start, stop, step) 中的合法整数
getrandbits(k) 返回一个 k 位的合法随机非负整数

序列方法增强:

方法 说明
choice(seq, *, exclude_bad=False) 随机选取元素,可选过滤
choices(pop, weights, *, exclude_bad=False) 带权重选择,可选过滤
sample(pop, k, *, exclude_bad=False) 无放回抽样,可选过滤

其余分布函数(uniformgauss 等)完全继承原行为,不受影响。


⚙️ 设计原理

  • 拒绝采样 + 候选列表混合:小范围直接生成候选列表保证效率,大范围优先使用拒绝采样并配有安全回退。
  • 线程安全:每个实例独立持有过滤规则,互不干扰。
  • 完全向后兼容:你可以用 luckyrandom.randint 直接替换 random.randint,原有代码无需修改。

📄 许可

MIT License © 2026


🔗 相关链接


现在,让每一次随机都充满“好运” 🎲✨

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

fortunerandom-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

fortunerandom-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fortunerandom-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for fortunerandom-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ebeea8db0c789bed2cee7e0cdccfcb2d8d0e020fe58e5f2d8004f2b1eb567300
MD5 5e98850a95fe92566a911515834f32a8
BLAKE2b-256 ac9f04b81c59a1dd04643314506338b45ee7b6c02207271c5aea97303753f5f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fortunerandom-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for fortunerandom-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2037ad10983f97a3e0d2f3627a48bcb6bfd2cfc001662a8147d64bdb361aeaa
MD5 6866d35e23834c54ff08300167630630
BLAKE2b-256 8c50904332b85b84c74c9f6a8c22df80fbaaede3ebcdd868c80226d5682b4992

See more details on using hashes here.

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