Skip to main content

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::{get_striped_bool_vec, StripedBitSource};
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::{get_striped_unsigned_vec, StripedBitSource};
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)
1296///             .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1297///         10,
1298///     ),
1299///     "[[0, 0, 111000, 0, 11111110, 10000001], [0], \
1300///     [11110000, 11111111, 11111111, 11111111, 11, 0, 10000000, 11111], [0], \
1301///     [10000, 0, 11111100, 11111111, 1111111, 11111000, 11, 0, 0, 10011000, 11111111, 111, 0, \
1302///     0], [], [11111111, 11111111, 11111111, 11111111, 10111111], [0, 0, 0, 11110000], \
1303///     [11111111], [], ...]"
1304/// );
1305/// ```
1306#[inline]
1307pub fn striped_random_unsigned_vecs<T: PrimitiveUnsigned>(
1308    seed: Seed,
1309    mean_stripe_numerator: u64,
1310    mean_stripe_denominator: u64,
1311    mean_length_numerator: u64,
1312    mean_length_denominator: u64,
1313) -> StripedRandomUnsignedVecs<T, GeometricRandomNaturalValues<u64>> {
1314    striped_random_unsigned_vecs_from_length_iterator(
1315        seed,
1316        &|seed_2| {
1317            geometric_random_unsigneds(seed_2, mean_length_numerator, mean_length_denominator)
1318        },
1319        mean_stripe_numerator,
1320        mean_stripe_denominator,
1321    )
1322}
1323
1324/// Generates random striped [`Vec`]s of unsigneds, with a minimum length.
1325///
1326/// See [here](self) for more information.
1327///
1328/// The lengths of the [`Vec`]s are sampled from a geometric distribution with a specified mean $m$,
1329/// equal to `mean_length_numerator / mean_length_denominator`. $m$ must be greater than
1330/// `min_length`.
1331///
1332/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1333/// mean_stripe_denominator`.
1334///
1335/// $$
1336/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
1337///     \frac{(m-a)^{n-a}}{(m+1-a)^{n+1-a}}\prod_{i=0}^{n-1}P(x_i) & n \geq a \\\\
1338///     0 & \\text{otherwise},
1339/// \\end{cases}
1340/// $$
1341/// where $a$ is `min_length`.
1342///
1343/// # Expected complexity per iteration
1344/// $T(n) = O(n)$
1345///
1346/// $M(n) = O(n)$
1347///
1348/// where $T$ is time, $M$ is additional memory, and $n$ is `mean_length_numerator /
1349/// mean_length_denominator`.
1350///
1351/// # Panics
1352/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1353/// mean_stripe_denominator`, if `mean_length_numerator` or `mean_length_denominator` are zero, if
1354/// their ratio is less than or equal to `min_length`, or if they are too large and manipulating
1355/// them leads to arithmetic overflow.
1356///
1357/// # Examples
1358/// ```
1359/// use malachite_base::iterators::prefix_to_string;
1360/// use malachite_base::num::random::striped::striped_random_unsigned_vecs_min_length;
1361/// use malachite_base::random::EXAMPLE_SEED;
1362/// use malachite_base::strings::ToBinaryString;
1363///
1364/// assert_eq!(
1365///     prefix_to_string(
1366///         striped_random_unsigned_vecs_min_length::<u8>(EXAMPLE_SEED, 2, 10, 1, 3, 1)
1367///             .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1368///         10,
1369///     ),
1370///     "[[0, 0, 111000], [0, 11111100, 11, 11111111], \
1371///     [11110000, 11111111, 11111111, 11111111], [11111000, 11111111, 11111111, 11000000], \
1372///     [0, 10000, 0], [111, 0, 0, 1111], [11110000, 11111111], [11111111, 111111], \
1373///     [110, 10000000, 11111111], [11111111, 11111111], ...]"
1374/// );
1375/// ```
1376#[inline]
1377pub fn striped_random_unsigned_vecs_min_length<T: PrimitiveUnsigned>(
1378    seed: Seed,
1379    min_length: u64,
1380    mean_stripe_numerator: u64,
1381    mean_stripe_denominator: u64,
1382    mean_length_numerator: u64,
1383    mean_length_denominator: u64,
1384) -> StripedRandomUnsignedVecs<T, GeometricRandomNaturalValues<u64>> {
1385    striped_random_unsigned_vecs_from_length_iterator(
1386        seed,
1387        &|seed_2| {
1388            geometric_random_unsigned_inclusive_range(
1389                seed_2,
1390                min_length,
1391                u64::MAX,
1392                mean_length_numerator,
1393                mean_length_denominator,
1394            )
1395        },
1396        mean_stripe_numerator,
1397        mean_stripe_denominator,
1398    )
1399}
1400
1401/// Generates random striped [`Vec`]s of unsigneds, with lengths in $[a, b)$.
1402///
1403/// See [here](self) for more information.
1404///
1405/// The lengths of the [`Vec`]s are sampled from a uniform distribution on $[a, b)$. $a$ must be
1406/// less than $b$.
1407///
1408/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1409/// mean_stripe_denominator`.
1410///
1411/// $$
1412/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
1413///     \frac{1}{b-a}\prod_{i=0}^{n-1}P(x_i) & \text{if} \\quad a \leq n < b, \\\\
1414///     0 & \\text{otherwise}.
1415/// \\end{cases}
1416/// $$
1417///
1418/// # Expected complexity per iteration
1419/// $T(b) = O(b)$
1420///
1421/// $M(b) = O(b)$
1422///
1423/// where $T$ is time, $M$ is additional memory, and $b$ is `b`.
1424///
1425/// # Panics
1426/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1427/// mean_stripe_denominator`, or if $a \geq b$.
1428///
1429/// # Examples
1430/// ```
1431/// use malachite_base::iterators::prefix_to_string;
1432/// use malachite_base::num::random::striped::striped_random_unsigned_vecs_length_range;
1433/// use malachite_base::random::EXAMPLE_SEED;
1434/// use malachite_base::strings::ToBinaryString;
1435///
1436/// assert_eq!(
1437///     prefix_to_string(
1438///         striped_random_unsigned_vecs_length_range::<u8>(EXAMPLE_SEED, 2, 4, 10, 1)
1439///             .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1440///         10,
1441///     ),
1442///     "[[0, 0, 111000], [0, 11111100], [11111000, 1, 11110000], [0, 0, 0], \
1443///     [11110000, 11111111], [11111111, 11, 11111111], [1000000, 0, 11110000], \
1444///     [11111111, 11111111], [1111000, 11000000, 11111111], [11111111, 11111111, 1100], ...]"
1445/// );
1446/// ```
1447#[inline]
1448pub fn striped_random_unsigned_vecs_length_range<T: PrimitiveUnsigned>(
1449    seed: Seed,
1450    a: u64,
1451    b: u64,
1452    mean_stripe_numerator: u64,
1453    mean_stripe_denominator: u64,
1454) -> StripedRandomUnsignedVecs<T, RandomUnsignedRange<u64>> {
1455    striped_random_unsigned_vecs_from_length_iterator(
1456        seed,
1457        &|seed_2| random_unsigned_range(seed_2, a, b),
1458        mean_stripe_numerator,
1459        mean_stripe_denominator,
1460    )
1461}
1462
1463/// Generates random striped [`Vec`]s of unsigneds, with lengths in $[a, b]$.
1464///
1465/// See [here](self) for more information.
1466///
1467/// The lengths of the [`Vec`]s are sampled from a uniform distribution on $[a, b]$. $a$ must be
1468/// less than $b$.
1469///
1470/// The mean run length (before the bit sequences are truncated) is $m$ = `mean_stripe_numerator /
1471/// mean_stripe_denominator`.
1472///
1473/// $$
1474/// P((x_0, x_1, \ldots, x_{n-1})) = \\begin{cases}
1475///     \frac{1}{b-a+1}\prod_{i=0}^{n-1}P(x_i) & \text{if} \\quad a \leq n \leq b, \\\\
1476///     0 & \\text{otherwise}.
1477/// \\end{cases}
1478/// $$
1479///
1480/// # Expected complexity per iteration
1481/// $T(b) = O(b)$
1482///
1483/// $M(b) = O(b)$
1484///
1485/// where $T$ is time, $M$ is additional memory, and $b$ is `b`.
1486///
1487/// # Panics
1488/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1489/// mean_stripe_denominator`, or if $a \geq b$.
1490///
1491/// # Examples
1492/// ```
1493/// use malachite_base::iterators::prefix_to_string;
1494/// use malachite_base::num::random::striped::striped_random_unsigned_vecs_length_inclusive_range;
1495/// use malachite_base::random::EXAMPLE_SEED;
1496/// use malachite_base::strings::ToBinaryString;
1497///
1498/// assert_eq!(
1499///     prefix_to_string(
1500///         striped_random_unsigned_vecs_length_inclusive_range::<u8>(EXAMPLE_SEED, 2, 3, 10, 1)
1501///             .map(|xs| prefix_to_string(xs.into_iter().map(|x: u8| x.to_binary_string()), 100)),
1502///         10,
1503///     ),
1504///     "[[0, 0, 111000], [0, 11111100], [11111000, 1, 11110000], [0, 0, 0], \
1505///     [11110000, 11111111], [11111111, 11, 11111111], [1000000, 0, 11110000], \
1506///     [11111111, 11111111], [1111000, 11000000, 11111111], [11111111, 11111111, 1100], ...]"
1507/// );
1508/// ```
1509#[inline]
1510pub fn striped_random_unsigned_vecs_length_inclusive_range<T: PrimitiveUnsigned>(
1511    seed: Seed,
1512    a: u64,
1513    b: u64,
1514    mean_stripe_numerator: u64,
1515    mean_stripe_denominator: u64,
1516) -> StripedRandomUnsignedVecs<T, RandomUnsignedInclusiveRange<u64>> {
1517    striped_random_unsigned_vecs_from_length_iterator(
1518        seed,
1519        &|seed_2| random_unsigned_inclusive_range(seed_2, a, b),
1520        mean_stripe_numerator,
1521        mean_stripe_denominator,
1522    )
1523}
1524
1525#[inline]
1526fn ranges_intersect<T: Copy + Ord>(lo_0: T, hi_0: T, lo: T, hi: T) -> bool {
1527    lo <= hi_0 && lo_0 <= hi
1528}
1529
1530/// Generates random striped unsigneds from a range.
1531#[derive(Clone, Debug)]
1532pub struct StripedRandomUnsignedInclusiveRange<T: PrimitiveUnsigned> {
1533    a: T,
1534    b: T,
1535    lo_template: T,
1536    hi_template: T,
1537    next_bit: u64,
1538    bit_source: StripedBitSource,
1539}
1540
1541impl<T: PrimitiveUnsigned> Iterator for StripedRandomUnsignedInclusiveRange<T> {
1542    type Item = T;
1543
1544    fn next(&mut self) -> Option<T> {
1545        if self.next_bit == 0 {
1546            return Some(self.lo_template);
1547        }
1548        let mut lo_template = self.lo_template;
1549        let mut hi_template = self.hi_template;
1550        let mut first = true;
1551        let mut previous_forced = true;
1552        let mut previous_bit = lo_template.get_bit(self.next_bit);
1553        for next_bit in (0..self.next_bit).rev() {
1554            let false_possible;
1555            let true_possible;
1556            if first {
1557                false_possible = true;
1558                true_possible = true;
1559                lo_template.assign_bit(next_bit, true);
1560                hi_template.assign_bit(next_bit, true);
1561                first = false;
1562            } else {
1563                lo_template.assign_bit(next_bit, false);
1564                hi_template.assign_bit(next_bit, false);
1565                false_possible = ranges_intersect(lo_template, hi_template, self.a, self.b);
1566                lo_template.assign_bit(next_bit, true);
1567                hi_template.assign_bit(next_bit, true);
1568                true_possible = ranges_intersect(lo_template, hi_template, self.a, self.b);
1569            }
1570            assert!(false_possible || true_possible);
1571            let bit = if !false_possible {
1572                previous_forced = true;
1573                true
1574            } else if !true_possible {
1575                previous_forced = true;
1576                false
1577            } else {
1578                if previous_forced {
1579                    self.bit_source.end_block();
1580                    self.bit_source.set_previous_bit(previous_bit);
1581                    previous_forced = false;
1582                }
1583                self.bit_source.next().unwrap()
1584            };
1585            if !bit {
1586                lo_template.assign_bit(next_bit, false);
1587                hi_template.assign_bit(next_bit, false);
1588            }
1589            previous_bit = bit;
1590        }
1591        Some(lo_template)
1592    }
1593}
1594
1595/// Generates random striped unsigneds in the range $[a, b)$.
1596///
1597/// See [here](self) for more information.
1598///
1599/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1600/// `mean_stripe_numerator / mean_stripe_denominator`.
1601///
1602/// Because the unsigneds are constrained to be within a certain range, the actual mean run length
1603/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1604/// length.
1605///
1606/// # Expected complexity per iteration
1607/// $T(n) = O(n)$
1608///
1609/// $M(n) = O(n)$
1610///
1611/// where $T$ is time, $M$ is additional memory, and $n$ is `b.significant_bits()`.
1612///
1613/// # Panics
1614/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1615/// mean_stripe_denominator`, or if $a \geq b$.
1616///
1617/// # Examples
1618/// ```
1619/// use malachite_base::iterators::prefix_to_string;
1620/// use malachite_base::num::random::striped::striped_random_unsigned_range;
1621/// use malachite_base::random::EXAMPLE_SEED;
1622/// use malachite_base::strings::ToBinaryString;
1623///
1624/// assert_eq!(
1625///     prefix_to_string(
1626///         striped_random_unsigned_range::<u8>(EXAMPLE_SEED, 1, 7, 4, 1)
1627///             .map(|x| x.to_binary_string()),
1628///         10
1629///     ),
1630///     "[1, 1, 1, 110, 1, 110, 10, 11, 11, 100, ...]"
1631/// );
1632/// ```
1633#[inline]
1634pub fn striped_random_unsigned_range<T: PrimitiveUnsigned>(
1635    seed: Seed,
1636    a: T,
1637    b: T,
1638    mean_stripe_numerator: u64,
1639    mean_stripe_denominator: u64,
1640) -> StripedRandomUnsignedInclusiveRange<T> {
1641    assert!(a < b);
1642    striped_random_unsigned_inclusive_range(
1643        seed,
1644        a,
1645        b - T::ONE,
1646        mean_stripe_numerator,
1647        mean_stripe_denominator,
1648    )
1649}
1650
1651/// Generates random striped unsigneds in the range $[a, b]$.
1652///
1653/// See [here](self) for more information.
1654///
1655/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1656/// `mean_stripe_numerator / mean_stripe_denominator`.
1657///
1658/// Because the unsigneds are constrained to be within a certain range, the actual mean run length
1659/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1660/// length.
1661///
1662/// # Expected complexity per iteration
1663/// $T(n) = O(n)$
1664///
1665/// $M(n) = O(n)$
1666///
1667/// where $T$ is time, $M$ is additional memory, and $n$ is `b.significant_bits()`.
1668///
1669/// # Panics
1670/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1671/// mean_stripe_denominator`, or if $a > b$.
1672///
1673/// # Examples
1674/// ```
1675/// use malachite_base::iterators::prefix_to_string;
1676/// use malachite_base::num::random::striped::striped_random_unsigned_inclusive_range;
1677/// use malachite_base::random::EXAMPLE_SEED;
1678/// use malachite_base::strings::ToBinaryString;
1679///
1680/// assert_eq!(
1681///     prefix_to_string(
1682///         striped_random_unsigned_inclusive_range::<u8>(EXAMPLE_SEED, 1, 6, 4, 1)
1683///             .map(|x| x.to_binary_string()),
1684///         10
1685///     ),
1686///     "[1, 1, 1, 110, 1, 110, 10, 11, 11, 100, ...]"
1687/// );
1688/// ```
1689pub fn striped_random_unsigned_inclusive_range<T: PrimitiveUnsigned>(
1690    seed: Seed,
1691    a: T,
1692    b: T,
1693    mean_stripe_numerator: u64,
1694    mean_stripe_denominator: u64,
1695) -> StripedRandomUnsignedInclusiveRange<T> {
1696    assert!(a <= b);
1697    let diff_bits = T::WIDTH - (a ^ b).leading_zeros();
1698    let mask = T::low_mask(diff_bits);
1699    let lo_template = a & !mask;
1700    let hi_template = lo_template | mask;
1701    StripedRandomUnsignedInclusiveRange {
1702        a,
1703        b,
1704        lo_template,
1705        hi_template,
1706        next_bit: diff_bits,
1707        bit_source: StripedBitSource::new(seed, mean_stripe_numerator, mean_stripe_denominator),
1708    }
1709}
1710
1711/// Generates random striped signeds from a range.
1712#[allow(clippy::large_enum_variant)]
1713#[derive(Clone, Debug)]
1714pub enum StripedRandomSignedInclusiveRange<
1715    U: PrimitiveUnsigned,
1716    S: PrimitiveSigned + WrappingFrom<U>,
1717> {
1718    NonNegative(PhantomData<S>, StripedRandomUnsignedInclusiveRange<U>),
1719    Negative(PhantomData<S>, StripedRandomUnsignedInclusiveRange<U>),
1720    Both(
1721        PhantomData<S>,
1722        Box<RandomBools>,
1723        StripedRandomUnsignedInclusiveRange<U>,
1724        StripedRandomUnsignedInclusiveRange<U>,
1725    ),
1726}
1727
1728impl<U: PrimitiveUnsigned, S: PrimitiveSigned + WrappingFrom<U>> Iterator
1729    for StripedRandomSignedInclusiveRange<U, S>
1730{
1731    type Item = S;
1732
1733    fn next(&mut self) -> Option<S> {
1734        match self {
1735            Self::NonNegative(_, xs) => xs.next().map(S::wrapping_from),
1736            Self::Negative(_, xs) => xs.next().map(|x| S::wrapping_from(x).wrapping_neg()),
1737            Self::Both(_, bs, xs_nn, xs_n) => {
1738                if bs.next().unwrap() {
1739                    xs_nn.next().map(S::wrapping_from)
1740                } else {
1741                    xs_n.next().map(|x| S::wrapping_from(x).wrapping_neg())
1742                }
1743            }
1744        }
1745    }
1746}
1747
1748/// Generates random striped signeds in the range $[a, b]$.
1749///
1750/// See [here](self) for more information.
1751///
1752/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1753/// `mean_stripe_numerator / mean_stripe_denominator`.
1754///
1755/// Because the signeds are constrained to be within a certain range, the actual mean run length
1756/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1757/// length.
1758///
1759/// # Expected complexity per iteration
1760/// $T(n) = O(n)$
1761///
1762/// $M(n) = O(n)$
1763///
1764/// where $T$ is time, $M$ is additional memory, and $n$ is `max(a.significant_bits(),
1765/// b.significant_bits())`.
1766///
1767/// # Panics
1768/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1769/// mean_stripe_denominator`, or if $a \geq b$.
1770///
1771/// # Examples
1772/// ```
1773/// use malachite_base::iterators::prefix_to_string;
1774/// use malachite_base::num::random::striped::striped_random_signed_inclusive_range;
1775/// use malachite_base::random::EXAMPLE_SEED;
1776/// use malachite_base::strings::ToBinaryString;
1777///
1778/// assert_eq!(
1779///     prefix_to_string(
1780///         striped_random_signed_inclusive_range::<u8, i8>(EXAMPLE_SEED, -5, 10, 4, 1)
1781///             .map(|x| x.to_binary_string()),
1782///         10
1783///     ),
1784///     "[11111011, 11111100, 1000, 111, 11111111, 1000, 11, 1000, 0, 1000, ...]"
1785/// );
1786/// ```
1787#[inline]
1788pub fn striped_random_signed_range<
1789    U: PrimitiveUnsigned + WrappingFrom<S>,
1790    S: PrimitiveSigned + WrappingFrom<U>,
1791>(
1792    seed: Seed,
1793    a: S,
1794    b: S,
1795    mean_stripe_numerator: u64,
1796    mean_stripe_denominator: u64,
1797) -> StripedRandomSignedInclusiveRange<U, S> {
1798    assert!(a < b);
1799    striped_random_signed_inclusive_range(
1800        seed,
1801        a,
1802        b - S::ONE,
1803        mean_stripe_numerator,
1804        mean_stripe_denominator,
1805    )
1806}
1807
1808/// Generates random striped signeds in the range $[a, b)$.
1809///
1810/// See [here](self) for more information.
1811///
1812/// The unsigneds are generated using a striped bit sequence with mean run length $m$ =
1813/// `mean_stripe_numerator / mean_stripe_denominator`.
1814///
1815/// Because the signeds are constrained to be within a certain range, the actual mean run length
1816/// will usually not be $m$. Nonetheless, setting a higher $m$ will result in a higher mean run
1817/// length.
1818///
1819/// # Expected complexity per iteration
1820/// $T(n) = O(n)$
1821///
1822/// $M(n) = O(n)$
1823///
1824/// where $T$ is time, $M$ is additional memory, and $n$ is `max(a.significant_bits(),
1825/// b.significant_bits())`.
1826///
1827/// # Panics
1828/// Panics if `mean_stripe_denominator` is zero, if `mean_stripe_numerator <=
1829/// mean_stripe_denominator`, or if $a > b$.
1830///
1831/// # Examples
1832/// ```
1833/// use malachite_base::iterators::prefix_to_string;
1834/// use malachite_base::num::random::striped::striped_random_signed_range;
1835/// use malachite_base::random::EXAMPLE_SEED;
1836/// use malachite_base::strings::ToBinaryString;
1837///
1838/// assert_eq!(
1839///     prefix_to_string(
1840///         striped_random_signed_range::<u8, i8>(EXAMPLE_SEED, -5, 11, 4, 1)
1841///             .map(|x| x.to_binary_string()),
1842///         10
1843///     ),
1844///     "[11111011, 11111100, 1000, 111, 11111111, 1000, 11, 1000, 0, 1000, ...]"
1845/// );
1846/// ```
1847pub fn striped_random_signed_inclusive_range<
1848    U: PrimitiveUnsigned + WrappingFrom<S>,
1849    S: PrimitiveSigned + WrappingFrom<U>,
1850>(
1851    seed: Seed,
1852    a: S,
1853    b: S,
1854    mean_stripe_numerator: u64,
1855    mean_stripe_denominator: u64,
1856) -> StripedRandomSignedInclusiveRange<U, S> {
1857    assert!(a <= b);
1858    if a >= S::ZERO {
1859        StripedRandomSignedInclusiveRange::NonNegative(
1860            PhantomData,
1861            striped_random_unsigned_inclusive_range(
1862                seed,
1863                U::wrapping_from(a),
1864                U::wrapping_from(b),
1865                mean_stripe_numerator,
1866                mean_stripe_denominator,
1867            ),
1868        )
1869    } else if b < S::ZERO {
1870        StripedRandomSignedInclusiveRange::Negative(
1871            PhantomData,
1872            striped_random_unsigned_inclusive_range(
1873                seed,
1874                U::wrapping_from(b.wrapping_neg()),
1875                U::wrapping_from(a.wrapping_neg()),
1876                mean_stripe_numerator,
1877                mean_stripe_denominator,
1878            ),
1879        )
1880    } else {
1881        StripedRandomSignedInclusiveRange::Both(
1882            PhantomData,
1883            Box::new(random_bools(seed.fork("sign"))),
1884            striped_random_unsigned_inclusive_range(
1885                seed.fork("non-negative"),
1886                U::ZERO,
1887                U::wrapping_from(b),
1888                mean_stripe_numerator,
1889                mean_stripe_denominator,
1890            ),
1891            striped_random_unsigned_inclusive_range(
1892                seed.fork("negative"),
1893                U::ONE,
1894                U::wrapping_from(a.wrapping_neg()),
1895                mean_stripe_numerator,
1896                mean_stripe_denominator,
1897            ),
1898        )
1899    }
1900}