Product OS Random
A comprehensive random number generation library providing cryptographically secure RNGs, random text/number generators, and word/name generators. Supports no_std environments.
Features
- Multiple RNG Backends: Support for
StdRng,ThreadRng,OsRng, and custom RNGs - Cryptographic Security: Dedicated
CryptoRNGtype for security-sensitive operations no_stdSupport: Works in embedded and constrained environments- Rich Generators: Random strings, passwords, keys, usernames, emails, and names
- Large Datasets: Built-in word lists, names, nouns, and adjectives
- Flexible API: Both stateful generators and one-time generation functions
Quick Start
Add to your Cargo.toml:
[]
= "0.0.27"
Basic Usage (Convenience Functions)
The simplest way to generate random data -- just call the top-level functions:
// Generate a random password
let password = random_password;
// Generate random alphanumeric string
let code = random_alphanumeric;
// Generate a random number in a range
let port = random_usize;
// Generate random bytes
let bytes = random_bytes;
// Generate a cryptographic key
let key = generate_random_key;
// Generate a random string from a custom character set
let hex = random_from_characters;
Stateful Generator
For repeated generation, create a RandomGenerator instance:
use ;
// Create a generator (uses default RNG)
let mut gen = new;
// Generate multiple random values efficiently
let bytes = gen.get_random_bytes;
let number = gen.get_random_usize;
let text = gen.get_random_string;
Custom RNG (Power Users)
For reproducible results or specific RNG backends, use the explicit API:
use ;
use SeedableRng;
// Seeded RNG for reproducible output
let rng = RNGStd;
let mut gen = new;
let number = gen.get_random_usize;
// Or use the one-time functions with an explicit RNG
let mut rng = Some;
let key = generate_key_one_time;
Cryptographic RNG
use ;
// Use OS randomness for maximum security
let mut rng = OS;
// Generate cryptographically secure random bytes
let mut key = ;
rng.fill_bytes;
Feature Flags
Core Features
default- Basic random generation withStdRngand getrandom supportcore- Full standard library support withStdRng,ThreadRng, andOsRngsend_only- Send-safe RNG variants onlyconstrained- Minimal allocation-only mode (requires user-provided RNG)custom- Custom RNG support with spin locks forno_stdcustom_send_only- Custom Send-safe RNG support
Dataset Features
words- Random word generationnouns- Random noun generationadjectives- Random adjective generationnames- Random full name generationfirst_names- Random first name generation (requiresinflections)last_names- Random last name generation (requiresinflections)
Example with Features
[]
= { = "0.0.27", = ["core", "names", "first_names", "last_names"] }
use RandomGenerator;
// Generate random username
let username = get_simple_random_username_one_time;
// Generate random email
let email = get_simple_random_email_one_time;
// Generate random first and last names
let first_name = get_simple_random_first_name_one_time;
let last_name = get_simple_random_last_name_one_time;
Security Considerations
Cryptographic vs Non-Cryptographic RNGs
CryptoRNG: Use for passwords, keys, tokens, and any security-sensitive dataRNG: Suitable for general-purpose randomness (simulations, games, testing)
Thread Safety
RNG::Threaduses thread-local storage- Custom RNG wrappers use
Arc<Mutex<>>for safe concurrent access - All RNG types implement
Clonefor easy distribution
Performance
- String generation pre-allocates with
String::with_capacity() - Helper functions optimize character filtering
- Lock-based custom RNGs may have contention under high concurrency
- Dataset features add to binary size (~400K lines of data)
no_std Support
This crate supports no_std environments:
[]
= { = "0.0.27", = false, = ["constrained"] }
Note: In constrained mode, you must provide an RNG to all generation functions.
Minimum Supported Rust Version (MSRV)
This crate requires Rust 1.69 or later.
License
This project is licensed under the GPL-3.0-only license.
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues.
Documentation
For detailed API documentation, visit docs.rs/product-os-random.