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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
//! # randlib
//!
//! Dependency-less 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 (checkout the examples/usage.rs file for more examples)
//!
//! ```rust
//! use randlib::Rand;
//!
//! fn main() {
//! let mut rng = Rand::new();
//! println!("random usize: {}", rng.rand());
//! println!("random u8: {}", rng.rand_u8());
//! println!("random i8: {}", rng.rand_i8());
//! println!("random u16: {}", rng.rand_u16());
//! println!("random i16: {}", rng.rand_i16());
//! println!("random u32: {}", rng.rand_u32());
//! println!("random i32: {}", rng.rand_i32());
//! println!("random u64: {}", rng.rand_u64());
//! println!("random i64: {}", rng.rand_i64());
//! println!("random u128: {}", rng.rand_u128());
//! println!("random i128: {}", rng.rand_i128());
//! println!("random usize: {}", rng.rand_usize());
//! println!("random bool: {:?}", 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 (usize)
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_number: usize = rng.rand();
/// ```
#[inline]
pub fn rand(&mut self) -> usize {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32).wrapping_mul(PRIME_A) as usize
}
/// Generate random bool value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_bool: bool = rng.rand_bool();
/// ```
#[inline]
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 usize value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_number: usize = rng.rand_usize();
/// ```
#[inline]
pub fn rand_usize(&mut self) -> usize {
self.seed = PRIME_A.wrapping_mul(self.seed).wrapping_add(PRIME_B);
(self.seed >> 32).wrapping_mul(PRIME_A) as usize
}
/// Generate random u8 value
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_u8: u8 = rng.rand_u8();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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();
/// ```
#[inline]
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)
}
/// Generate random (usize) value in a range
/// from min: usize (inclusive)
/// to max: usize (inclusive)
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let random_number: usize = rng.rand_range(0, 10);
/// ```
#[inline]
pub fn rand_range(&mut self, min: usize, max: usize) -> usize {
self.rand() % (max + 1 - min) + min
}
/// Shuffle a Vector of generic type
///
/// # Examples
///
/// ```no_run
/// use randlib::Rand;
///
/// let mut rng = Rand::new();
/// let mut vector: Vec<usize> = vec![1, 2, 3];
/// rng.shuffle_vec(&mut vector);
/// ```
#[inline]
pub fn shuffle_vec<T>(&mut self, vector: &mut Vec<T>) {
let vector_len = vector.len();
for _ in 0..vector_len {
let rand_index1 = self.rand_range(0, vector_len - 1);
let rand_index2 = self.rand_range(0, vector_len - 1);
vector.swap(rand_index1, rand_index2);
}
}
}