ax-rnd
Axrnd is a fast, small random number generator (rnd) library and CLI tool written in Rust.
Features:
- Fast: Single-call and bulk operations
- Small: Single
u64state (32 bytes with alignment) - Pure Rust: No external dependencies
- CLI Tool: Installable via
cargo install ax-rnd
Performance
Benchmark Results (Release Mode)
Single-call (next_u64):
| rnd | ps/iter | vs fastrand |
|---|---|---|
| ax-rnd | 317 | 0.9% faster |
| fastrand | 321 | baseline |
| rand_small | 692 | 2.2x slower |
Bulk fill (fill_bytes/1MB):
| rnd | GiB/s | vs fastrand |
|---|---|---|
| ax-rnd | 37.68 | 0.1% faster |
| fastrand | 37.64 | baseline |
| rand_small | 10.74 | 3.5x slower |
Algorithm: Custom variant based on Golden Ratio constant with XOR-first-then-multiply pattern.
Installation
As a Library
Add to your Cargo.toml:
[]
= "0.1.0"
As a CLI Tool
Library API
Basic Usage
Convenience API (like fastrand)
For quick random generation without managing state:
use ;
// Generate random numbers directly
let x = u32;
let y = u64;
let b = bool;
// Generate random floats
let f = f32;
let d = f64;
// Generate random strings
let token = alphanumeric; // base62 alphanumeric
let url_token = base64url; // base64url (URL-safe)
// Fill buffers
let mut buf = ;
fill;
Deterministic API (with seed control)
For production use where you need deterministic behavior:
use ;
// Create RNG with seed
let mut rng = rnd;
// Generate random values
let x = rng.next_u64;
let y = rng.next_u32;
let b = rng.next_bool;
let f = rng.next_f64;
Alphanumeric Strings
use rnd;
let mut rnd = rnd;
// Base62 (A-Z, a-z, 0-9) - 62 characters
let alpha = rnd.alpha; // "aB3xY9..."
// Base64url (A-Z, a-z, 0-9, -, _) - 64 characters, URL-safe
let token = rnd.token; // "UxelSo4vKQ3CgvhV..."
Bounded Random Numbers
use rnd;
let mut rnd = rnd;
// Random number in [0, upper)
let x = rnd.bounded_u64;
let y = rnd.bounded_u32;
Bulk Operations
use rnd;
let mut rnd = rnd;
// Fill byte buffer
let mut buf = ;
rnd.fill_bytes;
// Fill u64 buffer
let mut u64_buf = ;
rnd.fill_u64;
// Fill u32 buffer
let mut u32_buf = ;
rnd.fill_u32;
State Management
use Axrnd;
let mut rnd = new;
// Get current state
let state = rnd.state; // [u64; 1]
// Restore from state
let mut restored = from_raw;
// Split for independent streams
let mut other = rnd.split;
// Reseed
rnd.reseed;
Top-level Functions
use ax_rnd;
// One-off random values (creates new rnd each time)
let x = random_u64;
let y = random_u32;
let b = random_bool;
let f = random_f64;
// One-off bounded
let z = bounded_u64;
// One-off strings
let alpha = alpha;
let token = token;
CLI Usage
Commands
| Command | Description |
|---|---|
bytes [count] [--hex] [seed] |
Generate random bytes |
alpha [len] [seed] |
Generate alphanumeric (base62) |
token [len] [seed] |
Generate URL-safe token (base64url) |
shuffle [file] [seed] |
Shuffle lines from stdin or file |
u64 [count] [seed] |
Generate random u64 numbers |
uuid [count] [seed] |
Generate UUID v4 |
Examples
# Generate 64 random bytes (binary output)
# Generate 32 bytes as hex string
# 836233f222448066354d9340859e3743
# Generate 16-char alphanumeric string
# SEV3G6JYHei5YFhe
# Generate 32-char URL-safe token
# UxelSo4vKQ3CgvhV
# Generate 5 random u64 numbers
# 9370916739074745922
# 8432900285853589045
# ...
# Generate 3 UUIDs
# f59ba06a-85b2-4423-b795-999e1bd2e038
# adea1bad-c71d-49d3-8ab2-2bef2010a96a
# ...
# Shuffle lines from stdin
|
# b
# d
# a
# c
# Shuffle lines from file
# Use specific seed for reproducibility
Options
count/len- Number of bytes/chars/numbers (default: 32)--hex- Output bytes as hex stringseed- rnd seed (default: current timestamp)- Use
timefor current timestamp - Use numeric value for fixed seed
- Use
Testing
Unit Tests
Test Coverage:
- Deterministic sequences
- Different seeds produce different streams
- Fill operations
- Bounded range correctness
- Float range correctness
- State roundtrip
- Split functionality
- Alphanumeric character sets
- Base64url character sets
- Statistical quality tests (avalanche, bit distribution, stream correlation)
Results: 28 tests passing
Statistical Quality Tests
The library includes statistical quality tests in tests/statistical.rs:
- Bit histogram uniformity
- Avalanche effect
- Stream correlation
- Low byte uniformity
- PractRand stream quality (8MB sample)
Results: All statistical tests passing
Benchmarking
Algorithm
Core Algorithm: Custom variant based on Golden Ratio constant (0x9E3779B97F4A7C15)
Pattern:
state += GR; // Golden ratio increment
xored = state ^ GR; // XOR first
math = state * xored; // Multiply (128-bit)
return ^ ; // Fold
License
MIT