malachite_base/num/random/striped.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::bools::random::{RandomBools, WeightedRandomBools, random_bools, weighted_random_bools};
10use crate::iterators::{NonzeroValues, nonzero_values};
11use crate::num::basic::signeds::PrimitiveSigned;
12use crate::num::basic::unsigneds::PrimitiveUnsigned;
13use crate::num::conversion::traits::{ExactFrom, WrappingFrom};
14use crate::num::random::geometric::{
15 GeometricRandomNaturalValues, geometric_random_unsigned_inclusive_range,
16 geometric_random_unsigneds, mean_to_p_with_min,
17};
18use crate::num::random::{
19 RandomUnsignedInclusiveRange, RandomUnsignedRange, random_unsigned_inclusive_range,
20 random_unsigned_range,
21};
22use crate::random::Seed;
23use itertools::Itertools;
24use std::iter::{Repeat, repeat};
25use std::marker::PhantomData;
26
27/// Generates bits from a striped random sequence.
28///
29/// See [here](self) for more information.
30#[derive(Clone, Debug)]
31pub struct StripedBitSource {
32 first_bit_of_block: bool,
33 previous_bit: bool,
34 bs: RandomBools,
35 xs: WeightedRandomBools,
36}
37
38impl Iterator for StripedBitSource {
39 type Item = bool;
40
41 /// Gets a bit from this `StripedBitSource`. If this function is being called for the first
42 /// time, the probabilities of a `true` or a `false` are equal. On subsequent calls, the
43 /// probability of getting a bit different from the previous one is $1 / m$.
44 ///
45 /// To reset the bit source, so that the next call to `next` has equal probabilities of `true`
46 /// or `false`, call [`end_block`](Self::end_block).
47 ///
48 /// # Expected complexity
49 /// Constant time and additional memory.
50 #[inline]
51 fn next(&mut self) -> Option<bool> {
52 self.previous_bit = if self.first_bit_of_block {
53 self.first_bit_of_block = false;
54 self.bs.next().unwrap()
55 } else {
56 self.previous_bit ^ self.xs.next().unwrap()
57 };
58 Some(self.previous_bit)
59 }
60}
61
62impl StripedBitSource {
63 /// Creates a new `StripedBitSource`.
64 ///
65 /// The mean run length is $m$, where $m$ is `m_numerator / m_denominator`.
66 ///
67 /// # Expected complexity
68 /// Constant time and additional memory.
69 ///
70 /// # Panics
71 /// Panics if `m_denominator` is zero or if `m_numerator <= m_denominator`.
72 ///
73 /// # Examples
74 /// ```
75 /// use malachite_base::num::random::striped::StripedBitSource;
76 /// use malachite_base::random::EXAMPLE_SEED;
77 ///
78 /// let bit_source = StripedBitSource::new(EXAMPLE_SEED, 4, 1);
79 /// let mut string = String::with_capacity(40);
80 /// for bit in bit_source.take(40) {
81 /// if bit {
82 /// string.push('1');
83 /// } else {
84 /// string.push('0');
85 /// }
86 /// }
87 /// assert_eq!(string, "0000000101100110000000011110000000001111");
88 /// ```
89 pub fn new(seed: Seed, m_numerator: u64, m_denominator: u64) -> Self {
90 assert_ne!(m_denominator, 0);
91 assert!(m_numerator > m_denominator);
92 let (numerator, denominator) = mean_to_p_with_min(1u64, m_numerator, m_denominator);
93 Self {
94 first_bit_of_block: true,
95 previous_bit: false,
96 bs: random_bools(seed.fork("bs")),
97 xs: weighted_random_bools(seed.fork("xs"), numerator, numerator + denominator),
98 }
99 }
100
101 /// Resets this `StripedBitSource`, so that the next time [`next`](Self::next) is called, the
102 /// probabilities of `true` or `false` will be equal.
103 ///
104 /// # Expected complexity
105 /// Constant time and additional memory.
106 ///
107 /// # Examples
108 /// ```
109 /// use malachite_base::num::random::striped::StripedBitSource;
110 /// use malachite_base::random::EXAMPLE_SEED;
111 ///
112 /// fn generate_string(bit_source: &mut StripedBitSource) -> String {
113 /// let mut string = String::with_capacity(40);
114 /// for bit in bit_source.take(40) {
115 /// if bit {
116 /// string.push('1');
117 /// } else {
118 /// string.push('0');
119 /// }
120 /// }
121 /// string
122 /// }
123 ///
124 /// let mut bit_source = StripedBitSource::new(EXAMPLE_SEED, 1000000, 1);
125 /// let mut strings = Vec::with_capacity(5);
126 /// for _ in 0..5 {
127 /// strings.push(generate_string(&mut bit_source));
128 /// bit_source.end_block();
129 /// }
130 /// assert_eq!(
131 /// strings,
132 /// &[
133 /// "0000000000000000000000000000000000000000",
134 /// "0000000000000000000000000000000000000000",
135 /// "0000000000000000000000000000000000000000",
136 /// "1111111111111111111111111111111111111111",
137 /// "0000000000000000000000000000000000000000"
138 /// ]
139 /// );
140 /// ```
141 pub const fn end_block(&mut self) {
142 self.first_bit_of_block = true;
143 }
144
145 /// Sets the previous bit of a `StripedBitSource`. This will affect the probability of the next
146 /// bit.
147 ///
148 /// # Expected complexity
149 /// Constant time and additional memory.
150 ///
151 /// # Examples
152 /// ```
153 /// use malachite_base::num::random::striped::StripedBitSource;
154 /// use malachite_base::random::EXAMPLE_SEED;
155 ///
156 /// fn generate_string(bit_source: &mut StripedBitSource) -> String {
157 /// let mut string = String::with_capacity(40);
158 /// for bit in bit_source.take(40) {
159 /// if bit {
160 /// string.push('1');
161 /// } else {
162 /// string.push('0');
163 /// }
164 /// }
165 /// string
166 /// }
167 ///
168 /// let mut bit_source = StripedBitSource::new(EXAMPLE_SEED, 1000000, 1);
169 /// bit_source.next();
170 /// let mut strings = Vec::with_capacity(3);
171 /// bit_source.set_previous_bit(true);
172 /// strings.push(generate_string(&mut bit_source));
173 /// bit_source.set_previous_bit(false);
174 /// strings.push(generate_string(&mut bit_source));
175 /// bit_source.set_previous_bit(true);
176 /// strings.push(generate_string(&mut bit_source));
177 /// assert_eq!(
178 /// strings,
179 /// &[
180 /// "1111111111111111111111111111111111111111",
181 /// "0000000000000000000000000000000000000000",
182 /// "1111111111111111111111111111111111111111",
183 /// ]
184 /// );
185 /// ```
186 pub const fn set_previous_bit(&mut self, bit: bool) {
187 self.previous_bit = bit;
188 }
189}
190
191/// Generates random unsigned integers from a random striped distribution.
192///
193/// This `struct` is created by [`striped_random_unsigned_bit_chunks`]; see its documentation for
194/// more.
195#[derive(Clone, Debug)]
196pub struct StripedRandomUnsignedBitChunks<T: PrimitiveUnsigned> {
197 phantom: PhantomData<*const T>,
198 bits: StripedBitSource,
199 chunk_size: usize,
200}
201
202impl<T: PrimitiveUnsigned> Iterator for StripedRandomUnsignedBitChunks<T> {
203 type Item = T;
204
205 fn next(&mut self) -> Option<T> {
206 self.bits.end_block();
207 let mut x = T::ZERO;
208 for bit in (&mut self.bits).take(self.chunk_size) {
209 x <<= 1;
210 if bit {
211 x |= T::ONE;
212 }
213 }
214 Some(x)
215 }
216}
217
218/// Generates random signed integers from a random striped distribution.
219///
220/// This `struct` is created by [`striped_random_signeds`]; see its documentation for more.
221#[derive(Clone, Debug)]
222pub struct StripedRandomSigneds<T: PrimitiveSigned> {
223 phantom: PhantomData<*const T>,
224 bits: StripedBitSource,
225 bs: RandomBools,
226}
227
228impl<T: PrimitiveSigned> Iterator for StripedRandomSigneds<T> {
229 type Item = T;
230
231 fn next(&mut self) -> Option<T> {
232 self.bits.end_block();
233 let mut x = T::ZERO;
234 for bit in (&mut self.bits).take(usize::wrapping_from(T::WIDTH) - 1) {
235 x <<= 1;
236 if bit {
237 x |= T::ONE;
238 }
239 }
240 if self.bs.next().unwrap() {
241 x.set_bit(T::WIDTH - 1);
242 }
243 Some(x)
244 }
245}
246
247/// Generates random natural (non-negative) signed integers from a random striped distribution.
248///
249/// This `struct` is created by [`striped_random_natural_signeds`]; see its documentation for more.
250#[derive(Clone, Debug)]
251pub struct StripedRandomNaturalSigneds<T: PrimitiveSigned> {
252 phantom: PhantomData<*const T>,
253 bits: StripedBitSource,
254}
255
256impl<T: PrimitiveSigned> Iterator for StripedRandomNaturalSigneds<T> {
257 type Item = T;
258
259 fn next(&mut self) -> Option<T> {
260 self.bits.end_block();
261 let mut x = T::ZERO;
262 for bit in (&mut self.bits).take(usize::wrapping_from(T::WIDTH) - 1) {
263 x <<= 1;
264 if bit {
265 x |= T::ONE;
266 }
267 }
268 Some(x)
269 }
270}
271
272/// Generates random negative signed integers from a random striped distribution.
273///
274/// This `struct` is created by [`striped_random_negative_signeds`]; see its documentation for more.
275#[derive(Clone, Debug)]
276pub struct StripedRandomNegativeSigneds<T: PrimitiveSigned> {
277 phantom: PhantomData<*const T>,
278 bits: StripedBitSource,
279}
280
281impl<T: PrimitiveSigned> Iterator for StripedRandomNegativeSigneds<T> {
282 type Item = T;
283
284 fn next(&mut self) -> Option<T> {
285 self.bits.end_block();
286 let mut x = T::ZERO;
287 for bit in (&mut self.bits).take(usize::wrapping_from(T::WIDTH) - 1) {
288 x <<= 1;
289 if bit {
290 x |= T::ONE;
291 }
292 }
293 x.set_bit(T::WIDTH - 1);
294 Some(x)
295 }
296}
297
298/// Generates random unsigned integers from a random striped distribution.
299///
300/// See [here](self) for more information.
301///
302/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
303/// m_denominator`.
304///
305/// The output length is infinite.
306///
307/// # Expected complexity per iteration
308/// $T(n) = O(n)$
309///
310/// $M(n) = O(n)$
311///
312/// where $T$ is time, $M$ is additional memory, and $n$ is the width of the type.
313///
314/// # Panics
315/// Panics if `m_denominator` is zero or if m_numerator <= m_denominator.
316///
317/// # Examples
318/// ```
319/// use malachite_base::iterators::prefix_to_string;
320/// use malachite_base::num::random::striped::striped_random_unsigneds;
321/// use malachite_base::random::EXAMPLE_SEED;
322/// use malachite_base::strings::ToBinaryString;
323///
324/// assert_eq!(
325/// prefix_to_string(
326/// striped_random_unsigneds::<u8>(EXAMPLE_SEED, 4, 1).map(|x| x.to_binary_string()),
327/// 10
328/// ),
329/// "[1, 1001100, 1111111, 11000011, 0, 10000000, 1111, 1110110, 0, 11111000, ...]"
330/// )
331/// ```
332#[inline]
333pub fn striped_random_unsigneds<T: PrimitiveUnsigned>(
334 seed: Seed,
335 m_numerator: u64,
336 m_denominator: u64,
337) -> StripedRandomUnsignedBitChunks<T> {
338 striped_random_unsigned_bit_chunks(seed, T::WIDTH, m_numerator, m_denominator)
339}
340
341/// Generates random positive unsigned integers from a random striped distribution.
342///
343/// See [here](self) for more information.
344///
345/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
346/// m_denominator`.
347///
348/// The output length is infinite.
349///
350/// # Expected complexity per iteration
351/// $T(n) = O(n)$
352///
353/// $M(n) = O(n)$
354///
355/// where $T$ is time, $M$ is additional memory, and $n$ is the width of the type.
356///
357/// # Panics
358/// Panics if `m_denominator` is zero or if m_numerator <= m_denominator.
359///
360/// # Examples
361/// ```
362/// use malachite_base::iterators::prefix_to_string;
363/// use malachite_base::num::random::striped::striped_random_positive_unsigneds;
364/// use malachite_base::random::EXAMPLE_SEED;
365/// use malachite_base::strings::ToBinaryString;
366///
367/// assert_eq!(
368/// prefix_to_string(
369/// striped_random_positive_unsigneds::<u8>(EXAMPLE_SEED, 4, 1)
370/// .map(|x| x.to_binary_string()),
371/// 10
372/// ),
373/// "[1, 1001100, 1111111, 11000011, 10000000, 1111, 1110110, 11111000, 11111111, 11111101, \
374/// ...]"
375/// )
376/// ```
377#[inline]
378pub fn striped_random_positive_unsigneds<T: PrimitiveUnsigned>(
379 seed: Seed,
380 m_numerator: u64,
381 m_denominator: u64,
382) -> NonzeroValues<StripedRandomUnsignedBitChunks<T>> {
383 nonzero_values(striped_random_unsigneds(seed, m_numerator, m_denominator))
384}
385
386/// Generates random signed integers from a random striped distribution.
387///
388/// See [here](self) for more information.
389///
390/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
391/// m_denominator`.
392///
393/// The output length is infinite.
394///
395/// # Expected complexity per iteration
396/// $T(n) = O(n)$
397///
398/// $M(n) = O(n)$
399///
400/// where $T$ is time, $M$ is additional memory, and $n$ is the width of the type.
401///
402/// # Panics
403/// Panics if `m_denominator` is zero or if m_numerator <= m_denominator.
404///
405/// # Examples
406/// ```
407/// use malachite_base::iterators::prefix_to_string;
408/// use malachite_base::num::random::striped::striped_random_signeds;
409/// use malachite_base::random::EXAMPLE_SEED;
410/// use malachite_base::strings::ToBinaryString;
411///
412/// assert_eq!(
413/// prefix_to_string(
414/// striped_random_signeds::<i8>(EXAMPLE_SEED, 4, 1).map(|x| x.to_binary_string()),
415/// 10
416/// ),
417/// "[1100001, 1000000, 1100000, 10000111, 1111, 10000001, 1111000, 100011, 111101, 11111100, \
418/// ...]"
419/// )
420/// ```
421pub fn striped_random_signeds<T: PrimitiveSigned>(
422 seed: Seed,
423 m_numerator: u64,
424 m_denominator: u64,
425) -> StripedRandomSigneds<T> {
426 StripedRandomSigneds {
427 phantom: PhantomData,
428 bits: StripedBitSource::new(seed.fork("bits"), m_numerator, m_denominator),
429 bs: random_bools(seed.fork("bs")),
430 }
431}
432
433/// Generates random natural (non-negative) signed integers from a random striped distribution.
434///
435/// See [here](self) for more information.
436///
437/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
438/// m_denominator`.
439///
440/// The output length is infinite.
441///
442/// # Expected complexity per iteration
443/// $T(n) = O(n)$
444///
445/// $M(n) = O(n)$
446///
447/// where $T$ is time, $M$ is additional memory, and $n$ is the width of the type.
448///
449/// # Panics
450/// Panics if `m_denominator` is zero or if m_numerator <= m_denominator.
451///
452/// # Examples
453/// ```
454/// use malachite_base::iterators::prefix_to_string;
455/// use malachite_base::num::random::striped::striped_random_natural_signeds;
456/// use malachite_base::random::EXAMPLE_SEED;
457/// use malachite_base::strings::ToBinaryString;
458///
459/// assert_eq!(
460/// prefix_to_string(
461/// striped_random_natural_signeds::<i8>(EXAMPLE_SEED, 4, 1).map(|x| x.to_binary_string()),
462/// 10
463/// ),
464/// "[0, 101100, 110000, 1111100, 1111, 1111110, 0, 111, 11101, 1100000, ...]"
465/// )
466/// ```
467pub fn striped_random_natural_signeds<T: PrimitiveSigned>(
468 seed: Seed,
469 m_numerator: u64,
470 m_denominator: u64,
471) -> StripedRandomNaturalSigneds<T> {
472 StripedRandomNaturalSigneds {
473 phantom: PhantomData,
474 bits: StripedBitSource::new(seed, m_numerator, m_denominator),
475 }
476}
477
478/// Generates random positive signed integers from a random striped distribution.
479///
480/// See [here](self) for more information.
481///
482/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
483/// m_denominator`.
484///
485/// The output length is infinite.
486///
487/// # Expected complexity per iteration
488/// $T(n) = O(n)$
489///
490/// $M(n) = O(n)$
491///
492/// where $T$ is time, $M$ is additional memory, and $n$ is the width of the type.
493///
494/// # Panics
495/// Panics if `m_denominator` is zero or if m_numerator <= m_denominator.
496///
497/// # Examples
498/// ```
499/// use malachite_base::iterators::prefix_to_string;
500/// use malachite_base::num::random::striped::striped_random_positive_signeds;
501/// use malachite_base::random::EXAMPLE_SEED;
502/// use malachite_base::strings::ToBinaryString;
503///
504/// assert_eq!(
505/// prefix_to_string(
506/// striped_random_positive_signeds::<i8>(EXAMPLE_SEED, 4, 1).map(|x| x.to_binary_string()),
507/// 10
508/// ),
509/// "[101100, 110000, 1111100, 1111, 1111110, 111, 11101, 1100000, 1111111, 1100000, ...]"
510/// )
511/// ```
512#[inline]
513pub fn striped_random_positive_signeds<T: PrimitiveSigned>(
514 seed: Seed,
515 m_numerator: u64,
516 m_denominator: u64,
517) -> NonzeroValues<StripedRandomNaturalSigneds<T>> {
518 nonzero_values(striped_random_natural_signeds(
519 seed,
520 m_numerator,
521 m_denominator,
522 ))
523}
524
525/// Generates random negative signed integers from a random striped distribution.
526///
527/// See [here](self) for more information.
528///
529/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
530/// m_denominator`.
531///
532/// The output length is infinite.
533///
534/// # Expected complexity per iteration
535/// $T(n) = O(n)$
536///
537/// $M(n) = O(n)$
538///
539/// where $T$ is time, $M$ is additional memory, and $n$ is the width of the type.
540///
541/// # Panics
542/// Panics if `m_denominator` is zero or if m_numerator <= m_denominator.
543///
544/// # Examples
545/// ```
546/// use malachite_base::iterators::prefix_to_string;
547/// use malachite_base::num::random::striped::striped_random_negative_signeds;
548/// use malachite_base::random::EXAMPLE_SEED;
549/// use malachite_base::strings::ToBinaryString;
550///
551/// assert_eq!(
552/// prefix_to_string(
553/// striped_random_negative_signeds::<i8>(EXAMPLE_SEED, 4, 1).map(|x| x.to_binary_string()),
554/// 10
555/// ),
556/// "[10000000, 10101100, 10110000, 11111100, 10001111, 11111110, 10000000, 10000111, \
557/// 10011101, 11100000, ...]"
558/// )
559/// ```
560pub fn striped_random_negative_signeds<T: PrimitiveSigned>(
561 seed: Seed,
562 m_numerator: u64,
563 m_denominator: u64,
564) -> StripedRandomNegativeSigneds<T> {
565 StripedRandomNegativeSigneds {
566 phantom: PhantomData,
567 bits: StripedBitSource::new(seed, m_numerator, m_denominator),
568 }
569}
570
571/// Generates random nonzero signed integers from a random striped distribution.
572///
573/// See [here](self) for more information.
574///
575/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
576/// m_denominator`.
577///
578/// The output length is infinite.
579///
580/// # Expected complexity per iteration
581/// $T(n) = O(n)$
582///
583/// $M(n) = O(n)$
584///
585/// where $T$ is time, $M$ is additional memory, and $n$ is the width of the type.
586///
587/// # Panics
588/// Panics if `m_denominator` is zero or if m_numerator <= m_denominator.
589///
590/// # Examples
591/// ```
592/// use malachite_base::iterators::prefix_to_string;
593/// use malachite_base::num::random::striped::striped_random_nonzero_signeds;
594/// use malachite_base::random::EXAMPLE_SEED;
595/// use malachite_base::strings::ToBinaryString;
596///
597/// assert_eq!(
598/// prefix_to_string(
599/// striped_random_nonzero_signeds::<i8>(EXAMPLE_SEED, 4, 1).map(|x| x.to_binary_string()),
600/// 10
601/// ),
602/// "[1100001, 1000000, 1100000, 10000111, 1111, 10000001, 1111000, 100011, 111101, 11111100, \
603/// ...]"
604/// )
605/// ```
606#[inline]
607pub fn striped_random_nonzero_signeds<T: PrimitiveSigned>(
608 seed: Seed,
609 m_numerator: u64,
610 m_denominator: u64,
611) -> NonzeroValues<StripedRandomSigneds<T>> {
612 nonzero_values(striped_random_signeds(seed, m_numerator, m_denominator))
613}
614
615/// Generates random unsigned integers of up to `chunk_size` bits from a random striped
616/// distribution.
617///
618/// See [here](self) for more information.
619///
620/// The mean run length (before the bit sequences are truncated) is $m$ = `m_numerator /
621/// m_denominator`.
622///
623/// The output length is infinite.
624///
625/// # Expected complexity per iteration
626/// $T(n) = O(n)$
627///
628/// $M(n) = O(n)$
629///
630/// where $T$ is time, $M$ is additional memory, and $n$ is `chunk_size`.
631///
632/// # Panics
633/// Panics if `m_denominator` is zero, if m_numerator <= m_denominator, or if `chunk_size` is
634/// greater than the width of the type.
635///
636/// # Examples
637/// ```
638/// use malachite_base::iterators::prefix_to_string;
639/// use malachite_base::num::random::striped::striped_random_unsigned_bit_chunks;
640/// use malachite_base::random::EXAMPLE_SEED;
641/// use malachite_base::strings::ToBinaryString;
642///
643/// assert_eq!(
644/// prefix_to_string(
645/// striped_random_unsigned_bit_chunks::<u8>(EXAMPLE_SEED, 3, 4, 1)
646/// .map(|x| x.to_binary_string()),
647/// 10
648/// ),
649/// "[0, 0, 0, 101, 11, 100, 11, 11, 0, 111, ...]"
650/// )
651/// ```
652pub fn striped_random_unsigned_bit_chunks<T: PrimitiveUnsigned>(
653 seed: Seed,
654 chunk_size: u64,
655 m_numerator: u64,
656 m_denominator: u64,
657) -> StripedRandomUnsignedBitChunks<T> {
658 assert!(chunk_size <= T::WIDTH);
659 StripedRandomUnsignedBitChunks {
660 phantom: PhantomData,
661 bits: StripedBitSource::new(seed, m_numerator, m_denominator),
662 chunk_size: usize::exact_from(chunk_size),
663 }
664}
665
666/// Generates a striped `Vec<bool>`, with a given length, from a [`StripedBitSource`].
667///
668/// See [here](self) for more information.
669///
670/// The output length is `len`.
671///
672/// # Expected complexity
673/// $T(n) = O(n)$
674///
675/// $M(n) = O(n)$
676///
677/// where $T$ is time, $M$ is additional memory, and $n$ is `len`.
678///
679/// # Examples
680/// ```
681/// use malachite_base::num::random::striped::{StripedBitSource, get_striped_bool_vec};
682/// use malachite_base::random::EXAMPLE_SEED;
683///
684/// let mut bit_source = StripedBitSource::new(EXAMPLE_SEED, 10, 1);
685/// let bits: String = get_striped_bool_vec(&mut bit_source, 50)
686/// .into_iter()
687/// .map(|b| if b { '1' } else { '0' })
688/// .collect();
689/// assert_eq!(bits, "00011111111111000000011111111111111000000000001111");
690/// ```
691pub fn get_striped_bool_vec(bit_source: &mut StripedBitSource, len: u64) -> Vec<bool> {
692 bit_source.end_block();
693 bit_source.take(usize::exact_from(len)).collect()
694}
695
696/// Generates random striped `Vec<bool>`s.
697#[derive(Clone, Debug)]
698pub struct StripedRandomBoolVecs<I: Iterator<Item = u64>> {
699 lengths: I,
700 bit_source: StripedBitSource,
701}
702
703impl<I: Iterator<Item = u64>> Iterator for StripedRandomBoolVecs<I> {
704 type Item = Vec<bool>;
705
706 fn next(&mut self) -> Option<Vec<bool>> {
707 Some(get_striped_bool_vec(
708 &mut self.bit_source,
709 self.lengths.next().unwrap(),
710 ))
711 }
712}
713
714/// Generates random striped `Vec<bool>`s, with lengths from an iterator.
715///
716/// See [here](self) for more information.
717///
718/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
719/// mean_stripe_denominator`.
720///
721/// # Expected complexity per iteration
722/// $T(i) = O(\ell + T^\prime(i))$
723///
724/// $M(i) = O(\ell + M^\prime(i))$
725///
726/// where $T$ is time, $M$ is additional memory, $i$ is the iteration number, $T^\prime$ and
727/// $M^\prime$ are the time and memory functions of `lengths`, and $\ell$ is the number of elements
728/// in the $i$th output.
729///
730/// # Panics
731/// Panics if `mean_stripe_denominator` is zero or if `mean_stripe_numerator <=
732/// mean_stripe_denominator`.
733///
734/// # Examples
735/// ```
736/// use malachite_base::iterators::prefix_to_string;
737/// use malachite_base::num::random::striped::striped_random_bool_vecs_from_length_iterator;
738/// use malachite_base::random::EXAMPLE_SEED;
739/// use malachite_base::vecs::random_values_from_vec;
740///
741/// assert_eq!(
742/// prefix_to_string(
743/// striped_random_bool_vecs_from_length_iterator(
744/// EXAMPLE_SEED,
745/// &|seed| random_values_from_vec(seed, vec![0, 2, 4]),
746/// 10,
747/// 1,
748/// )
749/// .map(|bs| bs
750/// .into_iter()
751/// .map(|b| if b { '1' } else { '0' })
752/// .collect::<String>()),
753/// 20
754/// ),
755/// "[00, 0000, 00, 0000, 0000, 11, , 00, , 1111, 0001, 11, 1100, 00, 0000, 0000, 1110, , \
756/// 0000, , ...]"
757/// );
758/// ```
759#[inline]
760pub fn striped_random_bool_vecs_from_length_iterator<I: Iterator<Item = u64>>(
761 seed: Seed,
762 lengths_gen: &dyn Fn(Seed) -> I,
763 mean_stripe_numerator: u64,
764 mean_stripe_denominator: u64,
765) -> StripedRandomBoolVecs<I> {
766 StripedRandomBoolVecs {
767 lengths: lengths_gen(seed.fork("lengths")),
768 bit_source: StripedBitSource::new(
769 seed.fork("bit_source"),
770 mean_stripe_numerator,
771 mean_stripe_denominator,
772 ),
773 }
774}
775
776/// Generates random striped `Vec<bool>`s of a given length.
777///
778/// See [here](self) for more information.
779///
780/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
781/// mean_stripe_denominator`.
782///
783/// If `len` is 0, the output consists of the empty list, repeated.
784///
785/// # Expected complexity per iteration
786/// $T(n) = O(n)$
787///
788/// $M(n) = O(n)$
789///
790/// where $T$ is time, $M$ is additional memory, and $n$ is `len`.
791///
792/// # Examples
793/// ```
794/// use malachite_base::iterators::prefix_to_string;
795/// use malachite_base::num::random::striped::striped_random_fixed_length_bool_vecs;
796/// use malachite_base::random::EXAMPLE_SEED;
797///
798/// assert_eq!(
799/// prefix_to_string(
800/// striped_random_fixed_length_bool_vecs(EXAMPLE_SEED, 5, 10, 1).map(|bs| bs
801/// .into_iter()
802/// .map(|b| if b { '1' } else { '0' })
803/// .collect::<String>()),
804/// 20
805/// ),
806/// "[00000, 00000, 00000, 00000, 00011, 11000, 00000, 11111, 01111, 11111, 10000, 00011, \
807/// 00000, 00000, 11000, 00000, 11111, 00000, 00000, 11111, ...]"
808/// );
809/// ```
810#[inline]
811pub fn striped_random_fixed_length_bool_vecs(
812 seed: Seed,
813 len: u64,
814 mean_stripe_numerator: u64,
815 mean_stripe_denominator: u64,
816) -> StripedRandomBoolVecs<Repeat<u64>> {
817 striped_random_bool_vecs_from_length_iterator(
818 seed,
819 &|_| repeat(len),
820 mean_stripe_numerator,
821 mean_stripe_denominator,
822 )
823}
824
825/// Generates random striped `Vec<bool>`s.
826///
827/// See [here](self) for more information.
828///
829/// The lengths of the [`Vec`]s are sampled from a geometric distribution with a specified mean $m$,
830/// equal to `mean_length_numerator / mean_length_denominator`. $m$ must be greater than 0.
831///
832/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
833/// mean_stripe_denominator`.
834///
835/// # Expected complexity per iteration
836/// $T(n) = O(n)$
837///
838/// $M(n) = O(n)$
839///
840/// where $T$ is time, $M$ is additional memory, and $n$ is `mean_length_numerator /
841/// mean_length_denominator`.
842///
843/// # Panics
844/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
845/// mean_stripe_denominator`, if `mean_length_numerator` or `mean_length_denominator` are zero, or,
846/// if after being reduced to lowest terms, their sum is greater than or equal to $2^{64}$.
847///
848/// # Examples
849/// ```
850/// use malachite_base::iterators::prefix_to_string;
851/// use malachite_base::num::random::striped::striped_random_bool_vecs;
852/// use malachite_base::random::EXAMPLE_SEED;
853///
854/// assert_eq!(
855/// prefix_to_string(
856/// striped_random_bool_vecs(EXAMPLE_SEED, 10, 1, 2, 1).map(|bs| bs
857/// .into_iter()
858/// .map(|b| if b { '1' } else { '0' })
859/// .collect::<String>()),
860/// 20
861/// ),
862/// "[000000, 0, 00000000, 0, 00000001110000, , 11111, 0000, 1, , 011111, 11, , , 1, 000, , \
863/// 0, , 0, ...]"
864/// );
865/// ```
866#[inline]
867pub fn striped_random_bool_vecs(
868 seed: Seed,
869 mean_stripe_numerator: u64,
870 mean_stripe_denominator: u64,
871 mean_length_numerator: u64,
872 mean_length_denominator: u64,
873) -> StripedRandomBoolVecs<GeometricRandomNaturalValues<u64>> {
874 striped_random_bool_vecs_from_length_iterator(
875 seed,
876 &|seed_2| {
877 geometric_random_unsigneds(seed_2, mean_length_numerator, mean_length_denominator)
878 },
879 mean_stripe_numerator,
880 mean_stripe_denominator,
881 )
882}
883
884/// Generates random striped `Vec<bool>`s, with a minimum length.
885///
886/// See [here](self) for more information.
887///
888/// The lengths of the [`Vec`]s are sampled from a geometric distribution with a specified mean $m$,
889/// equal to `mean_length_numerator / mean_length_denominator`. $m$ must be greater than
890/// `min_length`.
891///
892/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
893/// mean_stripe_denominator`.
894///
895/// # Expected complexity per iteration
896/// $T(n) = O(n)$
897///
898/// $M(n) = O(n)$
899///
900/// where $T$ is time, $M$ is additional memory, and $n$ is `mean_length_numerator /
901/// mean_length_denominator`.
902///
903/// # Panics
904/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
905/// mean_stripe_denominator`, if `mean_length_numerator` or `mean_length_denominator` are zero, if
906/// their ratio is less than or equal to `min_length`, or if they are too large and manipulating
907/// them leads to arithmetic overflow.
908///
909/// # Examples
910/// ```
911/// use malachite_base::iterators::prefix_to_string;
912/// use malachite_base::num::random::striped::striped_random_bool_vecs_min_length;
913/// use malachite_base::random::EXAMPLE_SEED;
914///
915/// assert_eq!(
916/// prefix_to_string(
917/// striped_random_bool_vecs_min_length(EXAMPLE_SEED, 3, 10, 1, 5, 1).map(|bs| bs
918/// .into_iter()
919/// .map(|b| if b { '1' } else { '0' })
920/// .collect::<String>()),
921/// 20
922/// ),
923/// "[000000000, 0000, 00000000111, 0111, 00000000011111111, 100, 00000111, 1111111, 0001, \
924/// 111, 111111111, 00000, 000, 000, 1111, 000000, 111, 0011, 000, 1111, ...]"
925/// );
926/// ```
927#[inline]
928pub fn striped_random_bool_vecs_min_length(
929 seed: Seed,
930 min_length: u64,
931 mean_stripe_numerator: u64,
932 mean_stripe_denominator: u64,
933 mean_length_numerator: u64,
934 mean_length_denominator: u64,
935) -> StripedRandomBoolVecs<GeometricRandomNaturalValues<u64>> {
936 striped_random_bool_vecs_from_length_iterator(
937 seed,
938 &|seed_2| {
939 geometric_random_unsigned_inclusive_range(
940 seed_2,
941 min_length,
942 u64::MAX,
943 mean_length_numerator,
944 mean_length_denominator,
945 )
946 },
947 mean_stripe_numerator,
948 mean_stripe_denominator,
949 )
950}
951
952/// Generates random striped `Vec<bool>`s, with lengths in $[a, b)$.
953///
954/// See [here](self) for more information.
955///
956/// The lengths of the [`Vec`]s are sampled from a uniform distribution on $[a, b)$. $a$ must be
957/// less than $b$.
958///
959/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
960/// mean_stripe_denominator`.
961///
962/// $$
963/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
964/// \frac{1}{b-a}\prod_{i=0}^{n-1}P(x_i) & \text{if} \\quad a \leq n < b, \\\\
965/// 0 & \\text{otherwise}.
966/// \\end{cases}
967/// $$
968///
969/// # Expected complexity per iteration
970/// $T(b) = O(b)$
971///
972/// $M(b) = O(b)$
973///
974/// where $T$ is time, $M$ is additional memory, and $b$ is `b`.
975///
976/// # Panics
977/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
978/// mean_stripe_denominator`, or if $a \geq b$.
979///
980/// # Examples
981/// ```
982/// use malachite_base::iterators::prefix_to_string;
983/// use malachite_base::num::random::striped::striped_random_bool_vecs_length_range;
984/// use malachite_base::random::EXAMPLE_SEED;
985///
986/// assert_eq!(
987/// prefix_to_string(
988/// striped_random_bool_vecs_length_range(EXAMPLE_SEED, 4, 10, 10, 1).map(|bs| bs
989/// .into_iter()
990/// .map(|b| if b { '1' } else { '0' })
991/// .collect::<String>()),
992/// 20
993/// ),
994/// "[000000000, 000000000, 000111000, 000000000, 0111, 11111, 00111111, 1000000, 00000011, \
995/// 111111111, 111111, 00000000, 00000000, 001111, 111111111, 000000000, 110000, 0001111, \
996/// 0000000, 111101111, ...]"
997/// );
998/// ```
999#[inline]
1000pub fn striped_random_bool_vecs_length_range(
1001 seed: Seed,
1002 a: u64,
1003 b: u64,
1004 mean_stripe_numerator: u64,
1005 mean_stripe_denominator: u64,
1006) -> StripedRandomBoolVecs<RandomUnsignedRange<u64>> {
1007 striped_random_bool_vecs_from_length_iterator(
1008 seed,
1009 &|seed_2| random_unsigned_range(seed_2, a, b),
1010 mean_stripe_numerator,
1011 mean_stripe_denominator,
1012 )
1013}
1014
1015/// Generates random striped `Vec<bool>`s, with lengths in $[a, b]$.
1016///
1017/// See [here](self) for more information.
1018///
1019/// The lengths of the [`Vec`]s are sampled from a uniform distribution on $[a, b]$. $a$ must be
1020/// less than $b$.
1021///
1022/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1023/// mean_stripe_denominator`.
1024///
1025/// $$
1026/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
1027/// \frac{1}{b-a+1}\prod_{i=0}^{n-1}P(x_i) & \text{if} \\quad a \leq n \leq b, \\\\
1028/// 0 & \\text{otherwise}.
1029/// \\end{cases}
1030/// $$
1031///
1032/// # Expected complexity per iteration
1033/// $T(b) = O(b)$
1034///
1035/// $M(b) = O(b)$
1036///
1037/// where $T$ is time, $M$ is additional memory, and $b$ is `b`.
1038///
1039/// # Panics
1040/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1041/// mean_stripe_denominator`, or if $a \geq b$.
1042///
1043/// # Examples
1044/// ```
1045/// use malachite_base::iterators::prefix_to_string;
1046/// use malachite_base::num::random::striped::striped_random_bool_vecs_length_inclusive_range;
1047/// use malachite_base::random::EXAMPLE_SEED;
1048///
1049/// assert_eq!(
1050/// prefix_to_string(
1051/// striped_random_bool_vecs_length_inclusive_range(EXAMPLE_SEED, 4, 9, 10, 1).map(|bs| bs
1052/// .into_iter()
1053/// .map(|b| if b { '1' } else { '0' })
1054/// .collect::<String>()),
1055/// 20
1056/// ),
1057/// "[000000000, 000000000, 000111000, 000000000, 0111, 11111, 00111111, 1000000, 00000011, \
1058/// 111111111, 111111, 00000000, 00000000, 001111, 111111111, 000000000, 110000, 0001111, \
1059/// 0000000, 111101111, ...]"
1060/// );
1061/// ```
1062#[inline]
1063pub fn striped_random_bool_vecs_length_inclusive_range(
1064 seed: Seed,
1065 a: u64,
1066 b: u64,
1067 mean_stripe_numerator: u64,
1068 mean_stripe_denominator: u64,
1069) -> StripedRandomBoolVecs<RandomUnsignedInclusiveRange<u64>> {
1070 striped_random_bool_vecs_from_length_iterator(
1071 seed,
1072 &|seed_2| random_unsigned_inclusive_range(seed_2, a, b),
1073 mean_stripe_numerator,
1074 mean_stripe_denominator,
1075 )
1076}
1077
1078/// Generates a striped unsigned [`Vec`], with a given number of bits (not length!), from a
1079/// [`StripedBitSource`].
1080///
1081/// See [here](self) for more information.
1082///
1083/// The output length is `bit_len.div_round(T::WIDTH, Ceiling)`.
1084///
1085/// # Expected complexity
1086/// $T(n) = O(n)$
1087///
1088/// $M(n) = O(n)$
1089///
1090/// where $T$ is time, $M$ is additional memory, and $n$ is `bit_len`.
1091///
1092/// # Examples
1093/// ```
1094/// use itertools::Itertools;
1095/// use malachite_base::num::random::striped::{StripedBitSource, get_striped_unsigned_vec};
1096/// use malachite_base::random::EXAMPLE_SEED;
1097/// use malachite_base::strings::ToBinaryString;
1098///
1099/// let mut bit_source = StripedBitSource::new(EXAMPLE_SEED, 10, 1);
1100/// let xs = get_striped_unsigned_vec::<u8>(&mut bit_source, 100)
1101/// .iter()
1102/// .map(u8::to_binary_string)
1103/// .collect_vec();
1104/// assert_eq!(
1105/// xs,
1106/// &[
1107/// "11111000", "111111", "11100000", "11111111", "111", "11000000", "11111111", "0", "0",
1108/// "11111000", "11111111", "11111111", "11",
1109/// ]
1110/// );
1111/// ```
1112pub fn get_striped_unsigned_vec<T: PrimitiveUnsigned>(
1113 bit_source: &mut StripedBitSource,
1114 bit_len: u64,
1115) -> Vec<T> {
1116 bit_source.end_block();
1117 bit_source
1118 .take(usize::exact_from(bit_len))
1119 .chunks(usize::wrapping_from(T::WIDTH))
1120 .into_iter()
1121 .map(T::from_bits_asc)
1122 .collect()
1123}
1124
1125/// Generates random striped [`Vec`]s of unsigneds.
1126#[derive(Clone, Debug)]
1127pub struct StripedRandomUnsignedVecs<T: PrimitiveUnsigned, I: Iterator<Item = u64>> {
1128 phantom: PhantomData<*const T>,
1129 lengths: I,
1130 bit_source: StripedBitSource,
1131}
1132
1133impl<T: PrimitiveUnsigned, I: Iterator<Item = u64>> Iterator for StripedRandomUnsignedVecs<T, I> {
1134 type Item = Vec<T>;
1135
1136 fn next(&mut self) -> Option<Vec<T>> {
1137 Some(get_striped_unsigned_vec(
1138 &mut self.bit_source,
1139 self.lengths.next().unwrap() << T::LOG_WIDTH,
1140 ))
1141 }
1142}
1143
1144/// Generates random striped [`Vec`]s of unsigneds, with lengths from an iterator.
1145///
1146/// See [here](self) for more information.
1147///
1148/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1149/// mean_stripe_denominator`.
1150///
1151/// # Expected complexity per iteration
1152/// $T(i) = O(\ell + T^\prime(i))$
1153///
1154/// $M(i) = O(\ell + M^\prime(i))$
1155///
1156/// where $T$ is time, $M$ is additional memory, $i$ is the iteration number, $T^\prime$ and
1157/// $M^\prime$ are the time and memory functions of `lengths`, and $\ell$ is the number of elements
1158/// in the $i$th output.
1159///
1160/// # Panics
1161/// Panics if `mean_stripe_denominator` is zero or if `mean_stripe_numerator <=
1162/// mean_stripe_denominator`.
1163///
1164/// # Examples
1165/// ```
1166/// use malachite_base::iterators::prefix_to_string;
1167/// use malachite_base::num::random::striped::striped_random_unsigned_vecs_from_length_iterator;
1168/// use malachite_base::random::EXAMPLE_SEED;
1169/// use malachite_base::strings::ToBinaryString;
1170/// use malachite_base::vecs::random_values_from_vec;
1171///
1172/// assert_eq!(
1173/// prefix_to_string(
1174/// striped_random_unsigned_vecs_from_length_iterator::<u8, _>(
1175/// EXAMPLE_SEED,
1176/// &|seed| random_values_from_vec(seed, vec![0, 2, 4]),
1177/// 10,
1178/// 1,
1179/// )
1180/// .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1181/// 10,
1182/// ),
1183/// "[[0, 0], [1110000, 0, 11111100, 11], [11111110, 1111], [0, 0, 0, 11111000], \
1184/// [0, 0, 1111110, 0], [11011111, 11111111], [], [11110000, 11111111], [], \
1185/// [11111111, 11000011, 11111, 0], ...]"
1186/// );
1187/// ```
1188#[inline]
1189pub fn striped_random_unsigned_vecs_from_length_iterator<
1190 T: PrimitiveUnsigned,
1191 I: Iterator<Item = u64>,
1192>(
1193 seed: Seed,
1194 lengths_gen: &dyn Fn(Seed) -> I,
1195 mean_stripe_numerator: u64,
1196 mean_stripe_denominator: u64,
1197) -> StripedRandomUnsignedVecs<T, I> {
1198 StripedRandomUnsignedVecs {
1199 phantom: PhantomData,
1200 lengths: lengths_gen(seed.fork("lengths")),
1201 bit_source: StripedBitSource::new(
1202 seed.fork("bit_source"),
1203 mean_stripe_numerator,
1204 mean_stripe_denominator,
1205 ),
1206 }
1207}
1208
1209/// Generates random striped unsigned [`Vec`]s of a given length.
1210///
1211/// See [here](self) for more information.
1212///
1213/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1214/// mean_stripe_denominator`.
1215///
1216/// If `len` is 0, the output consists of the empty list, repeated.
1217///
1218/// # Expected complexity per iteration
1219/// $T(n) = O(n)$
1220///
1221/// $M(n) = O(n)$
1222///
1223/// where $T$ is time, $M$ is additional memory, and $n$ is `len`.
1224///
1225/// # Examples
1226/// ```
1227/// use malachite_base::iterators::prefix_to_string;
1228/// use malachite_base::num::random::striped::striped_random_fixed_length_unsigned_vecs;
1229/// use malachite_base::random::EXAMPLE_SEED;
1230/// use malachite_base::strings::ToBinaryString;
1231///
1232/// assert_eq!(
1233/// prefix_to_string(
1234/// striped_random_fixed_length_unsigned_vecs::<u8>(EXAMPLE_SEED, 3, 10, 1).map(|xs| {
1235/// prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)
1236/// }),
1237/// 10,
1238/// ),
1239/// "[[0, 0, 111000], [0, 11111100, 11], [11111110, 1111, 0], [0, 0, 11111000], \
1240/// [0, 0, 1111110], [11111111, 11011111, 11111111], [11110000, 11111111, 11111111], \
1241/// [11000011, 11111, 0], [0, 10000000, 11111001], [11111111, 0, 0], ...]"
1242/// );
1243/// ```
1244#[inline]
1245pub fn striped_random_fixed_length_unsigned_vecs<T: PrimitiveUnsigned>(
1246 seed: Seed,
1247 len: u64,
1248 mean_stripe_numerator: u64,
1249 mean_stripe_denominator: u64,
1250) -> StripedRandomUnsignedVecs<T, Repeat<u64>> {
1251 striped_random_unsigned_vecs_from_length_iterator(
1252 seed,
1253 &|_| repeat(len),
1254 mean_stripe_numerator,
1255 mean_stripe_denominator,
1256 )
1257}
1258
1259/// Generates random striped [`Vec`]s of unsigneds.
1260///
1261/// See [here](self) for more information.
1262///
1263/// The lengths of the [`Vec`]s are sampled from a geometric distribution with a specified mean $m$,
1264/// equal to `mean_length_numerator / mean_length_denominator`. $m$ must be greater than 0.
1265///
1266/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1267/// mean_stripe_denominator`.
1268///
1269/// $$
1270/// P((x_0, x_1, \ldots, x_{n-1})) = \frac{m^n}{(m+1)^{n+1}}\prod_{i=0}^{n-1}P(x_i).
1271/// $$
1272///
1273/// # Expected complexity per iteration
1274/// $T(n) = O(n)$
1275///
1276/// $M(n) = O(n)$
1277///
1278/// where $T$ is time, $M$ is additional memory, and $n$ is `mean_length_numerator /
1279/// mean_length_denominator`.
1280///
1281/// # Panics
1282/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1283/// mean_stripe_denominator`, if `mean_length_numerator` or `mean_length_denominator` are zero, or,
1284/// if after being reduced to lowest terms, their sum is greater than or equal to $2^{64}$.
1285///
1286/// # Examples
1287/// ```
1288/// use malachite_base::iterators::prefix_to_string;
1289/// use malachite_base::num::random::striped::striped_random_unsigned_vecs;
1290/// use malachite_base::random::EXAMPLE_SEED;
1291/// use malachite_base::strings::ToBinaryString;
1292///
1293/// assert_eq!(
1294/// prefix_to_string(
1295/// striped_random_unsigned_vecs::<u8>(EXAMPLE_SEED, 10, 1, 2, 1).map(|xs| {
1296/// prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)
1297/// }),
1298/// 10,
1299/// ),
1300/// "[[0, 0, 111000, 0, 11111110, 10000001], [0], \
1301/// [11110000, 11111111, 11111111, 11111111, 11, 0, 10000000, 11111], [0], \
1302/// [10000, 0, 11111100, 11111111, 1111111, 11111000, 11, 0, 0, 10011000, 11111111, 111, 0, \
1303/// 0], [], [11111111, 11111111, 11111111, 11111111, 10111111], [0, 0, 0, 11110000], \
1304/// [11111111], [], ...]"
1305/// );
1306/// ```
1307#[inline]
1308pub fn striped_random_unsigned_vecs<T: PrimitiveUnsigned>(
1309 seed: Seed,
1310 mean_stripe_numerator: u64,
1311 mean_stripe_denominator: u64,
1312 mean_length_numerator: u64,
1313 mean_length_denominator: u64,
1314) -> StripedRandomUnsignedVecs<T, GeometricRandomNaturalValues<u64>> {
1315 striped_random_unsigned_vecs_from_length_iterator(
1316 seed,
1317 &|seed_2| {
1318 geometric_random_unsigneds(seed_2, mean_length_numerator, mean_length_denominator)
1319 },
1320 mean_stripe_numerator,
1321 mean_stripe_denominator,
1322 )
1323}
1324
1325/// Generates random striped [`Vec`]s of unsigneds, with a minimum length.
1326///
1327/// See [here](self) for more information.
1328///
1329/// The lengths of the [`Vec`]s are sampled from a geometric distribution with a specified mean $m$,
1330/// equal to `mean_length_numerator / mean_length_denominator`. $m$ must be greater than
1331/// `min_length`.
1332///
1333/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1334/// mean_stripe_denominator`.
1335///
1336/// $$
1337/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
1338/// \frac{(m-a)^{n-a}}{(m+1-a)^{n+1-a}}\prod_{i=0}^{n-1}P(x_i) & n \geq a \\\\
1339/// 0 & \\text{otherwise},
1340/// \\end{cases}
1341/// $$
1342/// where $a$ is `min_length`.
1343///
1344/// # Expected complexity per iteration
1345/// $T(n) = O(n)$
1346///
1347/// $M(n) = O(n)$
1348///
1349/// where $T$ is time, $M$ is additional memory, and $n$ is `mean_length_numerator /
1350/// mean_length_denominator`.
1351///
1352/// # Panics
1353/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1354/// mean_stripe_denominator`, if `mean_length_numerator` or `mean_length_denominator` are zero, if
1355/// their ratio is less than or equal to `min_length`, or if they are too large and manipulating
1356/// them leads to arithmetic overflow.
1357///
1358/// # Examples
1359/// ```
1360/// use malachite_base::iterators::prefix_to_string;
1361/// use malachite_base::num::random::striped::striped_random_unsigned_vecs_min_length;
1362/// use malachite_base::random::EXAMPLE_SEED;
1363/// use malachite_base::strings::ToBinaryString;
1364///
1365/// assert_eq!(
1366/// prefix_to_string(
1367/// striped_random_unsigned_vecs_min_length::<u8>(EXAMPLE_SEED, 2, 10, 1, 3, 1)
1368/// .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1369/// 10,
1370/// ),
1371/// "[[0, 0, 111000], [0, 11111100, 11, 11111111], \
1372/// [11110000, 11111111, 11111111, 11111111], [11111000, 11111111, 11111111, 11000000], \
1373/// [0, 10000, 0], [111, 0, 0, 1111], [11110000, 11111111], [11111111, 111111], \
1374/// [110, 10000000, 11111111], [11111111, 11111111], ...]"
1375/// );
1376/// ```
1377#[inline]
1378pub fn striped_random_unsigned_vecs_min_length<T: PrimitiveUnsigned>(
1379 seed: Seed,
1380 min_length: u64,
1381 mean_stripe_numerator: u64,
1382 mean_stripe_denominator: u64,
1383 mean_length_numerator: u64,
1384 mean_length_denominator: u64,
1385) -> StripedRandomUnsignedVecs<T, GeometricRandomNaturalValues<u64>> {
1386 striped_random_unsigned_vecs_from_length_iterator(
1387 seed,
1388 &|seed_2| {
1389 geometric_random_unsigned_inclusive_range(
1390 seed_2,
1391 min_length,
1392 u64::MAX,
1393 mean_length_numerator,
1394 mean_length_denominator,
1395 )
1396 },
1397 mean_stripe_numerator,
1398 mean_stripe_denominator,
1399 )
1400}
1401
1402/// Generates random striped [`Vec`]s of unsigneds, with lengths in $[a, b)$.
1403///
1404/// See [here](self) for more information.
1405///
1406/// The lengths of the [`Vec`]s are sampled from a uniform distribution on $[a, b)$. $a$ must be
1407/// less than $b$.
1408///
1409/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1410/// mean_stripe_denominator`.
1411///
1412/// $$
1413/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
1414/// \frac{1}{b-a}\prod_{i=0}^{n-1}P(x_i) & \text{if} \\quad a \leq n < b, \\\\
1415/// 0 & \\text{otherwise}.
1416/// \\end{cases}
1417/// $$
1418///
1419/// # Expected complexity per iteration
1420/// $T(b) = O(b)$
1421///
1422/// $M(b) = O(b)$
1423///
1424/// where $T$ is time, $M$ is additional memory, and $b$ is `b`.
1425///
1426/// # Panics
1427/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1428/// mean_stripe_denominator`, or if $a \geq b$.
1429///
1430/// # Examples
1431/// ```
1432/// use malachite_base::iterators::prefix_to_string;
1433/// use malachite_base::num::random::striped::striped_random_unsigned_vecs_length_range;
1434/// use malachite_base::random::EXAMPLE_SEED;
1435/// use malachite_base::strings::ToBinaryString;
1436///
1437/// assert_eq!(
1438/// prefix_to_string(
1439/// striped_random_unsigned_vecs_length_range::<u8>(EXAMPLE_SEED, 2, 4, 10, 1)
1440/// .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1441/// 10,
1442/// ),
1443/// "[[0, 0, 111000], [0, 11111100], [11111000, 1, 11110000], [0, 0, 0], \
1444/// [11110000, 11111111], [11111111, 11, 11111111], [1000000, 0, 11110000], \
1445/// [11111111, 11111111], [1111000, 11000000, 11111111], [11111111, 11111111, 1100], ...]"
1446/// );
1447/// ```
1448#[inline]
1449pub fn striped_random_unsigned_vecs_length_range<T: PrimitiveUnsigned>(
1450 seed: Seed,
1451 a: u64,
1452 b: u64,
1453 mean_stripe_numerator: u64,
1454 mean_stripe_denominator: u64,
1455) -> StripedRandomUnsignedVecs<T, RandomUnsignedRange<u64>> {
1456 striped_random_unsigned_vecs_from_length_iterator(
1457 seed,
1458 &|seed_2| random_unsigned_range(seed_2, a, b),
1459 mean_stripe_numerator,
1460 mean_stripe_denominator,
1461 )
1462}
1463
1464/// Generates random striped [`Vec`]s of unsigneds, with lengths in $[a, b]$.
1465///
1466/// See [here](self) for more information.
1467///
1468/// The lengths of the [`Vec`]s are sampled from a uniform distribution on $[a, b]$. $a$ must be
1469/// less than $b$.
1470///
1471/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1472/// mean_stripe_denominator`.
1473///
1474/// $$
1475/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
1476/// \frac{1}{b-a+1}\prod_{i=0}^{n-1}P(x_i) & \text{if} \\quad a \leq n \leq b, \\\\
1477/// 0 & \\text{otherwise}.
1478/// \\end{cases}
1479/// $$
1480///
1481/// # Expected complexity per iteration
1482/// $T(b) = O(b)$
1483///
1484/// $M(b) = O(b)$
1485///
1486/// where $T$ is time, $M$ is additional memory, and $b$ is `b`.
1487///
1488/// # Panics
1489/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1490/// mean_stripe_denominator`, or if $a \geq b$.
1491///
1492/// # Examples
1493/// ```
1494/// use malachite_base::iterators::prefix_to_string;
1495/// use malachite_base::num::random::striped::striped_random_unsigned_vecs_length_inclusive_range;
1496/// use malachite_base::random::EXAMPLE_SEED;
1497/// use malachite_base::strings::ToBinaryString;
1498///
1499/// assert_eq!(
1500/// prefix_to_string(
1501/// striped_random_unsigned_vecs_length_inclusive_range::<u8>(EXAMPLE_SEED, 2, 3, 10, 1)
1502/// .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1503/// 10,
1504/// ),
1505/// "[[0, 0, 111000], [0, 11111100], [11111000, 1, 11110000], [0, 0, 0], \
1506/// [11110000, 11111111], [11111111, 11, 11111111], [1000000, 0, 11110000], \
1507/// [11111111, 11111111], [1111000, 11000000, 11111111], [11111111, 11111111, 1100], ...]"
1508/// );
1509/// ```
1510#[inline]
1511pub fn striped_random_unsigned_vecs_length_inclusive_range<T: PrimitiveUnsigned>(
1512 seed: Seed,
1513 a: u64,
1514 b: u64,
1515 mean_stripe_numerator: u64,
1516 mean_stripe_denominator: u64,
1517) -> StripedRandomUnsignedVecs<T, RandomUnsignedInclusiveRange<u64>> {
1518 striped_random_unsigned_vecs_from_length_iterator(
1519 seed,
1520 &|seed_2| random_unsigned_inclusive_range(seed_2, a, b),
1521 mean_stripe_numerator,
1522 mean_stripe_denominator,
1523 )
1524}
1525
1526#[inline]
1527fn ranges_intersect<T: Copy + Ord>(lo_0: T, hi_0: T, lo: T, hi: T) -> bool {
1528 lo <= hi_0 && lo_0 <= hi
1529}
1530
1531/// Generates random striped unsigneds from a range.
1532#[derive(Clone, Debug)]
1533pub struct StripedRandomUnsignedInclusiveRange<T: PrimitiveUnsigned> {
1534 a: T,
1535 b: T,
1536 lo_template: T,
1537 hi_template: T,
1538 next_bit: u64,
1539 bit_source: StripedBitSource,
1540}
1541
1542impl<T: PrimitiveUnsigned> Iterator for StripedRandomUnsignedInclusiveRange<T> {
1543 type Item = T;
1544
1545 fn next(&mut self) -> Option<T> {
1546 if self.next_bit == 0 {
1547 return Some(self.lo_template);
1548 }
1549 let mut lo_template = self.lo_template;
1550 let mut hi_template = self.hi_template;
1551 let mut first = true;
1552 let mut previous_forced = true;
1553 let mut previous_bit = lo_template.get_bit(self.next_bit);
1554 for next_bit in (0..self.next_bit).rev() {
1555 let false_possible;
1556 let true_possible;
1557 if first {
1558 false_possible = true;
1559 true_possible = true;
1560 lo_template.assign_bit(next_bit, true);
1561 hi_template.assign_bit(next_bit, true);
1562 first = false;
1563 } else {
1564 lo_template.assign_bit(next_bit, false);
1565 hi_template.assign_bit(next_bit, false);
1566 false_possible = ranges_intersect(lo_template, hi_template, self.a, self.b);
1567 lo_template.assign_bit(next_bit, true);
1568 hi_template.assign_bit(next_bit, true);
1569 true_possible = ranges_intersect(lo_template, hi_template, self.a, self.b);
1570 }
1571 assert!(false_possible || true_possible);
1572 let bit = if !false_possible {
1573 previous_forced = true;
1574 true
1575 } else if !true_possible {
1576 previous_forced = true;
1577 false
1578 } else {
1579 if previous_forced {
1580 self.bit_source.end_block();
1581 self.bit_source.set_previous_bit(previous_bit);
1582 previous_forced = false;
1583 }
1584 self.bit_source.next().unwrap()
1585 };
1586 if !bit {
1587 lo_template.assign_bit(next_bit, false);
1588 hi_template.assign_bit(next_bit, false);
1589 }
1590 previous_bit = bit;
1591 }
1592 Some(lo_template)
1593 }
1594}
1595
1596/// Generates random striped unsigneds in the range $[a, b)$.
1597///
1598/// See [here](self) for more information.
1599///
1600/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1601/// `mean_stripe_numerator / mean_stripe_denominator`.
1602///
1603/// Because the unsigneds are constrained to be within a certain range, the actual mean run length
1604/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1605/// length.
1606///
1607/// # Expected complexity per iteration
1608/// $T(n) = O(n)$
1609///
1610/// $M(n) = O(n)$
1611///
1612/// where $T$ is time, $M$ is additional memory, and $n$ is `b.significant_bits()`.
1613///
1614/// # Panics
1615/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1616/// mean_stripe_denominator`, or if $a \geq b$.
1617///
1618/// # Examples
1619/// ```
1620/// use malachite_base::iterators::prefix_to_string;
1621/// use malachite_base::num::random::striped::striped_random_unsigned_range;
1622/// use malachite_base::random::EXAMPLE_SEED;
1623/// use malachite_base::strings::ToBinaryString;
1624///
1625/// assert_eq!(
1626/// prefix_to_string(
1627/// striped_random_unsigned_range::<u8>(EXAMPLE_SEED, 1, 7, 4, 1)
1628/// .map(|x| x.to_binary_string()),
1629/// 10
1630/// ),
1631/// "[1, 1, 1, 110, 1, 110, 10, 11, 11, 100, ...]"
1632/// );
1633/// ```
1634#[inline]
1635pub fn striped_random_unsigned_range<T: PrimitiveUnsigned>(
1636 seed: Seed,
1637 a: T,
1638 b: T,
1639 mean_stripe_numerator: u64,
1640 mean_stripe_denominator: u64,
1641) -> StripedRandomUnsignedInclusiveRange<T> {
1642 assert!(a < b);
1643 striped_random_unsigned_inclusive_range(
1644 seed,
1645 a,
1646 b - T::ONE,
1647 mean_stripe_numerator,
1648 mean_stripe_denominator,
1649 )
1650}
1651
1652/// Generates random striped unsigneds in the range $[a, b]$.
1653///
1654/// See [here](self) for more information.
1655///
1656/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1657/// `mean_stripe_numerator / mean_stripe_denominator`.
1658///
1659/// Because the unsigneds are constrained to be within a certain range, the actual mean run length
1660/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1661/// length.
1662///
1663/// # Expected complexity per iteration
1664/// $T(n) = O(n)$
1665///
1666/// $M(n) = O(n)$
1667///
1668/// where $T$ is time, $M$ is additional memory, and $n$ is `b.significant_bits()`.
1669///
1670/// # Panics
1671/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1672/// mean_stripe_denominator`, or if $a > b$.
1673///
1674/// # Examples
1675/// ```
1676/// use malachite_base::iterators::prefix_to_string;
1677/// use malachite_base::num::random::striped::striped_random_unsigned_inclusive_range;
1678/// use malachite_base::random::EXAMPLE_SEED;
1679/// use malachite_base::strings::ToBinaryString;
1680///
1681/// assert_eq!(
1682/// prefix_to_string(
1683/// striped_random_unsigned_inclusive_range::<u8>(EXAMPLE_SEED, 1, 6, 4, 1)
1684/// .map(|x| x.to_binary_string()),
1685/// 10
1686/// ),
1687/// "[1, 1, 1, 110, 1, 110, 10, 11, 11, 100, ...]"
1688/// );
1689/// ```
1690pub fn striped_random_unsigned_inclusive_range<T: PrimitiveUnsigned>(
1691 seed: Seed,
1692 a: T,
1693 b: T,
1694 mean_stripe_numerator: u64,
1695 mean_stripe_denominator: u64,
1696) -> StripedRandomUnsignedInclusiveRange<T> {
1697 assert!(a <= b);
1698 let diff_bits = T::WIDTH - (a ^ b).leading_zeros();
1699 let mask = T::low_mask(diff_bits);
1700 let lo_template = a & !mask;
1701 let hi_template = lo_template | mask;
1702 StripedRandomUnsignedInclusiveRange {
1703 a,
1704 b,
1705 lo_template,
1706 hi_template,
1707 next_bit: diff_bits,
1708 bit_source: StripedBitSource::new(seed, mean_stripe_numerator, mean_stripe_denominator),
1709 }
1710}
1711
1712/// Generates random striped signeds from a range.
1713#[allow(clippy::large_enum_variant)]
1714#[derive(Clone, Debug)]
1715pub enum StripedRandomSignedInclusiveRange<
1716 U: PrimitiveUnsigned,
1717 S: PrimitiveSigned + WrappingFrom<U>,
1718> {
1719 NonNegative(PhantomData<S>, StripedRandomUnsignedInclusiveRange<U>),
1720 Negative(PhantomData<S>, StripedRandomUnsignedInclusiveRange<U>),
1721 Both(
1722 PhantomData<S>,
1723 Box<RandomBools>,
1724 StripedRandomUnsignedInclusiveRange<U>,
1725 StripedRandomUnsignedInclusiveRange<U>,
1726 ),
1727}
1728
1729impl<U: PrimitiveUnsigned, S: PrimitiveSigned + WrappingFrom<U>> Iterator
1730 for StripedRandomSignedInclusiveRange<U, S>
1731{
1732 type Item = S;
1733
1734 fn next(&mut self) -> Option<S> {
1735 match self {
1736 Self::NonNegative(_, xs) => xs.next().map(S::wrapping_from),
1737 Self::Negative(_, xs) => xs.next().map(|x| S::wrapping_from(x).wrapping_neg()),
1738 Self::Both(_, bs, xs_nn, xs_n) => {
1739 if bs.next().unwrap() {
1740 xs_nn.next().map(S::wrapping_from)
1741 } else {
1742 xs_n.next().map(|x| S::wrapping_from(x).wrapping_neg())
1743 }
1744 }
1745 }
1746 }
1747}
1748
1749/// Generates random striped signeds in the range $[a, b]$.
1750///
1751/// See [here](self) for more information.
1752///
1753/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1754/// `mean_stripe_numerator / mean_stripe_denominator`.
1755///
1756/// Because the signeds are constrained to be within a certain range, the actual mean run length
1757/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1758/// length.
1759///
1760/// # Expected complexity per iteration
1761/// $T(n) = O(n)$
1762///
1763/// $M(n) = O(n)$
1764///
1765/// where $T$ is time, $M$ is additional memory, and $n$ is `max(a.significant_bits(),
1766/// b.significant_bits())`.
1767///
1768/// # Panics
1769/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1770/// mean_stripe_denominator`, or if $a \geq b$.
1771///
1772/// # Examples
1773/// ```
1774/// use malachite_base::iterators::prefix_to_string;
1775/// use malachite_base::num::random::striped::striped_random_signed_inclusive_range;
1776/// use malachite_base::random::EXAMPLE_SEED;
1777/// use malachite_base::strings::ToBinaryString;
1778///
1779/// assert_eq!(
1780/// prefix_to_string(
1781/// striped_random_signed_inclusive_range::<u8, i8>(EXAMPLE_SEED, -5, 10, 4, 1)
1782/// .map(|x| x.to_binary_string()),
1783/// 10
1784/// ),
1785/// "[11111011, 11111100, 1000, 111, 11111111, 1000, 11, 1000, 0, 1000, ...]"
1786/// );
1787/// ```
1788#[inline]
1789pub fn striped_random_signed_range<
1790 U: PrimitiveUnsigned + WrappingFrom<S>,
1791 S: PrimitiveSigned + WrappingFrom<U>,
1792>(
1793 seed: Seed,
1794 a: S,
1795 b: S,
1796 mean_stripe_numerator: u64,
1797 mean_stripe_denominator: u64,
1798) -> StripedRandomSignedInclusiveRange<U, S> {
1799 assert!(a < b);
1800 striped_random_signed_inclusive_range(
1801 seed,
1802 a,
1803 b - S::ONE,
1804 mean_stripe_numerator,
1805 mean_stripe_denominator,
1806 )
1807}
1808
1809/// Generates random striped signeds in the range $[a, b)$.
1810///
1811/// See [here](self) for more information.
1812///
1813/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1814/// `mean_stripe_numerator / mean_stripe_denominator`.
1815///
1816/// Because the signeds are constrained to be within a certain range, the actual mean run length
1817/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1818/// length.
1819///
1820/// # Expected complexity per iteration
1821/// $T(n) = O(n)$
1822///
1823/// $M(n) = O(n)$
1824///
1825/// where $T$ is time, $M$ is additional memory, and $n$ is `max(a.significant_bits(),
1826/// b.significant_bits())`.
1827///
1828/// # Panics
1829/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1830/// mean_stripe_denominator`, or if $a > b$.
1831///
1832/// # Examples
1833/// ```
1834/// use malachite_base::iterators::prefix_to_string;
1835/// use malachite_base::num::random::striped::striped_random_signed_range;
1836/// use malachite_base::random::EXAMPLE_SEED;
1837/// use malachite_base::strings::ToBinaryString;
1838///
1839/// assert_eq!(
1840/// prefix_to_string(
1841/// striped_random_signed_range::<u8, i8>(EXAMPLE_SEED, -5, 11, 4, 1)
1842/// .map(|x| x.to_binary_string()),
1843/// 10
1844/// ),
1845/// "[11111011, 11111100, 1000, 111, 11111111, 1000, 11, 1000, 0, 1000, ...]"
1846/// );
1847/// ```
1848pub fn striped_random_signed_inclusive_range<
1849 U: PrimitiveUnsigned + WrappingFrom<S>,
1850 S: PrimitiveSigned + WrappingFrom<U>,
1851>(
1852 seed: Seed,
1853 a: S,
1854 b: S,
1855 mean_stripe_numerator: u64,
1856 mean_stripe_denominator: u64,
1857) -> StripedRandomSignedInclusiveRange<U, S> {
1858 assert!(a <= b);
1859 if a >= S::ZERO {
1860 StripedRandomSignedInclusiveRange::NonNegative(
1861 PhantomData,
1862 striped_random_unsigned_inclusive_range(
1863 seed,
1864 U::wrapping_from(a),
1865 U::wrapping_from(b),
1866 mean_stripe_numerator,
1867 mean_stripe_denominator,
1868 ),
1869 )
1870 } else if b < S::ZERO {
1871 StripedRandomSignedInclusiveRange::Negative(
1872 PhantomData,
1873 striped_random_unsigned_inclusive_range(
1874 seed,
1875 U::wrapping_from(b.wrapping_neg()),
1876 U::wrapping_from(a.wrapping_neg()),
1877 mean_stripe_numerator,
1878 mean_stripe_denominator,
1879 ),
1880 )
1881 } else {
1882 StripedRandomSignedInclusiveRange::Both(
1883 PhantomData,
1884 Box::new(random_bools(seed.fork("sign"))),
1885 striped_random_unsigned_inclusive_range(
1886 seed.fork("non-negative"),
1887 U::ZERO,
1888 U::wrapping_from(b),
1889 mean_stripe_numerator,
1890 mean_stripe_denominator,
1891 ),
1892 striped_random_unsigned_inclusive_range(
1893 seed.fork("negative"),
1894 U::ONE,
1895 U::wrapping_from(a.wrapping_neg()),
1896 mean_stripe_numerator,
1897 mean_stripe_denominator,
1898 ),
1899 )
1900 }
1901}