pub trait RandomScalar: TensorElement {
// Required method
fn random_value<R: Rng + ?Sized>(rng: &mut R) -> Self;
}Expand description
Trait for scalar types that can generate random values from a standard
normal distribution.
This enables the generic IdxTensor::random constructor.
Implementations consume the supplied RNG directly, including a
dyn rand::RngCore, without creating or reseeding another generator.
§Examples
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
use rand_distr::{Distribution, StandardNormal};
use tensor4all_core::tensor::RandomScalar;
let mut rng = ChaCha8Rng::seed_from_u64(42);
let mut reference = rng.clone();
let erased: &mut dyn RngCore = &mut rng;
let expected: f64 = StandardNormal.sample(&mut reference);
assert_eq!(f64::random_value(erased), expected);Required Methods§
Sourcefn random_value<R: Rng + ?Sized>(rng: &mut R) -> Self
fn random_value<R: Rng + ?Sized>(rng: &mut R) -> Self
Draw a scalar from the caller’s standard normal RNG stream.
rng may be a concrete generator or an erased dyn rand::RngCore.
Returns one normal draw for f64, or independent real and imaginary
normal draws for Complex64.
§Examples
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
use rand_distr::{Distribution, StandardNormal};
use num_complex::Complex64;
use tensor4all_core::tensor::RandomScalar;
let mut rng = ChaCha8Rng::seed_from_u64(42);
let mut reference = rng.clone();
let erased: &mut dyn RngCore = &mut rng;
let expected = Complex64::new(
StandardNormal.sample(&mut reference), StandardNormal.sample(&mut reference));
assert_eq!(Complex64::random_value(erased), expected);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".