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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
//! # randlib
//!
//! Dependencyless random value generator using pointer addresses and time
//!
//! The generator is using the LCG algorithm seed by a pointer address multiplied by
//! UNIX_EPOCH.
//!
//! # Usage
//!
//! Basic usage
//!
//! ```rust
//! use randlib::Rand;
//!
//! fn main() {
//! let mut rng = Rand::new();
//! println!("u8 MAX:\t\t{}\t\t\t\t\t RNG: {}", u8::MAX, rng.rand_u8());
//! println!("i8 MAX:\t\t{}\t\t\t\t\t RNG: {}", i8::MAX, rng.rand_i8());
//! println!("u16 MAX:\t{}\t\t\t\t\t RNG: {}", u16::MAX, rng.rand_u16());
//! println!("i16 MAX:\t{}\t\t\t\t\t RNG: {}", i16::MAX, rng.rand_i16());
//! println!("u32 MAX:\t{}\t\t\t\t RNG: {}", u32::MAX, rng.rand_u32());
//! println!("i32 MAX:\t{}\t\t\t\t RNG: {}", i32::MAX, rng.rand_i32());
//! println!("u64 MAX:\t{}\t\t\t RNG: {}", u64::MAX, rng.rand_u64());
//! println!("i64 MAX:\t{}\t\t\t RNG: {}", i64::MAX, rng.rand_i64());
//! println!("u128 MAX:\t{}\t RNG: {}", u128::MAX, rng.rand_u128());
//! println!("i128 MAX:\t{}\t RNG: {}", i128::MAX, rng.rand_i128());
//! println!("bool RNG:\t{:?}", rng.rand_bool());
//! }
//! ```
//!
//! # LICENSE
//!
//! This project is distributed under MIT license.
// Big prime numbers
const PRIME_A: u64 = 1442695040888963407;
const PRIME_B: u64 = 6364136223846793005;
/// Rand Struct
pub struct Rand {
seed: u64,
}
/// Default Rand implementation
impl Default for Rand {
fn default() -> Self {
Self::new()
}
}
/// Rand implementation
impl Rand {
/// Create new Rand object
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// ```
pub fn new() -> Rand {
let rand_ptr: *const i32 = &123;
Rand {
// Seed the RNG with a pointer address multiplied by UNIX_EPOCH
seed: (rand_ptr as u64).wrapping_mul(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
),
}
}
/// Generate random value (u32)
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_number: u32 = rng.rand();
/// ```
pub fn rand(&mut self) -> u32 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) as u32
}
/// Generate random bool value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_bool: bool = rng.rand_bool();
/// ```
pub fn rand_bool(&mut self) -> bool {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) % 2 == 0
}
/// Generate random u8 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_u8: u8 = rng.rand_u8();
/// ```
pub fn rand_u8(&mut self) -> u8 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) as u8
}
/// Generate random u16 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_u16: u16 = rng.rand_u16();
/// ```
pub fn rand_u16(&mut self) -> u16 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) as u16
}
/// Generate random u32 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_u32: u32 = rng.rand_u32();
/// ```
pub fn rand_u32(&mut self) -> u32 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) as u32
}
/// Generate random u64 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_u64: u64 = rng.rand_u64();
/// ```
pub fn rand_u64(&mut self) -> u64 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32).wrapping_mul(PRIME_A) as u64
}
/// Generate random u128 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_u128: u128 = rng.rand_u128();
/// ```
pub fn rand_u128(&mut self) -> u128 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
((self.seed >> 32) as u128)
.wrapping_mul(PRIME_A as u128)
.wrapping_mul(PRIME_B as u128)
}
/// Generate random i8 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_i8: i8 = rng.rand_i8();
/// ```
pub fn rand_i8(&mut self) -> i8 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) as i8
}
/// Generate random i16 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_i16: i16 = rng.rand_i16();
/// ```
pub fn rand_i16(&mut self) -> i16 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) as i16
}
/// Generate random i32 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_i32: i32 = rng.rand_i32();
/// ```
pub fn rand_i32(&mut self) -> i32 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32) as i32
}
/// Generate random i64 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_i64: i64 = rng.rand_i64();
/// ```
pub fn rand_i64(&mut self) -> i64 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32).wrapping_mul(PRIME_A) as i64
}
/// Generate random i128 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_i128: i128 = rng.rand_i128();
/// ```
pub fn rand_i128(&mut self) -> i128 {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
((self.seed >> 32) as i128)
.wrapping_mul(PRIME_A as i128)
.wrapping_mul(PRIME_B as i128)
}
}