| Crates.io | rand_pool |
| lib.rs | rand_pool |
| version | 0.1.3 |
| created_at | 2025-02-25 23:07:38.159597+00 |
| updated_at | 2025-02-28 17:04:26.693025+00 |
| description | Create a pool of random numbers pre generated thread safe |
| homepage | |
| repository | https://github.com/lrleon/cpp_rand_pool |
| max_upload_size | |
| id | 1569803 |
| size | 14,593 |
A Rust library that provides efficient pooling of random number arrays for high-performance applications.
RandomNumberPool is a thread-safe utility that pre-generates and manages pools of random number arrays. It's designed to minimize overhead in applications that require frequent access to random numbers by maintaining a pool of pre-generated arrays.
Key features:
Add this to your Cargo.toml:
[dependencies]
rand_pool = "0.1.2"
use rand_pool::rand_pool;
fn main() {
// Create a pool with 10 arrays, each containing 1000 random numbers,
// using seed 42 for reproducibility
let pool = rand_pool::RandomNumberPool::new(10, 1000, 42);
// Get an array and its index from the pool
let (index, array) = pool.get_array().unwrap();
// Use the random numbers
for (i, &num) in array.iter().enumerate().take(5) {
println!("Random number {}: {}", i, num);
}
// Release the array back to the pool for refilling
pool.release_array(index);
}
The pool maintains three collections of array indices:
When you request an array:
When you release an array:
When the pool is dropped:
The RandomNumberPool constructor accepts three parameters:
pool_size: Number of arrays to maintain in the poolarray_size: Number of random numbers in each arrayseed: Seed value for the random number generator (for reproducibility)All operations on the pool are thread-safe, making it suitable for multi-threaded applications. The implementation uses standard Rust synchronization primitives:
Mutex for controlling access to shared dataCondvar for signaling between threadsArc for shared ownership across threadsThis project is licensed under the MIT License.
Leandro Leon
Freddy Cuellar