rand-half

Crates.iorand-half
lib.rsrand-half
version0.1.0
created_at2025-04-15 20:51:16.192107+00
updated_at2025-04-15 20:51:16.192107+00
descriptionRandom number generation support for half-precision floating point types
homepage
repositoryhttps://github.com/angelmarauder/rand-half
max_upload_size
id1635265
size13,000
Angelmarauder (angelmarauder)

documentation

README

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::*; // 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));
}
Commit count: 0

cargo fmt