Skip to main content

Pseudo-random number generators from across projects

Project description

🎲 libprng

Build Status Coverage Latest Version Python Versions Format License Status Types



Purpose

This package provides a set of pseudo-random number generators (PRNGs). These are not meant to be cryptologically secure, but rather enable the reproduction of "random" number sequences regardless of the platform that they are being generated on.

Creation of this package was inspired by capture-the-flag competitions where random number generation using glibc's rand function on a typical Windows or Linux target would not match those generated on a macOS device (macOS generally uses Clang rather than glibc). This package exists to fill that gap and enable reproduction regardless of platform.

Installation

This package is available on PyPI and can be installed using pip:

python3 -m pip install libprng

[!NOTE]
Your Python command may differ depending on how it was installed on your system. Some installations omit the version (python) while others are minor-version specific (ex. python3.9).

You may also install the latest development releases from GitHub:

python3 -m pip install git+https://github.com/Jayson-Fong/libprng
📦 Installing Using a Virtual Environment

In most cases, you should install this package within a Python virtual environment. This enables you to leverage multiple versions of this package across various projects on your system.

To create a virtual environment:

python3 -m venv .venv

You will then need to activate your virtual environment. On Linux or macOS:

source .venv/bin/activate

On Windows, the activation command depends on what interpreter you are using. For command prompt:

.\.venv\Scripts\activate.bat

For PowerShell:

.\.venv\Scripts\Activate.ps1

Or when using GitBash:

source .venv/Scripts/activate

You may then install using pip normally:

python3 -m pip install libprng

Usage

[!IMPORTANT]
This package is under active development, and the API may change at any time. If using this package in your project, consider pinning it to a specific version.

🐂 GNU C Library (glibc): Traditional Usage

Initialize the global PRNG random data instance with a seed and generate a single random 31-bit integer:

from libprng.glibc import srand, rand


srand(1234)
print(rand())

Create a PRNG object that is unaffected by other rand() calls:

from libprng.glibc import GlibcRandom


random_generator: GlibcRandom = GlibcRandom(1234)

# Request a single random integers
print(random_generator.generate())

# Loop through random numbers until one is even:
for random_int in random_generator:
    print(random_int)
    if random_int % 2 == 0:
        break
🐂 GNU C Library (glibc): Reentrant

Generate a random integer with a seed of 1234:

from libprng.glibc import rand_r


result, _next_seed = rand_r(1234)
print(result)

Generate a random integer with a seed of 1234 without having to update the seed yourself:

from libprng.glibc import rand_r, Seed


seed: Seed = Seed(1234)
print(rand_r(seed))
print(rand_r(seed))

Leverage rand_r as a generator:

from libprng.glibc import GlibcReentrantRandom


random_generator: GlibcReentrantRandom = GlibcReentrantRandom(1234)

# Request a single random integers
print(random_generator.generate())

# Loop through random numbers until one is even:
for random_int in random_generator:
    print(random_int)
    if random_int % 2 == 0:
        break

Security

Pseudo-random number generators are generally insufficient for cryptographic or security-sensitive use cases. Likewise, this package was designed for reproducibility—not security or cryptography. You should not use this package for security-sensitive use cases, such as password generation, encryption, or key derivation.

Backlog

This package is still under active development. The following features are anticipated:

  • Clang PRNG implementation
  • Command-line PRNG utility

License

While this package's author seeks to provide this software openly and under the MIT License, it contains derivative works from open-source projects such as the GNU C Library. As a result, the overall distribution is licensed under the GNU Lesser General Public License version 2.1 or later (at your choosing). Files that are not licensed under the MIT License contain a banner explicitly noting so alongside notes on which sections are licensed under the MIT License, if applicable.

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

libprng-0.0.1.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

libprng-0.0.1-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file libprng-0.0.1.tar.gz.

File metadata

  • Download URL: libprng-0.0.1.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for libprng-0.0.1.tar.gz
Algorithm Hash digest
SHA256 63c65b87a26c1d89c4b0e91bf80a27e7b6eabd62d2e96404cd6fecaf98bf474d
MD5 4a988132ac129be156811295cad41ce8
BLAKE2b-256 c197dba69c99de18aaa19c3af4a937b0f5d5422315667796129d40ebb14dd5a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for libprng-0.0.1.tar.gz:

Publisher: python-publish.yml on Jayson-Fong/libprng

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

File details

Details for the file libprng-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: libprng-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for libprng-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9858596104f029e56f092c8d432076567000e4f2de915ab6991ee679aad905c
MD5 4dd40461d5ea22e11eb7bb2ad884cc1b
BLAKE2b-256 e5d2f28e8d774f5a2a0ef3596e595849d1ea177f61ccf2f67e6e889d6f0ba3b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for libprng-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on Jayson-Fong/libprng

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