rand-half 0.1.0

Random number generation support for half-precision floating point types
Documentation
  • Coverage
  • 16.67%
    2 out of 12 items documented0 out of 11 items with examples
  • Size
  • Source code size: 12.49 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.26 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 23s Average build duration of successful builds.
  • all releases: 23s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • angelmarauder

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));
}