Function malachite_base::chars::random::graphic_weighted_random_ascii_chars
source · [−]pub fn graphic_weighted_random_ascii_chars(
seed: Seed,
p_numerator: u64,
p_denominator: u64
) -> WeightedGraphicRandomCharRangeⓘNotable traits for WeightedGraphicRandomCharRangeimpl Iterator for WeightedGraphicRandomCharRange type Item = char;
Expand description
Generates random ASCII char
s, 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 ASCII char
s is selected with probability $p$, and the set of non-graphic
ASCII char
s with probability $1-p$. Then, a char
is selected uniformly from the
appropriate set. There are 95 graphic ASCII char
s out of 128, so we have
$$ P(x) = \begin{cases} \frac{p}{95} & \text{if} \quad x < \backslash\text{u\{0x80\}} \ \text{and} \ x \ \text{is graphic} \\ \frac{1-p}{33} & \text{if} \quad x < \backslash\text{u\{0x80\}} \ \text{and} \ x \ \text{is not graphic} \\ 0 & \text{otherwise} \end{cases} $$
To recover the uniform distribution, use $p = 95/128$.
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
.
Examples
use malachite_base::chars::random::graphic_weighted_random_ascii_chars;
use malachite_base::random::EXAMPLE_SEED;
assert_eq!(
graphic_weighted_random_ascii_chars(EXAMPLE_SEED, 10, 11)
.take(40)
.collect::<String>()
.as_str(),
"x14N(bcXr$g)7\u{1b}/E+\u{8}\rf\u{2}\u{11}Y\u{11}Poo.$V2R.$V=6\u{13}\t\u{11}"
)