Skip to main content

random_string

Function random_string 

Source
pub fn random_string(len: usize, charset: &[u8]) -> String
Available on crate feature tier2 only.
Expand description

Generate a random string of exactly len characters drawn from charset (uniformly).

Selection is unbiased — bounded_u64(charset.len() as u64) is used internally, so the per-character distribution is uniform.

§Panics

Panics if charset is empty or contains a non-ASCII byte (byte >= 128). The ASCII guard preserves UTF-8 validity of the returned String.

§Example

use mod_rand::{tier2, charsets};
let s = tier2::random_string(16, charsets::ALPHANUMERIC);
assert_eq!(s.len(), 16);