1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
DRAND48 Linear congruential generator
implementation by Radim Kolar <hsn@sendmail.cz> 2025
https://gitlab.com/hsn10/drand48
This is free and unencumbered software released into the public domain.
SPDX-License-Identifier: Unlicense OR CC0-1.0
For more information, please refer to <http://unlicense.org/>
*/
/// Extracts the top quality 3 bytes (24 most significant bits) from a 48-bit drand48 output.
///
/// Assumes the input is the raw 48-bit output,
/// as typically returned by a `next()` function in a drand48 implementation.
///
/// # Arguments
/// * `drand48_output` - A `i64` representing the 48-bit internal state
/// (should be < 2^48) and not negative.
///
/// # Returns
/// A `[u8; 3]` array where:
/// - index 0 contains bits 40–47 (most significant)
/// - index 1 contains bits 32–39
/// - index 2 contains bits 24–31
pub const
/// Extracts good quality 4 bytes (32 most significant bits) from a 48-bit `drand48` output.
///
/// Assumes the input is the raw 48-bit output,
/// as typically returned by the `next()` function in a `drand48` implementation.
///
/// # Arguments
/// * `drand48_output` - A `i64` representing the 48-bit internal state
/// (must be in range `0..2^48` and not negative).
///
/// # Returns
/// A `[u8; 4]` array where:
/// - index 0 contains bits 40–47 (most significant)
/// - index 1 contains bits 32–39
/// - index 2 contains bits 24–31
/// - index 3 contains bits 16–23
pub const
/// Extracts the top 15 bits from a 48-bit `drand48` output as a non-negative `i16`.
///
/// Bits 33–47 of the 48-bit state are used, giving 15 bits of high-quality randomness.
/// The returned value is always in the range `0..=32767` (i.e., fits in `i16` without sign).
///
/// # Arguments
/// * `drand48_output` - A 48-bit integer (`i64`) representing the internal LCG state.
///
/// # Returns
/// A non-negative `i16` containing the top 15 bits of the input.
pub const
/// Extracts the top 16 bits from a 48-bit `drand48` output as an unsigned 16-bit integer.
///
/// # Arguments
/// * `drand48_output` - A 48-bit integer (`i64`) representing the internal LCG state.
///
/// # Returns
/// A `u16` containing bits 32–47 (the most significant 16 bits).
pub const
/// Fills buffer with random bytes generated by provided DRAND48.
///
/// This function splits the buffer into chunks of up to 4 bytes and fills each chunk
/// with bytes derived from a `i64` value generated by `DRAND48.next()`.
///
/// # Parameters
///
/// - `buffer`: A mutable reference to a slice of bytes (`&mut [u8]`) that will be filled with random data.
/// - `rng`: A mutable reference to DRAND48.
///
/// # Examples
///
/// ```
/// use drand48::DRAND48;
///
/// let mut data = [0u8; 16];
/// drand48::extract::fill_bytes(&mut data, &mut DRAND48::new());
///
/// println!("{:?}", data); // Randomized output
/// ```