Skip to main content

malachite_base/orderings/
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::orderings::ORDERINGS;
10use crate::random::Seed;
11use crate::slices::{RandomValuesFromSlice, random_values_from_slice};
12use std::cmp::Ordering;
13use std::iter::Copied;
14
15pub type RandomOrderings = Copied<RandomValuesFromSlice<'static, Ordering>>;
16
17/// Generates a random [`Ordering`] that has an equal probability of being `Less`, `Greater`, or
18/// `Equal`.
19///
20/// $P(<) = P(=) = P(>) = \frac{1}{3}$.
21///
22/// The output length is infinite.
23///
24/// # Expected complexity per iteration
25/// Constant time and additional memory.
26///
27/// # Examples
28/// ```
29/// use itertools::Itertools;
30/// use malachite_base::orderings::random::random_orderings;
31/// use malachite_base::random::EXAMPLE_SEED;
32/// use std::cmp::Ordering::*;
33///
34/// assert_eq!(
35///     random_orderings(EXAMPLE_SEED).take(10).collect_vec(),
36///     &[Less, Equal, Less, Greater, Less, Less, Equal, Less, Equal, Greater]
37/// )
38/// ```
39#[inline]
40pub fn random_orderings(seed: Seed) -> RandomOrderings {
41    random_values_from_slice(seed, &ORDERINGS).copied()
42}