Function random

Source
pub fn random<T>(dist: impl Distribution<T>) -> T
🔬This is a nightly-only experimental API. (random #130703)
Expand description

Generates a random value from a distribution, using the default random source.

This is a convenience function for dist.sample(&mut DefaultRandomSource) and will sample according to the same distribution as the underlying Distribution trait implementation. See DefaultRandomSource for more information about how randomness is sourced.

§Examples

Generating a version 4/variant 1 UUID represented as text:

#![feature(random)]

use std::random::random;

let bits: u128 = random(..);
let g1 = (bits >> 96) as u32;
let g2 = (bits >> 80) as u16;
let g3 = (0x4000 | (bits >> 64) & 0x0fff) as u16;
let g4 = (0x8000 | (bits >> 48) & 0x3fff) as u16;
let g5 = (bits & 0xffffffffffff) as u64;
let uuid = format!("{g1:08x}-{g2:04x}-{g3:04x}-{g4:04x}-{g5:012x}");
println!("{uuid}");