Skip to main content

malachite_base/rounding_modes/
random.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::random::Seed;
10use crate::rounding_modes::{ROUNDING_MODES, RoundingMode};
11use crate::slices::{RandomValuesFromSlice, random_values_from_slice};
12use std::iter::Copied;
13
14/// Uniformly generates random [`RoundingMode`]s.
15pub type RandomRoundingModes = Copied<RandomValuesFromSlice<'static, RoundingMode>>;
16
17/// Uniformly generates random [`RoundingMode`]s.
18///
19/// The output length is infinite.
20///
21/// # Expected complexity
22///
23/// Constant time and additional memory.
24///
25/// # Examples
26/// ```
27/// use itertools::Itertools;
28/// use malachite_base::random::EXAMPLE_SEED;
29/// use malachite_base::rounding_modes::random::random_rounding_modes;
30/// use malachite_base::rounding_modes::RoundingMode::*;
31///
32/// assert_eq!(
33///     random_rounding_modes(EXAMPLE_SEED).take(10).collect_vec(),
34///     &[Up, Exact, Ceiling, Up, Floor, Nearest, Exact, Up, Floor, Exact]
35/// )
36/// ```
37#[inline]
38pub fn random_rounding_modes(seed: Seed) -> RandomRoundingModes {
39    random_values_from_slice(seed, &ROUNDING_MODES).copied()
40}