rand-half
This crate provides random number generation support for half-precision floating point types (bf16 and f16) from the half crate.
Features
- Random uniform distribution for
bf16 and f16 types
- Random normal distribution for
bf16 and f16 types
- Integration with the
rand and rand_distr ecosystems
Usage
use half::{bf16, f16};
use rand::prelude::*;
use rand_distr::Normal;
use rand_half::*;
fn main() {
let mut rng = rand::thread_rng();
let random_bf16: bf16 = rng.gen();
println!("Random bf16: {}", f32::from(random_bf16));
let random_f16: f16 = rng.gen();
println!("Random f16: {}", f32::from(random_f16));
let uniform = Uniform::new(bf16::from_f32(-1.0), bf16::from_f32(1.0));
let uniform_bf16 = uniform.sample(&mut rng);
println!("Uniform bf16: {}", f32::from(uniform_bf16));
let normal = Normal::new(0.0, 1.0).unwrap();
let normal_bf16: bf16 = normal.sample(&mut rng);
println!("Normal bf16: {}", f32::from(normal_bf16));
}