Fast and chaotic random number generator based on butterfly effect
Project description
🦋 butrand
butrand là một thư viện Python nhỏ gọn giúp sinh số ngẫu nhiên (PRNG) với tính hỗn loạn cao, dựa trên hiệu ứng cánh bướm (butterfly effect) và logistic map, kết hợp SHA‑256 để tăng cường entropy.
🚀 Giới thiệu
Trình sinh số ngẫu nhiên tiêu chuẩn của Python (random) đã rất mạnh mẽ, nhưng đôi khi ta muốn một PRNG:
- Độ nhạy cao với seed (một thay đổi rất nhỏ của seed dẫn đến chuỗi kết quả hoàn toàn khác biệt).
- Phân phối xấp xỉ đều nhưng dựa trên lý thuyết hỗn loạn.
- Dễ tái hiện (deterministic nếu đặt seed) để phục vụ mô phỏng.
butrand đáp ứng các yêu cầu này bằng cách:
- Sinh hai chuỗi hỗn loạn
xvàysong song qua logistic map. - Trộn hai giá trị (
x + y + x*y) để mở rộng dãy. - Băm kết quả bằng SHA‑256 và lấy 64 bit đầu (16 hex digit) thành số nguyên.
- Quy đổi về float hoặc integer tùy hàm
random()hoặcrandint(a,b).
🧠 Phân tích chi tiết thuật toán
1. Logistic map
Phương trình logistic map:
xₙ₊₁ = r · xₙ · (1 − xₙ)
- Với r≈3.99, hệ bước vào vùng hỗn loạn, cực kỳ nhạy với giá trị ban đầu
x₀. - Dòng tương tự cho yₙ.
2. Trộn và băm
- Tại mỗi bước:
- Cập nhật x, y qua
_logistic_step(). - Tính
mixed = (x + y + x*y) mod 1. - Băm SHA‑256 của chuỗi
f"{mixed:.16f}{counter}". - Lấy 16 ký tự hex đầu → 64 bits →
int_val. random() = int_val / 0xFFFFFFFFFFFFFFFF∈ [0,1).
- Cập nhật x, y qua
Điều này giúp:
- Giảm bias: trộn hai chuỗi.
- Tăng entropy: SHA‑256 phá vỡ mối quan hệ tuyến tính.
- Tái hiện: với cùng seed & counter, chuỗi giống hệt.
📦 Cài đặt
pip install butrand
Lưu ý:
butrandyêu cầu Python 3.7 trở lên.
git clone https://github.com/OmeraGod/butrand.git cd butrand pip install -e .
🧪 Hướng dẫn sử dụng
1. Import thư viện:
from butrand import BetterRand
# Với seed cố định, giúp kết quả luôn lặp lại
rng = BetterRand(seed=2025)
- Nếu không truyền seed, BetterRand() sẽ tự động lấy thời gian hiện tại (ms).
2. random()
# Trả về float trong [0,1)
for _ in range(5):
print(rng.random())
Giải thích:
- Mỗi lần gọi, x và y được cập nhật qua logistic map, trộn & băm → giá trị mới cực kỳ hỗn loạn.
3. randint(a, b)
# Trả về integer trong [a,b]
print([rng.randint(10, 20) for _ in range(5)])
Giải thích:
- randint tận dụng random() để phân phối đồng đều trên khoảng [a, b].
4. Ví dụ đầy đủ
from butrand import BetterRand
rng = BetterRand(seed=12345)
print("Floats:", [f"{rng.random():.6f}" for _ in range(5)])
print("Ints 1–10:", [rng.randint(1, 10) for _ in range(5)])
Ví dụ đầu ra (mỗi lần seed=12345):
Floats: ['0.102632', '0.982467', '0.985985', '0.004041', '0.024338']
Ints 1–10: [2, 10, 10, 1, 2]
📈 Ứng dụng
- Mô phỏng: cần chuỗi ngẫu nhiên có tính hỗn loạn cao.
- Game: sinh vật, bản đồ, sự kiện bất ngờ.
- Nghiên cứu: thử nghiệm PRNG mới, phân tích hỗn loạn.
🔐 License
Phân phối theo giấy phép MIT. Xem chi tiết trong file LICENSE.
👨💻 Tác giả
Bùi Phong Phú — Developer
📧 omerasutvailworkit@gmail.com
💻 GitHub – OmeraGod
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 butrand-0.0.1.tar.gz.
File metadata
- Download URL: butrand-0.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5391ae12ef56c667199d6e40d290640be8f5f92a1a532a913a11253681ef64c6
|
|
| MD5 |
24e6d9da9ab2e0c1116203d32a345310
|
|
| BLAKE2b-256 |
6ef42d2cab79e917b11394ad52cfc7053ff6a5b8086dc6ed3f459e00abfed6c0
|
File details
Details for the file butrand-0.0.1-py3-none-any.whl.
File metadata
- Download URL: butrand-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c0cd12a0524ed9b511120e44c50c4dd65fe87655444ee860a1b0e5a69a756a1
|
|
| MD5 |
01703304a430ca7b8fed03743ab3d2fd
|
|
| BLAKE2b-256 |
e4c094c751829085af65644402b6b4c3fe358d53b1967d325c390958007424c2
|