#byte #unsafe #byte-length #random #vec-t #generator #seed

block-pseudorand

Generator of random Vec<T>'s for types that can be transmuted from arbitrary bytes

3 releases

0.1.2 Mar 28, 2022
0.1.1 Sep 18, 2021
0.1.0 Sep 18, 2021

#2061 in Algorithms

Download history 33/week @ 2024-07-20 47/week @ 2024-07-27 33/week @ 2024-08-03 25/week @ 2024-08-10 23/week @ 2024-08-17 30/week @ 2024-08-24 56/week @ 2024-08-31 26/week @ 2024-09-07 35/week @ 2024-09-14 48/week @ 2024-09-21 50/week @ 2024-09-28 31/week @ 2024-10-05 36/week @ 2024-10-12 29/week @ 2024-10-19 34/week @ 2024-10-26 49/week @ 2024-11-02

149 downloads per month
Used in 10 crates (via rdst)

Apache-2.0 OR MIT

8KB
107 lines

block-pseudorand

This crate allows multi-threaded creation of pseudorandom Vec<T>'s of arbitrary length. It does this by generating arbitrary byte vectors randomly and transmuting to the provided type.

Warning: This is wildly unsafe for some types as it does not uphold any invariants your type might expect. Only use this crate if your type can be safely generated from completely arbitrary bytes. Generally, this means your type should consist of nothing but primitive numbers such as u32, i64, or f32.

Notes

  • Generated data is not guaranteed to be cryptographically secure
  • Output can be deterministic if you provide a seed
  • This is very unsafe for certain types, see the warning above

Use cases

If you need a lot of random numbers quickly for something non-production critical like unit tests, this may be a good candidate. Otherwise, if you are planning to use this at runtime, or with types that are non-numeric or otherwise cannot be created from arbitrary bytes, I would recommend you to choose another, safer crate.

Usage

If you are certain the above warnings do not apply to the type you are generating, you can use this library like so:

Without Seed

use block_pseudorand::block_rand;

let random_data: Vec<u64> = block_rand(128);

assert_eq!(random_data.len(), 128);

With Seed

use block_pseudorand::block_rand_with_seed;

// Populate this seed as you wish
let seed = [0u8; 32];
let random_data: Vec<u64> = block_rand_with_seed(128, &seed);

assert_eq!(random_data.len(), 128);

Dependencies

~1.5MB
~26K SLoC