Function malachite_base::chars::random::graphic_weighted_random_char_range
source · [−]pub fn graphic_weighted_random_char_range(
seed: Seed,
a: char,
b: char,
p_numerator: u64,
p_denominator: u64
) -> WeightedGraphicRandomCharRangeⓘNotable traits for WeightedGraphicRandomCharRangeimpl Iterator for WeightedGraphicRandomCharRange type Item = char;
Expand description
Generates random char
s in the half-open interval $[a, b)$, weighting graphic and
non-graphic char
s separately.
See char_is_graphic
for the definition of a graphic
char
.
Let $n_p$ be p_numerator
and $d_p$ be p_denominator
, and let $p = p_n/p_d$.
The set of graphic char
s in the specified range is selected with probability $p$, and the
set of non-graphic char
s in the range with probability $1-p$. Then, a char
is selected
uniformly from the appropriate set.
$a$ must be less than $b$. Furthermore, $[a, b)$ must contain both graphic and non-graphic
char
s. This function cannot create a range that includes char::MAX
; for that, use
graphic_weighted_random_char_inclusive_range
.
Let $g$ be the number of graphic char
s in $[a, b)$. Then we have
$$ P(x) = \begin{cases} \frac{p}{g} & a \leq x < b \ \text{and} \ x \ \text{is graphic} \\ \frac{1-p}{b-a-g} & a \leq x < b \ \text{and} \ x \ \text{is not graphic} \\ 0 & \text{otherwise} \end{cases} $$
To recover the uniform distribution, use $p = g/(b-a)$.
The output length is infinite.
Expected complexity per iteration
Constant time and additional memory.
Panics
Panics if p_denominator
is zero or p_denominator > p_denominator
, if $a \geq b$, if
$[a, b)$ contains no graphic char
s, or if $[a, b)$ contains only graphic char
s.
Examples
use malachite_base::chars::random::graphic_weighted_random_char_range;
use malachite_base::random::EXAMPLE_SEED;
assert_eq!(
graphic_weighted_random_char_range(EXAMPLE_SEED, '\u{100}', '\u{400}', 10, 11)
.take(30)
.collect::<String>()
.as_str(),
"ǘɂŜȢΙƘƣʅΰǟ˳ˊȇ\u{31b}ʰɥΈ\u{324}\u{35a}Ϟ\u{367}\u{337}ƃ\u{342}ʌμƢϳϪǰ"
)