Skip to main content

Production-grade random number generation with multiple PRNG algorithms, cryptographic security, and statistical distributions

Project description

randomed

PyPI version Python 3.7+ License: MIT

Production-grade random number generation library featuring multiple PRNG algorithms, cryptographically secure functions, advanced statistical distributions, and optimization utilities. Engineered for simulations, machine learning, cryptography, and scientific computing.

Table of Contents

Installation

PyPI

pip install randomed

From Source

git clone https://github.com/michaljerney/randomed.git
cd randomed
pip install -e .

Requirements

  • Python 3.7+
  • No external dependencies

Core Algorithms

Mersenne Twister (MT19937)

Fast, general-purpose PRNG with excellent statistical properties and period of 2^19937−1. Industry standard for simulations.

Specifications:

  • State size: 624 × 32-bit words (~2.5 KB)
  • Period: 2^19937 - 1
  • Performance: ~660 million integers/second
  • Statistical quality: Passes DIEHARD, TestU01
from randomed import MersenneTwister

rng = MersenneTwister(seed=42)
value = rng.randint(1, 100)

PCG64 (Permuted Congruential Generator)

Modern PRNG combining linear congruential generator with permutation. Superior uniformity with smaller state.

Specifications:

  • State size: 128 bits (16 bytes)
  • Period: 2^128
  • Performance: ~900 million integers/second
  • Quality: Excellent with reduced correlation artifacts
from randomed import PCG64

rng = PCG64(seed=42)
value = rng.randint(1, 100)

XORShift256

Ultra-fast PRNG using XOR bit operations. Optimal for high-throughput random number demands.

Specifications:

  • State size: 256 bits (32 bytes)
  • Period: 2^256 - 1
  • Performance: ~2 billion integers/second
  • Quality: Good, excellent for Monte Carlo
from randomed import XORShift256

rng = XORShift256(seed=12345)
value = rng.randint(1, 100)

Quick Start

Basic Operations

import randomed

randomed.randint(1, 100)
randomed.uniform(0.0, 1.0)
randomed.gauss(0, 1)
randomed.choice([1, 2, 3])
randomed.sample([1, 2, 3, 4, 5], 3)
randomed.shuffle([1, 2, 3, 4])

Cryptographic Security

from randomed import SecureRandom, CryptographicRNG

token = SecureRandom.hex(32)
password = SecureRandom.password(16)
uuid = SecureRandom.uuid()

crypto = CryptographicRNG()
api_key = crypto.generate_api_key(prefix="sk")
session_id = crypto.generate_session_id()

Distributions

from randomed import Distributions, MersenneTwister

rng = MersenneTwister(seed=42)
dist = Distributions(rng)

normal = dist.normal(mu=0, sigma=1, n=1000)
poisson = dist.poisson(lambd=3, n=100)
brownian = dist.brownian_motion(steps=1000)

API Reference

Core Functions

randint(a: int, b: int) -> int
uniform(a: float, b: float) -> float
choice(seq) -> any
sample(population, k: int) -> List
shuffle(seq) -> List
gauss(mu: float = 0, sigma: float = 1) -> float

Secure Random

SecureRandom.randint(a, b) -> int
SecureRandom.bytes(length) -> bytes
SecureRandom.hex(length) -> str
SecureRandom.urlsafe(length) -> str
SecureRandom.password(length=16, use_digits=True, use_punctuation=True) -> str
SecureRandom.uuid() -> str

CryptographicRNG.generate_token(prefix="", length=32) -> str
CryptographicRNG.generate_session_id() -> str
CryptographicRNG.generate_api_key(prefix="sk") -> str
CryptographicRNG.generate_password(length=16) -> str

RNG Classes

class MersenneTwister:
    randint(a, b) -> int
    uniform(a, b) -> float
    gauss(mu, sigma) -> float
    expovariate(lambd) -> float
    gammavariate(alpha, beta) -> float
    betavariate(alpha, beta) -> float
    lognormvariate(mu, sigma) -> float
    vonmisesvariate(mu, kappa) -> float
    choice(seq) -> any
    choices(population, weights=None, k=1) -> List
    sample(population, k) -> List
    shuffle(seq) -> None

