Skip to main content

純粋な偶然を与えるガチャアルゴリズム — Rust + PyO3 実装

Project description

pure-gacha

純粋な偶然を与える

Rust 製の高速なガチャアルゴリズムを Python から使えるライブラリです。

from pure_gacha import Gacha

gacha = Gacha()
gacha.pull(destiny=42)  # → 0.0〜1.0 の float

設計哲学

偶然性は 結果が良くて嬉しいのではなく、偶然性によって得られた結果だから嬉しい

  • 実装者が確率を操作する手段はアーキテクチャ上存在しない
  • 数式構造そのものがプルごとにランダムに組み変わる
  • 品質のばらつきは設計上の意図(バグではなく仕様)

ゆえに本ライブラリには「レアリティを上げる」「排出率を指定する」といった API は存在しません。実装者に与えられるのは destiny(運命)という整数を 1 つ渡す ことだけです。


インストール

pip install pure-gacha
  • Python 3.9 以降をサポート
  • Linux / macOS / Windows の wheel を配布

使い方

基本

from pure_gacha import Gacha

gacha = Gacha()
value: float = gacha.pull(destiny=42)
  • コンストラクタ Gacha() は引数を取らない
  • pull(destiny: int) -> float整数 1 つ を渡す
  • 返り値は 0.0 <= x <= 1.0float
  • 同じ destiny でも毎回異なる値が返る(OS エントロピーが混ぜられているため)

値の性質

性質 内容
範囲 0.0 <= x <= 1.0
分布 ほぼ一様(1000 回プルで各 0.1 幅のバケットが 70〜130 個に収まる基準)
再現性 なし — 同じ destiny でも毎回異なる
並列安全性 スレッドセーフ(OsRng 由来)

なぜ destiny は整数 1 つなの?

destiny(運命)はプルの種になる概念的な整数です。ユーザー ID、タイムスタンプ、イベント ID など、そのプルを一意に意味づける値を好きに入れてください。

ただし、destiny の値で結果を操作することはできません。Stage 1 で OS エントロピー 8 バイトと混ぜてハッシュ化されるため、destiny は単にハッシュの種の一部として機能します。


アルゴリズム概要

destiny: int(実装者が渡す整数 1 つ)
    │
    ▼
【Stage 1】SHA256(destiny_str + os_entropy_8bytes) → [u8; 32]
    │
    ▼
【Stage 2】h[31] が残り 29 バイトの読み方を決める自己参照的バイト分配
    │
    ▼
【Stage 3】二分木による数式構築・実行
    - 深さ 2〜4 の二分木をハッシュから組み立てる
    - 二項演算子 25 種 + 単項演算子 25 種 = 計 50 種からランダム選択
    │
    ▼
【Stage 4】SHA256(raw + h[0..8]) → u32 / u32::MAX → float (0.0〜1.0)

詳細仕様は AGENTS.md を参照。


ライセンス

未定(OSS として公開予定)。



コントリビューター向け

以下はこのリポジトリで開発する方向けの情報です。ライブラリを利用するだけの方は読む必要はありません。

プロジェクト構成

pure-gacha/
├── AGENTS.md              # 実装ガイド(= CLAUDE.md のシンボリックリンク元)
├── README.md              # このファイル
├── LICENSE-MIT / LICENSE-APACHE
├── pyproject.toml         # maturin プロジェクト定義(PyPI パッケージ)
├── Cargo.toml             # Rust workspace 定義
├── Docker/
│   └── Dockerfile         # Rust + Python + maturin の開発イメージ
├── docker-compose.yml     # dev サービス定義(キャッシュボリューム込み)
├── prototype/
│   └── prototype_v1.py    # Python リファレンス実装
├── gacha-core/            # Rust クレート本体
└── gacha-py/              # PyO3 バインディング crate(cdylib)

開発環境セットアップ

前提: Docker / Docker Compose が使えること。

1. イメージのビルド

docker compose build

2. 開発コンテナに入る

docker compose run --rm dev

コンテナ内では次がそのまま使えます。

ツール バージョン
rustc / cargo 1.82
Python 3.11
maturin 1.13
pytest 9.x
rustfmt / clippy

ホストの .//workspace にマウントされ、cargo レジストリと target/ は名前付きボリュームに分離されているためリビルドは高速です。

