| Crates.io | rand-half |
| lib.rs | rand-half |
| version | 0.1.0 |
| created_at | 2025-04-15 20:51:16.192107+00 |
| updated_at | 2025-04-15 20:51:16.192107+00 |
| description | Random number generation support for half-precision floating point types |
| homepage | |
| repository | https://github.com/angelmarauder/rand-half |
| max_upload_size | |
| id | 1635265 |
| size | 13,000 |
This crate provides random number generation support for half-precision floating point types (bf16 and f16) from the half crate.
bf16 and f16 typesbf16 and f16 typesrand and rand_distr ecosystemsuse half::{bf16, f16};
use rand::prelude::*;
use rand_distr::Normal;
use rand_half::*; // This brings the trait implementations into scope
fn main() {
let mut rng = rand::thread_rng();
// Generate random bf16 values
let random_bf16: bf16 = rng.gen();
println!("Random bf16: {}", f32::from(random_bf16));
// Generate random f16 values
let random_f16: f16 = rng.gen();
println!("Random f16: {}", f32::from(random_f16));
// Uniform distribution
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));
// Normal distribution
let normal = Normal::new(0.0, 1.0).unwrap();
let normal_bf16: bf16 = normal.sample(&mut rng);
println!("Normal bf16: {}", f32::from(normal_bf16));
}