Skip to main content

Module random

Module random 

Source
Expand description

Iterators that generate tuples randomly.

§random_pairs

use itertools::Itertools;
use malachite_base::chars::random::random_char_inclusive_range;
use malachite_base::num::random::random_unsigned_inclusive_range;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::tuples::random::random_pairs;

let ps = random_pairs(
    EXAMPLE_SEED,
    &|seed| random_unsigned_inclusive_range::<u8>(seed, 0, 2),
    &|seed| random_char_inclusive_range(seed, 'x', 'z'),
);
assert_eq!(
    ps.take(20).collect_vec().as_slice(),
    &[
        (1, 'z'),
        (1, 'x'),
        (1, 'z'),
        (1, 'y'),
        (2, 'x'),
        (0, 'z'),
        (0, 'z'),
        (0, 'z'),
        (2, 'z'),
        (0, 'y'),
        (2, 'x'),
        (0, 'x'),
        (2, 'z'),
        (0, 'z'),
        (2, 'x'),
        (2, 'x'),
        (2, 'y'),
        (1, 'y'),
        (0, 'x'),
        (2, 'x')
    ]
);

§random_pairs_from_single

use itertools::Itertools;
use malachite_base::num::random::random_unsigned_inclusive_range;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::tuples::random::random_pairs_from_single;

let ps = random_pairs_from_single(random_unsigned_inclusive_range::<u8>(EXAMPLE_SEED, 0, 2));
assert_eq!(
    ps.take(20).collect_vec().as_slice(),
    &[
        (1, 0),
        (1, 2),
        (1, 1),
        (0, 1),
        (0, 2),
        (1, 0),
        (1, 2),
        (2, 0),
        (1, 0),
        (2, 2),
        (2, 1),
        (0, 2),
        (2, 1),
        (1, 1),
        (0, 0),
        (2, 0),
        (2, 2),
        (1, 0),
        (1, 1),
        (0, 2)
    ]
);

§random_triples_xyx

use itertools::Itertools;
use malachite_base::chars::random::random_char_inclusive_range;
use malachite_base::num::random::random_unsigned_inclusive_range;
use malachite_base::random::{Seed, EXAMPLE_SEED};
use malachite_base::random_custom_tuples;

random_custom_tuples!(
    (pub(crate)),
    RandomTriplesXYX,
    (X, Y, X),
    random_triples_xyx,
    [X, I, xs, xs_gen, [x_0, x_0], [x_2, y_1]],
    [Y, J, ys, ys_gen, [y_1, x_2]]
);

// We are generating triples of `char`s using two input iterators. The first iterator, `xs`,
// produces all ASCII `char`s, and the second, `ys`, produces the three numbers 0, 1, and 2. The
// function we're using is `random_triples_xyx`, meaning that the first element of the
// output triples will be taken from `xs`, the second element from `ys`, and the third also from
// `xs`.
let ts = random_triples_xyx(
    EXAMPLE_SEED,
    &|seed| random_char_inclusive_range(seed, 'x', 'z'),
    &|seed| random_unsigned_inclusive_range::<u8>(seed, 0, 2),
);
assert_eq!(
    ts.take(20).collect_vec().as_slice(),
    &[
        ('y', 2, 'y'),
        ('y', 0, 'y'),
        ('z', 2, 'x'),
        ('x', 1, 'x'),
        ('z', 0, 'x'),
        ('z', 2, 'x'),
        ('z', 2, 'x'),
        ('z', 2, 'z'),
        ('z', 2, 'y'),
        ('x', 1, 'z'),
        ('z', 0, 'x'),
        ('y', 0, 'z'),
        ('y', 2, 'z'),
        ('x', 2, 'z'),
        ('z', 0, 'y'),
        ('z', 0, 'y'),
        ('y', 1, 'x'),
        ('z', 1, 'z'),
        ('x', 0, 'z'),
        ('z', 0, 'x')
    ]
);

§random_ordered_unique_quadruples