class PCG64:
    randint(a, b) -> int
    uniform(a, b) -> float

class XORShift256:
    next() -> int
    randint(a, b) -> int
    uniform(a, b) -> float

Distributions

class Distributions:
    normal(mu=0, sigma=1, n=1) -> List[float]
    exponential(lambd=1, n=1) -> List[float]
    gamma(alpha, beta=1, n=1) -> List[float]
    beta(alpha, beta, n=1) -> List[float]
    lognormal(mu=0, sigma=1, n=1) -> List[float]
    poisson(lambd, n=1) -> List[int]
    binomial(n, p, size=1) -> List[int]
    uniform(a, b, n=1) -> List[float]
    geometric(p, n=1) -> List[int]
    choice_weighted(choices, weights, n=1) -> List
    random_walk(steps, start=0, step_size=1) -> List[float]
    brownian_motion(steps, time_unit=0.01) -> List[float]

Advanced Classes

class TimeSeriesGenerator:
    seasonal(periods, trend_slope=0, noise_scale=0.1) -> List[float]
    arima_like(periods, p=0.7, d=0.3) -> List[float]

class Sampling:
    @staticmethod
    reservoir_sampling(stream, k, rng=None) -> List
    @staticmethod
    stratified_sampling(population, strata, k, rng=None) -> List

class Optimization:
    @staticmethod
    monte_carlo_integration(func, bounds, samples=10000, rng=None) -> float
    @staticmethod
    simulated_annealing(objective, initial_solution, temperature=1000, cooling_rate=0.95, iterations=1000, rng=None) -> tuple

class SyntheticDataGenerator:
    synthetic_dataframe(columns, rows) -> List[dict]
    synthetic_timeseries(length, frequency='H') -> List[dict]

Cryptographic Functions

Token Generation

from randomed import SecureRandom

token = SecureRandom.hex(32)
urlsafe = SecureRandom.urlsafe(32)
raw_bytes = SecureRandom.bytes(64)

Password & API Key Generation

password = SecureRandom.password(
    length=16,
    use_digits=True,
    use_punctuation=True
)

from randomed import CryptographicRNG
crypto = CryptographicRNG()
api_key = crypto.generate_api_key(prefix="sk")

Statistical Distributions

Continuous

from randomed import Distributions

dist = Distributions()

samples = dist.normal(mu=0, sigma=1, n=1000)
samples = dist.exponential(lambd=1.0, n=1000)
samples = dist.gamma(alpha=2.0, beta=1.0, n=1000)
samples = dist.beta(alpha=0.5, beta=0.5, n=1000)
samples = dist.lognormal(mu=0, sigma=1, n=1000)

Discrete

samples = dist.poisson(lambd=3, n=1000)
samples = dist.binomial(n=10, p=0.5, size=1000)
samples = dist.geometric(p=0.3, n=1000)

Advanced Features

Stochastic Processes

dist = Distributions()

random_walk = dist.random_walk(steps=100, start=0, step_size=1)
brownian = dist.brownian_motion(steps=1000, time_unit=0.01)

Time Series

from randomed import TimeSeriesGenerator

tsg = TimeSeriesGenerator()
seasonal = tsg.seasonal(periods=365, trend_slope=0.1, noise_scale=0.5)
arima = tsg.arima_like(periods=200, p=0.8, d=0.2)

Monte Carlo Integration

from randomed import Optimization

def sphere_vol(x, y, z):
    return 1 if x**2 + y**2 + z**2 <= 1 else 0

volume = Optimization.monte_carlo_integration(
    func=sphere_vol,
    bounds=[(-1, 1), (-1, 1), (-1, 1)],
    samples=100000
)

Simulated Annealing

def objective(x):
    return (x - 3)**2 + 2*x

solution, cost = Optimization.simulated_annealing(
    objective=objective,
    initial_solution=10,
    temperature=100,
    cooling_rate=0.95,
    iterations=1000
)

Reservoir Sampling

from randomed import Sampling

