pub struct Rng { /* private fields */ }Expand description
Oddiy, tez va deterministik pseudo-random generator (xorshift64*).
Diqqat: kriptografiya uchun yaramaydi. Faqat demo/benchmark uchun.
§Misol
use rust_algorithms::util::Rng;
let mut rng = Rng::new(42);
let a: Vec<u64> = (0..5).map(|_| rng.next_u64() % 100).collect();
let mut rng2 = Rng::new(42);
let b: Vec<u64> = (0..5).map(|_| rng2.next_u64() % 100).collect();
assert_eq!(a, b); // bir xil urug' (seed) → bir xil ketma-ketlikImplementations§
Source§impl Rng
impl Rng
Sourcepub fn new(seed: u64) -> Self
pub fn new(seed: u64) -> Self
Yangi generator yaratadi. seed == 0 bo’lsa, avtomatik almashtiriladi.
Sourcepub fn below(&mut self, n: usize) -> usize
pub fn below(&mut self, n: usize) -> usize
0..n oralig’idagi tasodifiy usize (n > 0 bo’lishi shart).
Sourcepub fn shuffle<T>(&mut self, slice: &mut [T])
pub fn shuffle<T>(&mut self, slice: &mut [T])
Slicening elementlarini joyida aralashtiradi (Fisher–Yates, O(n)).
§Misol
use rust_algorithms::util::Rng;
let mut v: Vec<i32> = (0..10).collect();
Rng::new(7).shuffle(&mut v);
let mut sorted = v.clone();
sorted.sort();
assert_eq!(sorted, (0..10).collect::<Vec<_>>()); // elementlar yo'qolmadiAuto Trait Implementations§
impl Freeze for Rng
impl RefUnwindSafe for Rng
impl Send for Rng
impl Sync for Rng
impl Unpin for Rng
impl UnsafeUnpin for Rng
impl UnwindSafe for Rng
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more