Skip to main content

Module tier2

Module tier2 

Source
Available on crate feature tier2 only.
Expand description

§Tier 2 — Process-unique seeds

Fast pseudo-random values derived from process ID, high-resolution time, and a monotonic atomic counter, then run through several rounds of a strong scalar mixer. Good for:

  • Tempdir / temp-file names
  • Request and trace IDs
  • Log correlation IDs
  • Any application-level “good-enough” uniqueness

Not cryptographic. An attacker who can observe outputs can often reconstruct internal state and predict subsequent values. Use tier3 for security-sensitive randomness.

§Uniqueness guarantees

  • Within a single process: every unique_u64 call returns a distinct value (guaranteed by a never-resetting atomic counter, modulo wrap at 2^64 calls — about 584 years at 1 GHz).
  • Across processes on the same host: PID + nanosecond timestamp make collisions vanishingly unlikely.
  • Across hosts: this tier makes no claims. Use tier3 when you need values unique across machines.

§Thread safety

All functions in this module are thread-safe and lock-free. The shared atomic counter ensures uniqueness even under concurrent access from any number of threads.

§Performance

Target <100ns per call. Cost is dominated by a SystemTime::now() reading; the mixing itself is a handful of multiplies and rotates.

Functions§

random_alpha
Generate a random ASCII alphabetic string (A-Z, a-z) of exactly len characters.
random_alphanumeric
Generate a random ASCII alphanumeric string (A-Z, a-z, 0-9) of exactly len characters.
random_hex_string
Generate a random lowercase hex string (0-9, a-f) of exactly len characters.
random_numeric
Generate a random ASCII numeric string (0-9) of exactly len characters. Leading zeros may appear.
random_string
Generate a random string of exactly len characters drawn from charset (uniformly).
range_i8
Generate a uniformly-distributed i8 in the half-open range [range.start, range.end).
range_i16
Generate a uniformly-distributed i16 in the half-open range [range.start, range.end).
range_i32
Generate a uniformly-distributed i32 in the half-open range [range.start, range.end).
range_i64
Generate a uniformly-distributed i64 in the half-open range [range.start, range.end).
range_i128
Generate a uniformly-distributed i128 in the half-open range [range.start, range.end).
range_inclusive_i8
Generate a uniformly-distributed i8 in the closed range [range.start(), range.end()]. The full-width i8::MIN..=i8::MAX is supported.
range_inclusive_i16
Generate a uniformly-distributed i16 in the closed range [range.start(), range.end()]. The full-width i16::MIN..=i16::MAX is supported.
range_inclusive_i32
Generate a uniformly-distributed i32 in the closed range [range.start(), range.end()].
range_inclusive_i64
Generate a uniformly-distributed i64 in the closed range [range.start(), range.end()].
range_inclusive_i128
Generate a uniformly-distributed i128 in the closed range [range.start(), range.end()]. The full-width i128::MIN..=i128::MAX is supported.
range_inclusive_isize
Generate a uniformly-distributed isize in the closed range [range.start(), range.end()]. The full-width isize::MIN..=isize::MAX is supported.
range_inclusive_u8
Generate a uniformly-distributed u8 in the closed range [range.start(), range.end()]. The full-width 0..=u8::MAX is supported.
range_inclusive_u16
Generate a uniformly-distributed u16 in the closed range [range.start(), range.end()]. The full-width 0..=u16::MAX is supported.
range_inclusive_u32
Generate a uniformly-distributed u32 in the closed range [range.start(), range.end()].
range_inclusive_u64
Generate a uniformly-distributed u64 in the closed range [range.start(), range.end()].
range_inclusive_u128
Generate a uniformly-distributed u128 in the closed range [range.start(), range.end()]. The full-width 0..=u128::MAX is supported.
range_inclusive_usize
Generate a uniformly-distributed usize in the closed range [range.start(), range.end()]. The full-width 0..=usize::MAX is supported.
range_isize
Generate a uniformly-distributed isize in the half-open range [range.start, range.end).
range_u8
Generate a uniformly-distributed u8 in the half-open range [range.start, range.end).
range_u16
Generate a uniformly-distributed u16 in the half-open range [range.start, range.end).
range_u32
Generate a uniformly-distributed u32 in the half-open range [range.start, range.end).
range_u64
Generate a uniformly-distributed u64 in the half-open range [range.start, range.end).
range_u128
Generate a uniformly-distributed u128 in the half-open range [range.start, range.end).
range_usize
Generate a uniformly-distributed usize in the half-open range [range.start, range.end).
unique_base32
Produce a process-unique base32 (Crockford alphabet) string of exactly len characters. Equivalent to unique_name; provided for symmetry with unique_hex.
unique_hex
Produce a process-unique lowercase hex string of exactly len characters.
unique_name
Produce a process-unique base32 (Crockford alphabet) name of exactly len characters.
unique_u64
Produce a process-unique u64.