Function malachite_base::vecs::random_values_from_vec
source · pub fn random_values_from_vec<T: Clone>(
seed: Seed,
xs: Vec<T>
) -> RandomValuesFromVec<T> ⓘ
Expand description
Uniformly generates a random value from a nonempty Vec
.
The iterator owns the data. It may be more convenient for the iterator to return references to a
pre-existing slice, in which case you may use
random_values_from_slice
instead.
The output length is infinite.
$P(x) = 1/n$, where $n$ is xs.len()
.
Panics
Panics if xs
is empty.
Examples
use itertools::Itertools;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::vecs::random_values_from_vec;
let xs = vec![2, 3, 5, 7, 11];
assert_eq!(
random_values_from_vec(EXAMPLE_SEED, xs).take(10).collect_vec(),
&[3, 7, 3, 5, 11, 3, 5, 11, 2, 2]
);
Examples found in repository?
src/iterators/mod.rs (line 384)
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
pub fn with_special_values<I: Iterator>(
seed: Seed,
special_values: Vec<I::Item>,
p_numerator: u64,
p_denominator: u64,
xs_gen: &dyn Fn(Seed) -> I,
) -> WithSpecialValues<I>
where
I::Item: Clone,
{
WithSpecialValues {
bs: weighted_random_bools(seed.fork("bs"), p_numerator, p_denominator),
special_values: random_values_from_vec(seed.fork("special_values"), special_values),
xs: xs_gen(seed.fork("xs")),
}
}
More examples
src/chars/random.rs (line 453)
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
pub fn graphic_weighted_random_char_inclusive_range(
seed: Seed,
a: char,
b: char,
p_numerator: u64,
p_denominator: u64,
) -> WeightedGraphicRandomCharRange {
if a > b {
panic!("a must be less than or equal to b. a: {}, b: {}", a, b);
}
let (graphic_chars, non_graphic_chars): (Vec<_>, Vec<_>) =
(a..=b).partition(|&c| char_is_graphic(c));
if graphic_chars.is_empty() {
panic!("The range {:?}..={:?} contains no graphic chars", a, b);
}
if non_graphic_chars.is_empty() {
panic!("The range {:?}..={:?} only contains graphic chars", a, b);
}
WeightedGraphicRandomCharRange {
xs: weighted_random_bools(seed.fork("xs"), p_numerator, p_denominator),
graphic: random_values_from_vec(seed.fork("graphic"), graphic_chars),
non_graphic: random_values_from_vec(seed.fork("non_graphic"), non_graphic_chars),
}
}
src/num/random/mod.rs (line 2422)
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
pub fn special_random_primitive_float_inclusive_range<T: PrimitiveFloat>(
seed: Seed,
mut a: T,
mut b: T,
mean_sci_exponent_numerator: u64,
mean_sci_exponent_denominator: u64,
mean_precision_numerator: u64,
mean_precision_denominator: u64,
mean_special_p_numerator: u64,
mean_special_p_denominator: u64,
) -> SpecialRandomFloatInclusiveRange<T> {
assert!(!a.is_nan());
assert!(!b.is_nan());
assert!(NiceFloat(a) <= NiceFloat(b));
assert_ne!(mean_special_p_denominator, 0);
assert!(mean_special_p_numerator <= mean_special_p_denominator);
assert_ne!(mean_precision_denominator, 0);
assert!(mean_precision_numerator > mean_precision_denominator);
let only_special =
a == T::POSITIVE_INFINITY || b == T::NEGATIVE_INFINITY || a == T::ZERO && b == T::ZERO;
let mut special_values = Vec::new();
if a == T::NEGATIVE_INFINITY {
special_values.push(a);
a = -T::MAX_FINITE;
}
if b == T::POSITIVE_INFINITY {
special_values.push(b);
b = T::MAX_FINITE;
}
if NiceFloat(a) <= NiceFloat(T::NEGATIVE_ZERO) && NiceFloat(b) >= NiceFloat(T::NEGATIVE_ZERO) {
special_values.push(T::NEGATIVE_ZERO);
}
if NiceFloat(a) <= NiceFloat(T::ZERO) && NiceFloat(b) >= NiceFloat(T::ZERO) {
special_values.push(T::ZERO);
}
if a == T::ZERO {
a = T::MIN_POSITIVE_SUBNORMAL;
}
if b == T::ZERO {
b = -T::MIN_POSITIVE_SUBNORMAL;
}
if only_special {
SpecialRandomFloatInclusiveRange::OnlySpecial(random_values_from_vec(seed, special_values))
} else if special_values.is_empty() {
SpecialRandomFloatInclusiveRange::NoSpecial(Box::new(
special_random_finite_float_inclusive_range(
seed,
a,
b,
mean_sci_exponent_numerator,
mean_sci_exponent_denominator,
mean_precision_numerator,
mean_precision_denominator,
),
))
} else {
SpecialRandomFloatInclusiveRange::Special(Box::new(with_special_values(
seed,
special_values,
mean_special_p_numerator,
mean_special_p_denominator,
&|seed| {
special_random_finite_float_inclusive_range(
seed,
a,
b,
mean_sci_exponent_numerator,
mean_sci_exponent_denominator,
mean_precision_numerator,
mean_precision_denominator,
)
},
)))
}
}