def stream():
    for i in range(1000000):
        yield i

sample = Sampling.reservoir_sampling(stream(), k=1000)

Synthetic Data

from randomed import SyntheticDataGenerator

gen = SyntheticDataGenerator()

data = gen.synthetic_dataframe(
    columns={'id': 'int', 'value': 'float', 'score': 'normal'},
    rows=10000
)

ts = gen.synthetic_timeseries(length=365)

Performance

Benchmarks (operations/second)

Algorithm Speed Memory Use Case
XORShift256 2B ops/sec 32 bytes Ultra-high throughput
PCG64 900M ops/sec 16 bytes General purpose
MersenneTwister 660M ops/sec 2.5 KB Simulations
SecureRandom 50M ops/sec minimal Cryptography

Theory

PRNG Quality Metrics

Equidistribution: Output values appear with equal frequency across the period

Periodicity: Sequence length before repetition; longer period = better

Statistical Independence: Minimal correlation between consecutive outputs

Computational Efficiency: CPU cycles required per output

Mersenne Twister Characteristics

MT19937 uses matrix linear recurrence over GF(2) with:

  • 624-word state vector
  • 397-tap recurrence relation
  • Tempering transformation for output
  • Period exactly 2^19937 - 1

Passes DIEHARD, NIST, TestU01 suites.

PCG Algorithm

PCG combines:

  • Linear congruential generator (fast state advancement)
  • Permutation function (output quality improvement)
  • Variable output permutation (reduced correlation)

Superior to MT19937 in statistical tests with 1/4 the state.

XORShift Method

Ultra-fast generators using only XOR and bit shifts:

  • Trivial implementation
  • Minimal state
  • High throughput
  • Lower period (still 2^256 - 1)

Examples

Example 1: Monte Carlo Pi Estimation

from randomed import MersenneTwister

rng = MersenneTwister()
inside = sum(1 for _ in range(1000000) 
             if rng.uniform(0,1)**2 + rng.uniform(0,1)**2 <= 1)
pi_est = 4 * inside / 1000000

Example 2: Portfolio Optimization

from randomed import Optimization

def variance(weights):
    return sum(w**2 for w in weights)

optimal, var = Optimization.simulated_annealing(
    objective=variance,
    initial_solution=[0.5, 0.3, 0.2],
    temperature=10,
    iterations=5000
)

Example 3: Secure Token Generation

from randomed import CryptographicRNG

crypto = CryptographicRNG()
tokens = [crypto.generate_session_id() for _ in range(1000)]

Example 4: Time Series Forecasting

from randomed import TimeSeriesGenerator

tsg = TimeSeriesGenerator()
training = tsg.seasonal(periods=1000, trend_slope=0.05)
testing = tsg.seasonal(periods=100, trend_slope=0.05)

Example 5: Stratified Sampling

from randomed import Sampling

pop = list(range(10000))
strata = [pop[i:i+1000] for i in range(0, 10000, 1000)]
sample = Sampling.stratified_sampling(pop, strata, k=500)

License

MIT License - Copyright (c) 2025

References

  • Matsumoto & Nishimura (1998): "Mersenne Twister" - ACM TOMS
  • O'Neill (2014): "PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms"
  • Knuth (1997): "The Art of Computer Programming, Volume 2" - Seminumerical Algorithms

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

randomed-0.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

randomed-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for randomed-0.1.0.tar.gz
Algorithm Hash digest
SHA256 84f6f16b633a0647feae3093e71ce2f91d91b7f5a583cdd18a1603072abed234
MD5 0fc280d5b151eacd1e6c897fe0394ff6
BLAKE2b-256 735b843b8d993cdb85dee45e2c6a68c901faa9eadf66c0b09d5f080206fb5dcc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for randomed-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee462e03b1b5e1ea94b30a240db59a1d508834696b1933e9a0dea474a8ae100f
MD5 fc2192e94b89026fd6f3d2308da28bc8
BLAKE2b-256 e25070c2e08a07991e890d14137e9da98cbe8ed5da12cc45962b0deec7b94e0c

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