use itertools::Itertools;
use malachite_base::num::random::random_unsigned_inclusive_range;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::random_ordered_unique_tuples;
use malachite_base::sets::random::{
    random_b_tree_sets_fixed_length, RandomBTreeSetsFixedLength,
};

random_ordered_unique_tuples!(
    (pub(crate)),
    RandomOrderedUniqueQuadruples,
    4,
    (I::Item, I::Item, I::Item, I::Item),
    random_ordered_unique_quadruples,
    [0, 1, 2, 3]
);

let qs = random_ordered_unique_quadruples(random_unsigned_inclusive_range::<u8>(
    EXAMPLE_SEED,
    1,
    10,
));
assert_eq!(
    qs.take(20).collect_vec().as_slice(),
    &[
        (2, 5, 6, 8),
        (3, 5, 7, 9),
        (1, 2, 6, 8),
        (3, 4, 6, 7),
        (3, 6, 9, 10),
        (4, 6, 8, 10),
        (3, 6, 8, 10),
        (2, 5, 9, 10),
        (2, 3, 8, 10),
        (1, 3, 7, 8),
        (1, 2, 6, 10),
        (2, 5, 8, 9),
        (1, 8, 9, 10),
        (1, 3, 7, 8),
        (2, 3, 4, 5),
        (1, 3, 4, 8),
        (3, 6, 7, 9),
        (5, 6, 7, 8),
        (3, 4, 5, 9),
        (4, 6, 9, 10)
    ]
);

§random_unique_quadruples

use itertools::Itertools;
use malachite_base::num::random::random_unsigned_inclusive_range;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::random_unique_tuples;
use std::collections::HashMap;
use std::hash::Hash;

random_unique_tuples!(
    (pub(crate)),
    RandomOrderedUniqueQuadruples,
    4,
    (I::Item, I::Item, I::Item, I::Item),
    random_unique_quadruples,
    [0, 1, 2, 3]
);

let qs = random_unique_quadruples(random_unsigned_inclusive_range::<u8>(EXAMPLE_SEED, 1, 10));
assert_eq!(
    qs.take(20).collect_vec().as_slice(),
    &[
        (2, 8, 6, 5),
        (7, 5, 3, 9),
        (2, 8, 6, 1),
        (3, 7, 4, 6),
        (3, 10, 6, 9),
        (6, 10, 4, 8),
        (6, 10, 8, 3),
        (10, 2, 9, 5),
        (8, 10, 2, 3),
        (8, 1, 7, 3),
        (2, 6, 1, 10),
        (9, 5, 8, 2),
        (8, 1, 9, 10),
        (7, 3, 8, 1),
        (3, 2, 5, 4),
        (3, 8, 4, 1),
        (9, 7, 6, 3),
        (5, 7, 8, 6),
        (5, 3, 9, 4),
        (9, 10, 4, 6)
    ]
);

Structs§

RandomOrderedUniquePairs
Generates random pairs using elements from a single iterator, where the first element is less than the second.
RandomPairs
This documentation applies not only to RandomPairs, but also to RandomTriples, RandomQuadruples, and so on. See random_tuples for more information.
RandomPairsFromSingle
This documentation applies not only to RandomPairsFromSingle, but also to RandomTriplesFromSingle, RandomQuadruplesFromSingle, and so on. See random_tuples for more information.
RandomUniquePairs
Generates random pairs using elements from a single iterator, where the first element is not equal to the second.

Functions§

random_ordered_unique_pairs
Generates random pairs using elements from a single iterator, where the first element of each pair is less than the second.
random_pairs
This documentation applies not only to random_pairs, but also to random_triples, random_quadruples, and so on. See random_tuples for more information.
random_pairs_from_single
This documentation applies not only to random_pairs_from_single, but also to random_triples_from_single, random_quadruples_from_single, and so on. See random_tuples for more information.
random_unique_pairs
Generates random pairs using elements from a single iterator, where the two elements of each pair are unequal.
random_units
Generates random units; repeats ().