3. Python プロトタイプの実行

docker compose run --rm dev python3 prototype/prototype_v1.py

4. Rust 実装のビルド・テスト

docker compose run --rm dev cargo test
docker compose run --rm dev bash -lc 'maturin develop && pytest'

実装上の制約(厳守)

  1. os.urandom(8) 相当を必ず使う(Rust では OsRng)。同じ destiny でも毎回違う結果が出得る性質を壊さないこと。
  2. 非有限値(NaN / Inf)は必ず default_val にフォールバックする。
  3. 演算子の順序・インデックスを変えない(byte >> 1) % 25 の対応は固定。
  4. destiny と struct_bytes[0..4] を結合して SHA256 に掛けるロジックを変えない。独自性の核心部分。
  5. 演算子をユーザーに選ばせない。50 種は内部に固定。
  6. 公開 API は Gacha() / pull(destiny) のみ。むやみに拡張しない。

詳細は AGENTS.md を参照してください。

ステータス

  • Python プロトタイプ (prototype/prototype_v1.py)
  • Docker 開発環境
  • gacha-core Rust クレート
  • gacha-py PyO3 バインディング
  • 分布テスト・Invariant テスト
  • PyPI 公開

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

pure_gacha-0.1.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distributions

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

pure_gacha-0.1.0-cp39-abi3-win_amd64.whl (166.1 kB view details)

Uploaded CPython 3.9+Windows x86-64

pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (264.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (255.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

pure_gacha-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (231.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

pure_gacha-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl (239.7 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pure_gacha-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e05ab5acb337d196eaedbeb6cfaa644ac5765ec2885338e71b313c22b529cfc9
MD5 012215d0424440ffb343a27e5948d92b
BLAKE2b-256 aa35843803473a41aa453f135c2240bb3c0cc058e2baf636d65edda7813895b0

See more details on using hashes here.

Provenance

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

Publisher: release.yaml on Gachal29/pure-gacha

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

File details

Details for the file pure_gacha-0.1.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: pure_gacha-0.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 166.1 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pure_gacha-0.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e68473ccdf9bc41551968e6618b33c2c3bbbfe485482f66d0c8b5b5593ab6e0b
MD5 3aa1ec86ac51943e8925aa3b1a9eb7c2
BLAKE2b-256 9aa53556e90f812b154281baebb725f512930d0c5fdda8c1b0c49ef17cc112a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pure_gacha-0.1.0-cp39-abi3-win_amd64.whl:

Publisher: release.yaml on Gachal29/pure-gacha

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

File details

Details for the file pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1bd1590ca1d1c62c4f382b962d7d86431acd7fd3f79e086542747003aa97558a
MD5 d5793ebb91c823629c8d40a500bf3685
BLAKE2b-256 058a99e7b695b3fc2d4c5958df4c6479ec9f075e9a07e701a3f13e696c638c7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yaml on Gachal29/pure-gacha

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

File details

Details for the file pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a398e694cd8bee3c93bc18893b1dd8e0df54d215ac238c9078b1ffb43eeaf81f
MD5 28f1eb18442e4e993c6d900a7921c4b5
BLAKE2b-256 72e453af85eb7034c9a2f89457fa65a934ac096e24cc8943246510e5dade5907

See more details on using hashes here.

Provenance

The following attestation bundles were made for pure_gacha-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yaml on Gachal29/pure-gacha

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

File details

Details for the file pure_gacha-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pure_gacha-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c66945c6729d8802c0fcffdb676c6cd31ca3f1cf087cdf3eee509df5e6cc7708
MD5 fc4d18b8f9a0740af9525cf6ee0f0a3a
BLAKE2b-256 923d31b064a826046a9dadccf4f4025efb817f84fd231fe9b6898c1fc354ac62

See more details on using hashes here.

Provenance

The following attestation bundles were made for pure_gacha-0.1.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yaml on Gachal29/pure-gacha

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

File details

Details for the file pure_gacha-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pure_gacha-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 dd500b72d873c84a25ba36c84265e3791abe7df4819642c1e6342a62545379b7
MD5 104638df2b8c3a45c6690e038b2fe972
BLAKE2b-256 c1d90905ee2330cc9180668191fde59c717ef2a394269a550287132a70f16fa1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pure_gacha-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yaml on Gachal29/pure-gacha

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